forked from XiovV/Golty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube-dl.go
106 lines (98 loc) · 3.14 KB
/
youtube-dl.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"encoding/json"
"fmt"
"os/exec"
"path/filepath"
log "github.com/sirupsen/logrus"
)
func (target DownloadTarget) GetMetadata() (TargetMetadata, error) {
cmd := exec.Command("youtube-dl", "-j", "--playlist-end", "1", target.URL)
log.Info("executing youtube-dl command: ", cmd.String())
out, err := cmd.CombinedOutput()
if err != nil {
log.Error("From GetMetadata(): ", err)
return TargetMetadata{}, fmt.Errorf("From c.GetMetadata(): %v", string(out))
}
metaData := &TargetMetadata{}
if err = json.Unmarshal(out, metaData); err != nil {
log.Error("From GetMetadata(): ", err)
return TargetMetadata{}, fmt.Errorf("From c.GetMetadata(): %v", string(out))
}
return *metaData, nil
}
func (target DownloadTarget) GetLatestVideo() (string, error) {
log.Info("fetching latest upload")
cmd := exec.Command("youtube-dl", "-j", "--playlist-end", "1", target.URL)
log.Info("executing youtube-dl command: ", cmd.String())
out, err := cmd.CombinedOutput()
if err != nil {
log.Error(string(out))
log.Errorf("c.GetLatestVideo: %s | %s", err, string(out))
}
metaData := &TargetMetadata{}
if err = json.Unmarshal(out, metaData); err != nil {
log.Error("c.GetLatestVideo: ", err)
return "", fmt.Errorf("c.GetLatestVideo: %s", err)
}
log.Info("successfully fetched latest video ")
return metaData.ID, nil
}
func (target DownloadTarget) Download(downloadQuality, fileExtension string, downloadEntire bool) error {
log.Info("DOWNLOAD: ", target)
var ytdlCommand YTDLCommand
if target.DownloadMode == "Audio Only" {
log.Info("downloading audio only")
switch downloadQuality {
case "best":
downloadQuality = "0"
case "medium":
downloadQuality = "5"
case "worst":
downloadQuality = "9"
}
log.Info("download quality set to: ", downloadQuality)
}
ytdlCommand = YTDLCommand{
Binary: "youtube-dl",
Target: target.URL,
}
switch target.DownloadMode {
case "Audio Only":
ytdlCommand.FileType = "bestaudio"
ytdlCommand.Output = filepath.Join(dlRoot, target.DownloadPath)
case "Video And Audio":
ytdlCommand.FileType = "bestvideo[height<=" + downloadQuality + "]" + "+ bestaudio/best[height<=" + downloadQuality + "]"
ytdlCommand.Output = filepath.Join(dlRoot, target.DownloadPath)
}
switch downloadEntire {
case true:
ytdlCommand.FirstFlag = "--ignore-errors"
case false:
ytdlCommand.FirstFlag = "--playlist-end"
ytdlCommand.FirstFlagArg = "1"
}
err := DownloadVideo(ytdlCommand)
if err != nil {
return fmt.Errorf("Download: %v", err)
}
return nil
}
func DownloadVideo(command YTDLCommand) error {
log.Info("downloading video")
var cmd *exec.Cmd
log.Info(command)
if command.FirstFlag == "" {
cmd = exec.Command(command.Binary, "-f", command.FileType, "-o", command.Output, command.Target)
} else {
cmd = exec.Command(command.Binary, command.FirstFlag, command.FirstFlagArg, "-f", command.FileType, "-o", command.Output, command.Target)
}
log.Info("executing youtube-dl command: ", cmd.String())
out, err := cmd.CombinedOutput()
if err != nil {
log.Error("There was an error downloading the video: ", string(out))
return fmt.Errorf("DownloadVideo: %s", string(out))
}
log.Info(string(out))
return nil
}