]> jfr.im git - yt-dlp.git/blame - test/test_YoutubeDL.py
[YoutubeDL] _calc_cookies: add get_header method to _PseudoRequest (#4861)
[yt-dlp.git] / test / test_YoutubeDL.py
CommitLineData
e028d0d1
JMF
1#!/usr/bin/env python
2
89087418
PH
3from __future__ import unicode_literals
4
5d254f77
PH
5# Allow direct execution
6import os
e028d0d1
JMF
7import sys
8import unittest
5d254f77 9sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
e028d0d1 10
0217c783
PH
11import copy
12
c57f7757 13from test.helper import FakeYDL, assertRegexpMatches
26e63931 14from youtube_dl import YoutubeDL
3d4a70b8 15from youtube_dl.extractor import YoutubeIE
e028d0d1 16
e028d0d1
JMF
17
18class YDL(FakeYDL):
f4d96df0
PH
19 def __init__(self, *args, **kwargs):
20 super(YDL, self).__init__(*args, **kwargs)
e028d0d1 21 self.downloaded_info_dicts = []
f4d96df0 22 self.msgs = []
5d254f77 23
e028d0d1
JMF
24 def process_info(self, info_dict):
25 self.downloaded_info_dicts.append(info_dict)
26
f4d96df0
PH
27 def to_screen(self, msg):
28 self.msgs.append(msg)
29
5d254f77 30
3537b93d
PH
31def _make_result(formats, **kwargs):
32 res = {
33 'formats': formats,
34 'id': 'testid',
35 'title': 'testttitle',
36 'extractor': 'testex',
37 }
38 res.update(**kwargs)
39 return res
40
41
e028d0d1
JMF
42class TestFormatSelection(unittest.TestCase):
43 def test_prefer_free_formats(self):
44 # Same resolution => download webm
45 ydl = YDL()
46 ydl.params['prefer_free_formats'] = True
5d254f77 47 formats = [
3537b93d
PH
48 {'ext': 'webm', 'height': 460, 'url': 'x'},
49 {'ext': 'mp4', 'height': 460, 'url': 'y'},
5d254f77 50 ]
3537b93d 51 info_dict = _make_result(formats)
3d4a70b8
PH
52 yie = YoutubeIE(ydl)
53 yie._sort_formats(info_dict['formats'])
e028d0d1
JMF
54 ydl.process_ie_result(info_dict)
55 downloaded = ydl.downloaded_info_dicts[0]
89087418 56 self.assertEqual(downloaded['ext'], 'webm')
e028d0d1
JMF
57
58 # Different resolution => download best quality (mp4)
59 ydl = YDL()
60 ydl.params['prefer_free_formats'] = True
5d254f77 61 formats = [
3537b93d
PH
62 {'ext': 'webm', 'height': 720, 'url': 'a'},
63 {'ext': 'mp4', 'height': 1080, 'url': 'b'},
5d254f77 64 ]
89087418 65 info_dict['formats'] = formats
3d4a70b8
PH
66 yie = YoutubeIE(ydl)
67 yie._sort_formats(info_dict['formats'])
e028d0d1
JMF
68 ydl.process_ie_result(info_dict)
69 downloaded = ydl.downloaded_info_dicts[0]
89087418 70 self.assertEqual(downloaded['ext'], 'mp4')
e028d0d1 71
1c783bca 72 # No prefer_free_formats => prefer mp4 and flv for greater compatibility
e028d0d1
JMF
73 ydl = YDL()
74 ydl.params['prefer_free_formats'] = False
5d254f77 75 formats = [
3537b93d
PH
76 {'ext': 'webm', 'height': 720, 'url': '_'},
77 {'ext': 'mp4', 'height': 720, 'url': '_'},
78 {'ext': 'flv', 'height': 720, 'url': '_'},
5d254f77 79 ]
89087418 80 info_dict['formats'] = formats
3d4a70b8
PH
81 yie = YoutubeIE(ydl)
82 yie._sort_formats(info_dict['formats'])
83 ydl.process_ie_result(info_dict)
84 downloaded = ydl.downloaded_info_dicts[0]
89087418 85 self.assertEqual(downloaded['ext'], 'mp4')
3d4a70b8
PH
86
87 ydl = YDL()
88 ydl.params['prefer_free_formats'] = False
89 formats = [
3537b93d
PH
90 {'ext': 'flv', 'height': 720, 'url': '_'},
91 {'ext': 'webm', 'height': 720, 'url': '_'},
3d4a70b8 92 ]
89087418 93 info_dict['formats'] = formats
3d4a70b8
PH
94 yie = YoutubeIE(ydl)
95 yie._sort_formats(info_dict['formats'])
e028d0d1
JMF
96 ydl.process_ie_result(info_dict)
97 downloaded = ydl.downloaded_info_dicts[0]
89087418 98 self.assertEqual(downloaded['ext'], 'flv')
e028d0d1 99
f4d96df0
PH
100 def test_format_limit(self):
101 formats = [
89087418
PH
102 {'format_id': 'meh', 'url': 'http://example.com/meh', 'preference': 1},
103 {'format_id': 'good', 'url': 'http://example.com/good', 'preference': 2},
104 {'format_id': 'great', 'url': 'http://example.com/great', 'preference': 3},
105 {'format_id': 'excellent', 'url': 'http://example.com/exc', 'preference': 4},
f4d96df0 106 ]
3537b93d 107 info_dict = _make_result(formats)
f4d96df0
PH
108
109 ydl = YDL()
110 ydl.process_ie_result(info_dict)
111 downloaded = ydl.downloaded_info_dicts[0]
89087418 112 self.assertEqual(downloaded['format_id'], 'excellent')
f4d96df0
PH
113
114 ydl = YDL({'format_limit': 'good'})
115 assert ydl.params['format_limit'] == 'good'
8e3e0322 116 ydl.process_ie_result(info_dict.copy())
f4d96df0 117 downloaded = ydl.downloaded_info_dicts[0]
89087418 118 self.assertEqual(downloaded['format_id'], 'good')
f4d96df0
PH
119
120 ydl = YDL({'format_limit': 'great', 'format': 'all'})
8e3e0322 121 ydl.process_ie_result(info_dict.copy())
89087418
PH
122 self.assertEqual(ydl.downloaded_info_dicts[0]['format_id'], 'meh')
123 self.assertEqual(ydl.downloaded_info_dicts[1]['format_id'], 'good')
124 self.assertEqual(ydl.downloaded_info_dicts[2]['format_id'], 'great')
f4d96df0
PH
125 self.assertTrue('3' in ydl.msgs[0])
126
127 ydl = YDL()
128 ydl.params['format_limit'] = 'excellent'
8e3e0322 129 ydl.process_ie_result(info_dict.copy())
f4d96df0 130 downloaded = ydl.downloaded_info_dicts[0]
89087418 131 self.assertEqual(downloaded['format_id'], 'excellent')
f4d96df0 132
a9c58ad9
JMF
133 def test_format_selection(self):
134 formats = [
3537b93d
PH
135 {'format_id': '35', 'ext': 'mp4', 'preference': 1, 'url': '_'},
136 {'format_id': '45', 'ext': 'webm', 'preference': 2, 'url': '_'},
137 {'format_id': '47', 'ext': 'webm', 'preference': 3, 'url': '_'},
138 {'format_id': '2', 'ext': 'flv', 'preference': 4, 'url': '_'},
a9c58ad9 139 ]
3537b93d 140 info_dict = _make_result(formats)
a9c58ad9 141
89087418 142 ydl = YDL({'format': '20/47'})
8e3e0322 143 ydl.process_ie_result(info_dict.copy())
a9c58ad9 144 downloaded = ydl.downloaded_info_dicts[0]
89087418 145 self.assertEqual(downloaded['format_id'], '47')
a9c58ad9 146
89087418 147 ydl = YDL({'format': '20/71/worst'})
8e3e0322 148 ydl.process_ie_result(info_dict.copy())
a9c58ad9 149 downloaded = ydl.downloaded_info_dicts[0]
89087418 150 self.assertEqual(downloaded['format_id'], '35')
a9c58ad9
JMF
151
152 ydl = YDL()
8e3e0322 153 ydl.process_ie_result(info_dict.copy())
a9c58ad9 154 downloaded = ydl.downloaded_info_dicts[0]
89087418 155 self.assertEqual(downloaded['format_id'], '2')
a9c58ad9 156
89087418 157 ydl = YDL({'format': 'webm/mp4'})
8e3e0322 158 ydl.process_ie_result(info_dict.copy())
49e86983 159 downloaded = ydl.downloaded_info_dicts[0]
89087418 160 self.assertEqual(downloaded['format_id'], '47')
49e86983 161
89087418 162 ydl = YDL({'format': '3gp/40/mp4'})
8e3e0322 163 ydl.process_ie_result(info_dict.copy())
49e86983 164 downloaded = ydl.downloaded_info_dicts[0]
89087418 165 self.assertEqual(downloaded['format_id'], '35')
49e86983 166
ba7678f9
PH
167 def test_format_selection_audio(self):
168 formats = [
3537b93d
PH
169 {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none', 'url': '_'},
170 {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none', 'url': '_'},
171 {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none', 'url': '_'},
172 {'format_id': 'vid', 'ext': 'mp4', 'preference': 4, 'url': '_'},
ba7678f9 173 ]
3537b93d 174 info_dict = _make_result(formats)
ba7678f9 175
89087418 176 ydl = YDL({'format': 'bestaudio'})
ba7678f9
PH
177 ydl.process_ie_result(info_dict.copy())
178 downloaded = ydl.downloaded_info_dicts[0]
89087418 179 self.assertEqual(downloaded['format_id'], 'audio-high')
ba7678f9 180
89087418 181 ydl = YDL({'format': 'worstaudio'})
ba7678f9
PH
182 ydl.process_ie_result(info_dict.copy())
183 downloaded = ydl.downloaded_info_dicts[0]
89087418 184 self.assertEqual(downloaded['format_id'], 'audio-low')
ba7678f9
PH
185
186 formats = [
3537b93d
PH
187 {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': '_'},
188 {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': '_'},
ba7678f9 189 ]
3537b93d 190 info_dict = _make_result(formats)
ba7678f9 191
89087418 192 ydl = YDL({'format': 'bestaudio/worstaudio/best'})
ba7678f9
PH
193 ydl.process_ie_result(info_dict.copy())
194 downloaded = ydl.downloaded_info_dicts[0]
89087418 195 self.assertEqual(downloaded['format_id'], 'vid-high')
ba7678f9 196
0217c783
PH
197 def test_format_selection_audio_exts(self):
198 formats = [
199 {'format_id': 'mp3-64', 'ext': 'mp3', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
200 {'format_id': 'ogg-64', 'ext': 'ogg', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
201 {'format_id': 'aac-64', 'ext': 'aac', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
202 {'format_id': 'mp3-32', 'ext': 'mp3', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'},
203 {'format_id': 'aac-32', 'ext': 'aac', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'},
204 ]
205
206 info_dict = _make_result(formats)
207 ydl = YDL({'format': 'best'})
208 ie = YoutubeIE(ydl)
209 ie._sort_formats(info_dict['formats'])
210 ydl.process_ie_result(copy.deepcopy(info_dict))
211 downloaded = ydl.downloaded_info_dicts[0]
212 self.assertEqual(downloaded['format_id'], 'aac-64')
213
214 ydl = YDL({'format': 'mp3'})
215 ie = YoutubeIE(ydl)
216 ie._sort_formats(info_dict['formats'])
217 ydl.process_ie_result(copy.deepcopy(info_dict))
218 downloaded = ydl.downloaded_info_dicts[0]
219 self.assertEqual(downloaded['format_id'], 'mp3-64')
220
221 ydl = YDL({'prefer_free_formats': True})
222 ie = YoutubeIE(ydl)
223 ie._sort_formats(info_dict['formats'])
224 ydl.process_ie_result(copy.deepcopy(info_dict))
225 downloaded = ydl.downloaded_info_dicts[0]
226 self.assertEqual(downloaded['format_id'], 'ogg-64')
227
bc6d5978
JMF
228 def test_format_selection_video(self):
229 formats = [
3537b93d
PH
230 {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none', 'url': '_'},
231 {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none', 'url': '_'},
232 {'format_id': 'vid', 'ext': 'mp4', 'preference': 3, 'url': '_'},
bc6d5978 233 ]
3537b93d 234 info_dict = _make_result(formats)
bc6d5978
JMF
235
236 ydl = YDL({'format': 'bestvideo'})
237 ydl.process_ie_result(info_dict.copy())
238 downloaded = ydl.downloaded_info_dicts[0]
239 self.assertEqual(downloaded['format_id'], 'dash-video-high')
240
241 ydl = YDL({'format': 'worstvideo'})
242 ydl.process_ie_result(info_dict.copy())
243 downloaded = ydl.downloaded_info_dicts[0]
244 self.assertEqual(downloaded['format_id'], 'dash-video-low')
245
3d4a70b8
PH
246 def test_youtube_format_selection(self):
247 order = [
248 '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '36', '17', '13',
249 # Apple HTTP Live Streaming
250 '96', '95', '94', '93', '92', '132', '151',
251 # 3D
252 '85', '84', '102', '83', '101', '82', '100',
253 # Dash video
c11125f9 254 '137', '248', '136', '247', '135', '246',
3d4a70b8
PH
255 '245', '244', '134', '243', '133', '242', '160',
256 # Dash audio
a053c349 257 '141', '172', '140', '171', '139',
3d4a70b8
PH
258 ]
259
260 for f1id, f2id in zip(order, order[1:]):
261 f1 = YoutubeIE._formats[f1id].copy()
262 f1['format_id'] = f1id
3537b93d 263 f1['url'] = 'url:' + f1id
3d4a70b8
PH
264 f2 = YoutubeIE._formats[f2id].copy()
265 f2['format_id'] = f2id
3537b93d 266 f2['url'] = 'url:' + f2id
3d4a70b8 267
3537b93d 268 info_dict = _make_result([f1, f2], extractor='youtube')
3d4a70b8
PH
269 ydl = YDL()
270 yie = YoutubeIE(ydl)
271 yie._sort_formats(info_dict['formats'])
272 ydl.process_ie_result(info_dict)
273 downloaded = ydl.downloaded_info_dicts[0]
274 self.assertEqual(downloaded['format_id'], f1id)
275
3537b93d 276 info_dict = _make_result([f2, f1], extractor='youtube')
3d4a70b8
PH
277 ydl = YDL()
278 yie = YoutubeIE(ydl)
279 yie._sort_formats(info_dict['formats'])
280 ydl.process_ie_result(info_dict)
281 downloaded = ydl.downloaded_info_dicts[0]
282 self.assertEqual(downloaded['format_id'], f1id)
283
083c9df9
PH
284 def test_format_filtering(self):
285 formats = [
286 {'format_id': 'A', 'filesize': 500, 'width': 1000},
287 {'format_id': 'B', 'filesize': 1000, 'width': 500},
288 {'format_id': 'C', 'filesize': 1000, 'width': 400},
289 {'format_id': 'D', 'filesize': 2000, 'width': 600},
290 {'format_id': 'E', 'filesize': 3000},
291 {'format_id': 'F'},
292 {'format_id': 'G', 'filesize': 1000000},
293 ]
294 for f in formats:
295 f['url'] = 'http://_/'
296 f['ext'] = 'unknown'
297 info_dict = _make_result(formats)
298
299 ydl = YDL({'format': 'best[filesize<3000]'})
300 ydl.process_ie_result(info_dict)
301 downloaded = ydl.downloaded_info_dicts[0]
302 self.assertEqual(downloaded['format_id'], 'D')
303
304 ydl = YDL({'format': 'best[filesize<=3000]'})
305 ydl.process_ie_result(info_dict)
306 downloaded = ydl.downloaded_info_dicts[0]
307 self.assertEqual(downloaded['format_id'], 'E')
308
309 ydl = YDL({'format': 'best[filesize <= ? 3000]'})
310 ydl.process_ie_result(info_dict)
311 downloaded = ydl.downloaded_info_dicts[0]
312 self.assertEqual(downloaded['format_id'], 'F')
313
314 ydl = YDL({'format': 'best [filesize = 1000] [width>450]'})
315 ydl.process_ie_result(info_dict)
316 downloaded = ydl.downloaded_info_dicts[0]
317 self.assertEqual(downloaded['format_id'], 'B')
318
319 ydl = YDL({'format': 'best [filesize = 1000] [width!=450]'})
320 ydl.process_ie_result(info_dict)
321 downloaded = ydl.downloaded_info_dicts[0]
322 self.assertEqual(downloaded['format_id'], 'C')
323
324 ydl = YDL({'format': '[filesize>?1]'})
325 ydl.process_ie_result(info_dict)
326 downloaded = ydl.downloaded_info_dicts[0]
327 self.assertEqual(downloaded['format_id'], 'G')
328
329 ydl = YDL({'format': '[filesize<1M]'})
330 ydl.process_ie_result(info_dict)
331 downloaded = ydl.downloaded_info_dicts[0]
332 self.assertEqual(downloaded['format_id'], 'E')
333
334 ydl = YDL({'format': '[filesize<1MiB]'})
335 ydl.process_ie_result(info_dict)
336 downloaded = ydl.downloaded_info_dicts[0]
337 self.assertEqual(downloaded['format_id'], 'G')
338
b6c45014
JMF
339 def test_add_extra_info(self):
340 test_dict = {
341 'extractor': 'Foo',
342 }
343 extra_info = {
344 'extractor': 'Bar',
345 'playlist': 'funny videos',
346 }
347 YDL.add_extra_info(test_dict, extra_info)
348 self.assertEqual(test_dict['extractor'], 'Foo')
349 self.assertEqual(test_dict['playlist'], 'funny videos')
350
26e63931
JMF
351 def test_prepare_filename(self):
352 info = {
89087418
PH
353 'id': '1234',
354 'ext': 'mp4',
355 'width': None,
26e63931 356 }
5f6a1245 357
26e63931
JMF
358 def fname(templ):
359 ydl = YoutubeDL({'outtmpl': templ})
360 return ydl.prepare_filename(info)
89087418
PH
361 self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4')
362 self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4')
26e63931 363 # Replace missing fields with 'NA'
89087418 364 self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4')
26e63931 365
c57f7757
PH
366 def test_format_note(self):
367 ydl = YoutubeDL()
368 self.assertEqual(ydl._format_note({}), '')
369 assertRegexpMatches(self, ydl._format_note({
370 'vbr': 10,
1c783bca 371 }), '^\s*10k$')
f4d96df0 372
e028d0d1
JMF
373if __name__ == '__main__':
374 unittest.main()