forked from BernardZhao/Jukebox
-
Notifications
You must be signed in to change notification settings - Fork 2
/
music.go
38 lines (31 loc) · 970 Bytes
/
music.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"encoding/json"
"os/exec"
)
// Song : Song information fetched from youtube-dl. More fields exist in the JSON response, but this is what we need.
type Song struct {
Title string `json:"title"`
URL string `json:"url"`
Thumbnail string `json:"thumbnail"`
WebpageURL string `json:"webpage_url"`
}
func fetchSong(url string) (Song, error) {
// Maybe make this function return a promise or something in the future
var songData Song
// JSON dump has no extra overhead, and we get more info that we need that might be useful
cmd := exec.Command("yt-dlp", "--no-playlist", "-J", "--youtube-skip-dash-manifest", "-f bestaudio", url)
stdOut, err := cmd.StdoutPipe()
if err != nil {
return Song{}, err
}
if err := cmd.Start(); err != nil {
return Song{}, err
}
if err := json.NewDecoder(stdOut).Decode(&songData); err != nil {
return Song{}, err
}
cmd.Wait()
// Only using these fields atm
return songData, nil
}