-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
102 lines (71 loc) · 1.64 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
package main
import (
"fmt"
)
const (
QUALITY_HIGHRES = "highres"
QUALITY_HD1080 = "hd1080"
QUALITY_HD720 = "hd720"
QUALITY_LARGE = "large"
QUALITY_MEDIUM = "medium"
QUALITY_SMALL = "small"
QUALITY_MIN = "min"
QUALITY_MAX = "max"
QUALITY_UNKNOWN = "unknown"
FORMAT_MP4 = "mp4"
FORMAT_WEBM = "webm"
FORMAT_FLV = "flv"
FORMAT_3GP = "3ggp"
FORMAT_UNKNOWN = "unknown"
AUDIO_BITRATE_AUTO = 0
AUDIO_BITRATE_LOW = 64
AUDIO_BITRATE_MEDIUM = 128
AUDIO_BITRATE_HIGH = 192
DEFAULT_DESTINATION = "./%title%.%format%"
DEFAULT_DESTINATION_MP3 = "./%title%.mp3"
)
func log(format string, params ...interface{}) {
if cfg.verbose {
fmt.Printf(format + "\n", params...)
}
}
func main() {
videoId, err := cfg.findVideoId()
if err != nil {
fmt.Printf("ERROR: unable to detect the video id: %s\n", err)
return
}
response, err := getVideoInfo(videoId)
if err != nil {
fmt.Printf("ERROR: unable to request the video information: %s\n", err)
return
}
streams, err := decodeVideoInfo(response)
if err != nil {
fmt.Printf("ERROR: unable to decode the server's answer: %s\n", err)
return
}
stream, err := cfg.selectStream(streams)
if err != nil {
fmt.Printf("ERROR: unable to select a stream: %s\n", err)
return
}
out, err := getWriter(cfg, stream)
if err != nil {
fmt.Printf("ERROR: unable to create the output writer: %s\n", err)
return
}
defer func() {
err = out.Close()
if err != nil {
fmt.Printf("ERROR: unable to close destination: %s\n", err)
}
}()
err = stream.download(out)
if err != nil {
fmt.Printf("ERROR: unable to download the stream: %s\n", err)
return
}
fmt.Printf("Done\n")
return
}