forked from rylio/ytdl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo_info_test.go
More file actions
229 lines (209 loc) · 9.37 KB
/
video_info_test.go
File metadata and controls
229 lines (209 loc) · 9.37 KB
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
package ytdl
import (
"context"
"io/ioutil"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Run a single test with:
func TestVideoInfo(t *testing.T) {
tests := []struct {
url string
errorMessage string
duration time.Duration
published time.Time
title string
uploader string
description string
song string
artist string
}{
{
url: "https://www.youtube.com/",
errorMessage: "invalid youtube URL, no video id",
},
{
url: "https://www.facebook.com/video.php?v=10153820411888896",
errorMessage: "invalid youtube URL, no video id",
},
//{
// url: "https://www.youtube.com/watch?v=TDgn8k9uyW4",
//},
{
url: "https://www.youtube.com/watch?v=BaW_jenozKc",
title: `youtube-dl test video "'/\ä↭𝕐`,
uploader: "Philipp Hagemeister",
duration: time.Second * 10,
published: newDate(2012, 10, 2),
description: "test chars: \"'/\\ä↭𝕐\ntest URL: https://github.com/rg3/youtube-dl/iss...\n\nThis is a test video for youtube-dl.\n\nFor more information, contact phihag@phihag.de .",
},
{
url: "https://www.youtube.com/watch?v=YQHsXMglC9A",
title: "Adele - Hello",
uploader: "AdeleVEVO",
duration: time.Second * 367,
published: newDate(2015, 10, 22),
artist: "Adele",
song: "Hello",
description: "‘Hello' is taken from the new album, 25, out November 20. http://adele.com\nAvailable now from iTunes http://smarturl.it/itunes25 \nAvailable now from Amazon http://smarturl.it/25amazon \nAvailable now from Google Play http://smarturl.it/25gplay\nAvailable now at Target (US Only): http://smarturl.it/target25\n\nDirected by Xavier Dolan, @XDolan\n\nFollow Adele on:\n\nFacebook - https://www.facebook.com/Adele\nTwitter - https://twitter.com/Adele \nInstagram - http://instagram.com/Adele\n\nhttp://vevo.ly/jzAuJ1\n\nCommissioner: Phil Lee\nProduction Company: Believe Media/Sons of Manual/Metafilms\nDirector: Xavier Dolan\nExecutive Producer: Jannie McInnes\nProducer: Nancy Grant/Xavier Dolan\nCinematographer: André Turpin\nProduction design : Colombe Raby\nEditor: Xavier Dolan\nAdele's lover : Tristan Wilds",
},
{
url: "https://www.youtube.com/watch?v=H-30B0cqh88",
title: "Kung Fu Panda 3 Official Trailer #3 (2016) - Jack Black, Angelina Jolie Animated Movie HD",
uploader: "Movieclips Trailers",
duration: time.Second * 145,
published: newDate(2015, 12, 16),
description: "Subscribe to TRAILERS: http://bit.ly/sxaw6h\nSubscribe to COMING SOON: http://bit.ly/H2vZUn\nLike us on FACEBOOK: http://bit.ly/1QyRMsE\nFollow us on TWITTER: http://bit.ly/1ghOWmt\nKung Fu Panda 3 Official International Trailer #1 (2016) - Jack Black, Angelina Jolie Animation HD\n\nIn 2016, one of the most successful animated franchises in the world returns with its biggest comedy adventure yet, KUNG FU PANDA 3. When Po's long-lost panda father suddenly reappears, the reunited duo travels to a secret panda paradise to meet scores of hilarious new panda characters. But when the supernatural villain Kai begins to sweep across China defeating all the kung fu masters, Po must do the impossible - learn to train a village full of his fun-loving, clumsy brethren to become the ultimate band of Kung Fu Pandas!\n\nThe Fandango MOVIECLIPS Trailers channel is your destination for the hottest new trailers the second they drop. Whether it's the latest studio release, an indie horror flick, an evocative documentary, or that new RomCom you've been waiting for, the Fandango MOVIECLIPS team is here day and night to make sure all the best new movie trailers are here for you the moment they're released.\n\nIn addition to being the #1 Movie Trailers Channel on YouTube, we deliver amazing and engaging original videos each week. Watch our exclusive Ultimate Trailers, Showdowns, Instant Trailer Reviews, Monthly MashUps, Movie News, and so much more to keep you in the know.\n\nHere at Fandango MOVIECLIPS, we love movies as much as you!",
},
// Test VEVO video with age protection
// https://github.com/ytdl-org/youtube-dl/issues/956
{
url: "https://www.youtube.com/watch?v=07FYdnEawAQ",
title: `Justin Timberlake - Tunnel Vision (Official Music Video) (Explicit)`,
uploader: "justintimberlakeVEVO",
duration: time.Second * 419,
published: newDate(2013, 7, 3),
song: "Tunnel Vision",
artist: "Justin Timberlake",
description: "Executive Producer: Jeff Nicholas \nProduced by Jonathan Craven and Nathan Scherrer \nDirected by Jonathan Craven, Simon McLoughlin and Jeff Nicholas for The Uprising Creative (http://theuprisingcreative.com) \nDirector Of Photography: Sing Howe Yam \nEditor: Jacqueline London\n\nOfficial music video by Justin Timberlake performing Tunnel Vision (Explicit). (C) 2013 RCA Records, a division of Sony Music Entertainment\n\n#JustinTimberlake #TunnelVision #Vevo #Pop #OfficialMuiscVideo",
},
{
url: "https://www.youtube.com/watch?v=qHGTs1NSB1s",
title: "Why Linus Torvalds doesn't use Ubuntu or Debian",
uploader: "TFiR",
description: `Subscribe to our weekly newsletter: https://www.tfir.io/dnl
Become a patron of this channel: https://www.patreon.com/TFIR
Follow us on Twitter: https://twitter.com/tfir_io
Like us on Facebook: https://www.facebook.com/TFiRMedia/
Linus gives the practical reasons why he doesn't use Ubuntu or Debian.`,
duration: time.Second * 162,
published: newDate(2014, 9, 3),
},
// 256k DASH audio (format 141) via DASH manifest
{
url: "https://www.youtube.com/watch?v=a9LDPn-MO4I",
title: "UHDTV TEST 8K VIDEO.mp4",
uploader: "8KVIDEO",
description: "",
duration: time.Second * 60,
published: newDate(2012, 10, 2),
},
}
for _, tt := range tests {
t.Run(tt.url, func(t *testing.T) {
assert := assert.New(t)
client := newTestClient(t)
info, err := client.GetVideoInfo(context.Background(), tt.url)
if tt.errorMessage != "" {
// we expect an error
assert.EqualError(err, tt.errorMessage)
} else {
if assert.NoError(err) {
assert.Equal(tt.duration, info.Duration, "Duration mismatch")
assert.Equal(tt.title, info.Title, "Title mismatch")
assert.Equal(tt.published, info.DatePublished, "DatePublished mismatch")
assert.Equal(tt.uploader, info.Uploader, "Uploader mismatch")
assert.Equal(tt.song, info.Song, "Song mismatch")
assert.Equal(tt.artist, info.Artist, "Artist mismatch")
assert.Equal(tt.description, info.Description, "Description mismatch")
}
}
})
}
}
func TestExtractIDfromValidURL(t *testing.T) {
tests := []string{
"http://youtube.com/watch?v=BaW_jenozKc",
"https://youtube.com/watch?v=BaW_jenozKc",
"https://m.youtube.com/watch?v=BaW_jenozKc",
"https://www.youtube.com/watch?v=BaW_jenozKc",
"https://www.youtube.com/watch?v=BaW_jenozKc&v=UxxajLWwzqY",
"https://www.youtube.com/embed/BaW_jenozKc?list=PLEbnTDJUr_IegfoqO4iPnPYQui46QqT0j",
"https://youtu.be/BaW_jenozKc",
}
for _, input := range tests {
t.Run(input, func(t *testing.T) {
uri, err := url.ParseRequestURI(input)
require.NoError(t, err)
assert.Equal(t, "BaW_jenozKc", extractVideoID(uri))
})
}
}
func TestExtractIDfromInvalidURL(t *testing.T) {
uri, err := url.ParseRequestURI("https://otherhost.com/watch?v=BaW_jenozKc")
require.NoError(t, err)
assert.Equal(t, "", extractVideoID(uri))
}
func TestGetDownloadURL(t *testing.T) {
testCases := []string{
"FrG4TEcSuRg",
"jgVhBThJdXc",
"MXgnIP4rMoI",
"peBgUMT26jM",
"aQZDbBGBJsM",
"cRS4mS4gKwg",
"0fllyJTBsRU",
}
for _, id := range testCases {
t.Run(id, func(t *testing.T) {
client := newTestClient(t)
info, err := client.GetVideoInfo(context.Background(), "https://www.youtube.com/watch?v="+id)
require.NoError(t, err)
if len(info.Formats) == 0 {
t.Fatal("empty format list")
}
format := info.Formats.Worst(FormatResolutionKey)[0]
_, err = client.GetDownloadURL(context.Background(), info, format)
assert.NoError(t, err)
})
}
}
func TestDownloadVideo(t *testing.T) {
client := newTestClient(t)
info, err := client.GetVideoInfo(context.Background(), "https://www.youtube.com/watch?v=FrG4TEcSuRg")
if err != nil {
t.Fatal(err)
}
format := info.Formats.Worst(FormatResolutionKey)[0]
err = client.Download(context.Background(), info, format, ioutil.Discard)
if err != nil {
t.Error(err)
}
}
func TestThumbnail(t *testing.T) {
client := newTestClient(t)
info, err := client.GetVideoInfo(context.Background(), "https://www.youtube.com/watch?v=FrG4TEcSuRg")
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
// test valid thumnail qualities
qualities := []ThumbnailQuality{
ThumbnailQualityDefault,
ThumbnailQualityHigh,
ThumbnailQualityMaxRes,
ThumbnailQualityMedium,
ThumbnailQualitySD,
}
for _, v := range qualities {
t.Run(string(v), func(t *testing.T) {
u := info.GetThumbnailURL(v)
resp, err := client.httpGetAndCheckResponse(ctx, u.String())
if assert.NoError(t, err) {
resp.Body.Close()
}
})
}
// test invalid thumbnail quality
t.Run("invalid", func(t *testing.T) {
u := info.GetThumbnailURL(ThumbnailQuality("invalid"))
_, err = client.httpGetAndCheckResponse(ctx, u.String())
assert.EqualError(t, err, "unexpected status code: 404")
})
}
func newDate(y, m, d int) time.Time {
return time.Date(y, time.Month(m), d, 0, 0, 0, 0, time.UTC)
}