forked from lithdew/youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (66 loc) · 2.04 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
package main
import (
"fmt"
"github.com/lithdew/nicehttp"
"github.com/lithdew/youtube"
)
func check(err error) {
if err != nil {
panic(err)
}
}
func main() {
// Search for the song Animus Vox by The Glitch Mob.
results, err := youtube.Search("animus vox", 0)
check(err)
fmt.Printf("Got %d search result(s).\n\n", results.Hits)
if len(results.Items) == 0 {
check(fmt.Errorf("got zero search results"))
}
// Get the first search result and print out its details.
details := results.Items[0]
fmt.Printf(
"ID: %q\n\nTitle: %q\nAuthor: %q\nDuration: %q\n\nView Count: %q\nLikes: %d\nDislikes: %d\n\n",
details.ID,
details.Title,
details.Author,
details.Duration,
details.Views,
details.Likes,
details.Dislikes,
)
// Instantiate a player for the first search result.
player, err := youtube.Load(details.ID)
check(err)
// Fetch audio-only direct link.
stream, ok := player.SourceFormats().AudioOnly().BestAudio()
if !ok {
check(fmt.Errorf("no audio-only stream available"))
}
audioOnlyFilename := "audio." + stream.FileExtension()
audioOnlyURL, err := player.ResolveURL(stream)
check(err)
fmt.Printf("Audio-only direct link: %q\n", audioOnlyURL)
// Fetch video-only direct link.
stream, ok = player.SourceFormats().VideoOnly().BestVideo()
if !ok {
check(fmt.Errorf("no video-only stream available"))
}
videoOnlyFilename := "video." + stream.FileExtension()
videoOnlyURL, err := player.ResolveURL(stream)
check(err)
fmt.Printf("Video-only direct link: %q\n", videoOnlyURL)
// Fetch muxed video/audio direct link.
stream, ok = player.MuxedFormats().BestVideo()
if !ok {
check(fmt.Errorf("no muxed stream available"))
}
muxedFilename := "muxed." + stream.FileExtension()
muxedURL, err := player.ResolveURL(stream)
check(err)
fmt.Printf("Muxed (video/audio) direct link: %q\n", muxedURL)
// Download all the links.
check(nicehttp.DownloadFile(audioOnlyFilename, audioOnlyURL))
check(nicehttp.DownloadFile(videoOnlyFilename, videoOnlyURL))
check(nicehttp.DownloadFile(muxedFilename, muxedURL))
}