forked from Te-cho/go-youtube-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
241 lines (197 loc) · 4.62 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"io"
"flag"
"fmt"
"net/url"
"os"
"log"
"os/exec"
"os/user"
"path/filepath"
)
const CMD = "youtube-dl"
const BEST_VIDEO_FORMAT = "18"
const BEST_AUDIO_FORMAT = "140"
// Youtube-dl Downloader
type YoutubeDl struct {
Path string
Format string
}
func extractVideoID(videoURL string) (string, error) {
parsedURL, err := url.Parse(videoURL)
if err != nil {
return "", err
}
queryParams := parsedURL.Query()
videoID := queryParams.Get("v")
return videoID, nil
}
func (youtubedl YoutubeDl) DownloadSubs(url string) error {
videoID, error := extractVideoID(url)
if error != nil {
fmt.Fprintln(os.Stderr, error)
return error
}
cmdArgs := []string{
CMD,
"--write-auto-sub",
"--sub-lang",
"en",
"--output", youtubedl.Path + videoID + ".srt",
"--skip-download",
url,
}
err := run(cmdArgs)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return err
}
return nil
}
func (youtubedl YoutubeDl) DownloadAudio(url string) (error) {
cmdArgs := []string{
CMD,
"--format", youtubedl.Format,
"--extract-audio",
"--audio-format", "m4a",
"--audio-quality", "192",
"--output", youtubedl.Path + "%(title)s-%(id)s.%(ext)s\"",
"--write-thumbnail",
"--embed-thumbnail",
url,
}
err := run(cmdArgs)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return err
}
return nil
}
func (youtubedl YoutubeDl) DownloadVideo(videoURL string) error {
sep := string(filepath.Separator)
// Escape and quote the path to handle spaces or special characters
escapedPath := "\"" + youtubedl.Path + "%(title)s-%(id)s.%(ext)s\""
cmdArgs := []string{
CMD,
"--format", youtubedl.Format,
"--output", escapedPath,
"--embed-thumbnail",
videoURL,
}
return run(cmdArgs)
}
func run(cmdArgs []string) error {
fmt.Println("Starting ...")
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
fmt.Println("Executing command: ", cmd.String())
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
go func() {
defer stdout.Close()
io.Copy(os.Stdout, stdout)
}()
go func() {
defer stderr.Close()
io.Copy(os.Stderr, stderr)
}()
if err := cmd.Wait(); err != nil {
log.Fatal(err)
return err
}
return nil
}
func main() {
var (
audioFlag bool
format string
output string
subsFlag bool
url string
videoFlag bool
ytdlFlag bool
)
usr, err := user.Current()
if err != nil {
panic(err)
}
sep := string(filepath.Separator)
path := filepath.Join(sep, "home", usr.Username, "Music")
// Parsing command line flags
flag.StringVar(&url, "url", "", "url of the youtube /audio/video to download")
flag.BoolVar(&subsFlag, "s", false, "Download subs from the youtube for the specified video")
flag.BoolVar(&audioFlag, "a", false, "Download audio")
flag.BoolVar(&videoFlag, "v", false, "Download video")
flag.StringVar(&format, "f", "", "Specify video format")
flag.StringVar(&output, "o", path + sep, "Path where the video will be written to")
flag.BoolVar(&ytdlFlag, "y", false, "Find where youtube-dl is installed and its version")
flag.Parse()
if _, err := os.Stat(output); os.IsNotExist(err) {
fmt.Printf("Error: The specified path '%s' does not exist.\n", output)
os.Exit(1)
}
// If user did not provide youtube video url, show usage.
if url == "" && !ytdlFlag {
flag.Usage()
os.Exit(1)
}
ytdl := YoutubeDl{Path: output + sep}
switch {
case ytdlFlag:
cmdp := exec.Command("which", "youtube-dl")
output, err := cmdp.CombinedOutput()
if err != nil {
fmt.Printf("Error executing command: %v\n", err)
return
}
fmt.Printf("youtube-dl is located at:\n%s\n", output)
cmdv := exec.Command("youtube-dl", "--version")
version, error := cmdv.CombinedOutput()
if error != nil {
fmt.Printf("Error executing command: %v\n", error)
return
}
fmt.Printf("Version:\n%s\n", version)
case audioFlag:
if format != "" {
ytdl.Format = format
} else {
ytdl.Format = BEST_AUDIO_FORMAT
}
err := ytdl.DownloadAudio(url)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
case videoFlag:
if format != "" {
ytdl.Format = format
} else {
ytdl.Format = BEST_VIDEO_FORMAT
}
err := ytdl.DownloadVideo(url)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
case subsFlag:
fmt.Println("Downloading subs...")
err := ytdl.DownloadSubs(url)
if err != nil {
fmt.Println("Error downloading video.")
}
default:
// Invalid flag provided
fmt.Fprintln(os.Stderr, fmt.Sprintf("Invalid option"))
os.Exit(1)
}
}