]> jfr.im git - yt-dlp.git/blob - test/test_YoutubeDL.py
[cleanup] Point all shebang to `python3` (#372)
[yt-dlp.git] / test / test_YoutubeDL.py
1 #!/usr/bin/env python3
2 # coding: utf-8
3
4 from __future__ import unicode_literals
5
6 # Allow direct execution
7 import os
8 import sys
9 import unittest
10 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11
12 import copy
13
14 from test.helper import FakeYDL, assertRegexpMatches
15 from yt_dlp import YoutubeDL
16 from yt_dlp.compat import compat_str, compat_urllib_error
17 from yt_dlp.extractor import YoutubeIE
18 from yt_dlp.extractor.common import InfoExtractor
19 from yt_dlp.postprocessor.common import PostProcessor
20 from yt_dlp.utils import ExtractorError, match_filter_func
21
22 TEST_URL = 'http://localhost/sample.mp4'
23
24
25 class YDL(FakeYDL):
26 def __init__(self, *args, **kwargs):
27 super(YDL, self).__init__(*args, **kwargs)
28 self.downloaded_info_dicts = []
29 self.msgs = []
30
31 def process_info(self, info_dict):
32 info_dict.pop('__original_infodict', None)
33 self.downloaded_info_dicts.append(info_dict)
34
35 def to_screen(self, msg):
36 self.msgs.append(msg)
37
38
39 def _make_result(formats, **kwargs):
40 res = {
41 'formats': formats,
42 'id': 'testid',
43 'title': 'testttitle',
44 'extractor': 'testex',
45 'extractor_key': 'TestEx',
46 'webpage_url': 'http://example.com/watch?v=shenanigans',
47 }
48 res.update(**kwargs)
49 return res
50
51
52 class TestFormatSelection(unittest.TestCase):
53 def test_prefer_free_formats(self):
54 # Same resolution => download webm
55 ydl = YDL()
56 ydl.params['prefer_free_formats'] = True
57 formats = [
58 {'ext': 'webm', 'height': 460, 'url': TEST_URL},
59 {'ext': 'mp4', 'height': 460, 'url': TEST_URL},
60 ]
61 info_dict = _make_result(formats)
62 yie = YoutubeIE(ydl)
63 yie._sort_formats(info_dict['formats'])
64 ydl.process_ie_result(info_dict)
65 downloaded = ydl.downloaded_info_dicts[0]
66 self.assertEqual(downloaded['ext'], 'webm')
67
68 # Different resolution => download best quality (mp4)
69 ydl = YDL()
70 ydl.params['prefer_free_formats'] = True
71 formats = [
72 {'ext': 'webm', 'height': 720, 'url': TEST_URL},
73 {'ext': 'mp4', 'height': 1080, 'url': TEST_URL},
74 ]
75 info_dict['formats'] = formats
76 yie = YoutubeIE(ydl)
77 yie._sort_formats(info_dict['formats'])
78 ydl.process_ie_result(info_dict)
79 downloaded = ydl.downloaded_info_dicts[0]
80 self.assertEqual(downloaded['ext'], 'mp4')
81
82 # No prefer_free_formats => prefer mp4 and webm
83 ydl = YDL()
84 ydl.params['prefer_free_formats'] = False
85 formats = [
86 {'ext': 'webm', 'height': 720, 'url': TEST_URL},
87 {'ext': 'mp4', 'height': 720, 'url': TEST_URL},
88 {'ext': 'flv', 'height': 720, 'url': TEST_URL},
89 ]
90 info_dict['formats'] = formats
91 yie = YoutubeIE(ydl)
92 yie._sort_formats(info_dict['formats'])
93 ydl.process_ie_result(info_dict)
94 downloaded = ydl.downloaded_info_dicts[0]
95 self.assertEqual(downloaded['ext'], 'mp4')
96
97 ydl = YDL()
98 ydl.params['prefer_free_formats'] = False
99 formats = [
100 {'ext': 'flv', 'height': 720, 'url': TEST_URL},
101 {'ext': 'webm', 'height': 720, 'url': TEST_URL},
102 ]
103 info_dict['formats'] = formats
104 yie = YoutubeIE(ydl)
105 yie._sort_formats(info_dict['formats'])
106 ydl.process_ie_result(info_dict)
107 downloaded = ydl.downloaded_info_dicts[0]
108 self.assertEqual(downloaded['ext'], 'webm')
109
110 def test_format_selection(self):
111 formats = [
112 {'format_id': '35', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL},
113 {'format_id': 'example-with-dashes', 'ext': 'webm', 'preference': 1, 'url': TEST_URL},
114 {'format_id': '45', 'ext': 'webm', 'preference': 2, 'url': TEST_URL},
115 {'format_id': '47', 'ext': 'webm', 'preference': 3, 'url': TEST_URL},
116 {'format_id': '2', 'ext': 'flv', 'preference': 4, 'url': TEST_URL},
117 ]
118 info_dict = _make_result(formats)
119
120 ydl = YDL({'format': '20/47'})
121 ydl.process_ie_result(info_dict.copy())
122 downloaded = ydl.downloaded_info_dicts[0]
123 self.assertEqual(downloaded['format_id'], '47')
124
125 ydl = YDL({'format': '20/71/worst'})
126 ydl.process_ie_result(info_dict.copy())
127 downloaded = ydl.downloaded_info_dicts[0]
128 self.assertEqual(downloaded['format_id'], '35')
129
130 ydl = YDL()
131 ydl.process_ie_result(info_dict.copy())
132 downloaded = ydl.downloaded_info_dicts[0]
133 self.assertEqual(downloaded['format_id'], '2')
134
135 ydl = YDL({'format': 'webm/mp4'})
136 ydl.process_ie_result(info_dict.copy())
137 downloaded = ydl.downloaded_info_dicts[0]
138 self.assertEqual(downloaded['format_id'], '47')
139
140 ydl = YDL({'format': '3gp/40/mp4'})
141 ydl.process_ie_result(info_dict.copy())
142 downloaded = ydl.downloaded_info_dicts[0]
143 self.assertEqual(downloaded['format_id'], '35')
144
145 ydl = YDL({'format': 'example-with-dashes'})
146 ydl.process_ie_result(info_dict.copy())
147 downloaded = ydl.downloaded_info_dicts[0]
148 self.assertEqual(downloaded['format_id'], 'example-with-dashes')
149
150 def test_format_selection_audio(self):
151 formats = [
152 {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL},
153 {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none', 'url': TEST_URL},
154 {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none', 'url': TEST_URL},
155 {'format_id': 'vid', 'ext': 'mp4', 'preference': 4, 'url': TEST_URL},
156 ]
157 info_dict = _make_result(formats)
158
159 ydl = YDL({'format': 'bestaudio'})
160 ydl.process_ie_result(info_dict.copy())
161 downloaded = ydl.downloaded_info_dicts[0]
162 self.assertEqual(downloaded['format_id'], 'audio-high')
163
164 ydl = YDL({'format': 'worstaudio'})
165 ydl.process_ie_result(info_dict.copy())
166 downloaded = ydl.downloaded_info_dicts[0]
167 self.assertEqual(downloaded['format_id'], 'audio-low')
168
169 formats = [
170 {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL},
171 {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': TEST_URL},
172 ]
173 info_dict = _make_result(formats)
174
175 ydl = YDL({'format': 'bestaudio/worstaudio/best'})
176 ydl.process_ie_result(info_dict.copy())
177 downloaded = ydl.downloaded_info_dicts[0]
178 self.assertEqual(downloaded['format_id'], 'vid-high')
179
180 def test_format_selection_audio_exts(self):
181 formats = [
182 {'format_id': 'mp3-64', 'ext': 'mp3', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
183 {'format_id': 'ogg-64', 'ext': 'ogg', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
184 {'format_id': 'aac-64', 'ext': 'aac', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
185 {'format_id': 'mp3-32', 'ext': 'mp3', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'},
186 {'format_id': 'aac-32', 'ext': 'aac', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'},
187 ]
188
189 info_dict = _make_result(formats)
190 ydl = YDL({'format': 'best'})
191 ie = YoutubeIE(ydl)
192 ie._sort_formats(info_dict['formats'])
193 ydl.process_ie_result(copy.deepcopy(info_dict))
194 downloaded = ydl.downloaded_info_dicts[0]
195 self.assertEqual(downloaded['format_id'], 'aac-64')
196
197 ydl = YDL({'format': 'mp3'})
198 ie = YoutubeIE(ydl)
199 ie._sort_formats(info_dict['formats'])
200 ydl.process_ie_result(copy.deepcopy(info_dict))
201 downloaded = ydl.downloaded_info_dicts[0]
202 self.assertEqual(downloaded['format_id'], 'mp3-64')
203
204 ydl = YDL({'prefer_free_formats': True})
205 ie = YoutubeIE(ydl)
206 ie._sort_formats(info_dict['formats'])
207 ydl.process_ie_result(copy.deepcopy(info_dict))
208 downloaded = ydl.downloaded_info_dicts[0]
209 self.assertEqual(downloaded['format_id'], 'ogg-64')
210
211 def test_format_selection_video(self):
212 formats = [
213 {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none', 'url': TEST_URL},
214 {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none', 'url': TEST_URL},
215 {'format_id': 'vid', 'ext': 'mp4', 'preference': 3, 'url': TEST_URL},
216 ]
217 info_dict = _make_result(formats)
218
219 ydl = YDL({'format': 'bestvideo'})
220 ydl.process_ie_result(info_dict.copy())
221 downloaded = ydl.downloaded_info_dicts[0]
222 self.assertEqual(downloaded['format_id'], 'dash-video-high')
223
224 ydl = YDL({'format': 'worstvideo'})
225 ydl.process_ie_result(info_dict.copy())
226 downloaded = ydl.downloaded_info_dicts[0]
227 self.assertEqual(downloaded['format_id'], 'dash-video-low')
228
229 ydl = YDL({'format': 'bestvideo[format_id^=dash][format_id$=low]'})
230 ydl.process_ie_result(info_dict.copy())
231 downloaded = ydl.downloaded_info_dicts[0]
232 self.assertEqual(downloaded['format_id'], 'dash-video-low')
233
234 formats = [
235 {'format_id': 'vid-vcodec-dot', 'ext': 'mp4', 'preference': 1, 'vcodec': 'avc1.123456', 'acodec': 'none', 'url': TEST_URL},
236 ]
237 info_dict = _make_result(formats)
238
239 ydl = YDL({'format': 'bestvideo[vcodec=avc1.123456]'})
240 ydl.process_ie_result(info_dict.copy())
241 downloaded = ydl.downloaded_info_dicts[0]
242 self.assertEqual(downloaded['format_id'], 'vid-vcodec-dot')
243
244 def test_format_selection_string_ops(self):
245 formats = [
246 {'format_id': 'abc-cba', 'ext': 'mp4', 'url': TEST_URL},
247 {'format_id': 'zxc-cxz', 'ext': 'webm', 'url': TEST_URL},
248 ]
249 info_dict = _make_result(formats)
250
251 # equals (=)
252 ydl = YDL({'format': '[format_id=abc-cba]'})
253 ydl.process_ie_result(info_dict.copy())
254 downloaded = ydl.downloaded_info_dicts[0]
255 self.assertEqual(downloaded['format_id'], 'abc-cba')
256
257 # does not equal (!=)
258 ydl = YDL({'format': '[format_id!=abc-cba]'})
259 ydl.process_ie_result(info_dict.copy())
260 downloaded = ydl.downloaded_info_dicts[0]
261 self.assertEqual(downloaded['format_id'], 'zxc-cxz')
262
263 ydl = YDL({'format': '[format_id!=abc-cba][format_id!=zxc-cxz]'})
264 self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy())
265
266 # starts with (^=)
267 ydl = YDL({'format': '[format_id^=abc]'})
268 ydl.process_ie_result(info_dict.copy())
269 downloaded = ydl.downloaded_info_dicts[0]
270 self.assertEqual(downloaded['format_id'], 'abc-cba')
271
272 # does not start with (!^=)
273 ydl = YDL({'format': '[format_id!^=abc]'})
274 ydl.process_ie_result(info_dict.copy())
275 downloaded = ydl.downloaded_info_dicts[0]
276 self.assertEqual(downloaded['format_id'], 'zxc-cxz')
277
278 ydl = YDL({'format': '[format_id!^=abc][format_id!^=zxc]'})
279 self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy())
280
281 # ends with ($=)
282 ydl = YDL({'format': '[format_id$=cba]'})
283 ydl.process_ie_result(info_dict.copy())
284 downloaded = ydl.downloaded_info_dicts[0]
285 self.assertEqual(downloaded['format_id'], 'abc-cba')
286
287 # does not end with (!$=)
288 ydl = YDL({'format': '[format_id!$=cba]'})
289 ydl.process_ie_result(info_dict.copy())
290 downloaded = ydl.downloaded_info_dicts[0]
291 self.assertEqual(downloaded['format_id'], 'zxc-cxz')
292
293 ydl = YDL({'format': '[format_id!$=cba][format_id!$=cxz]'})
294 self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy())
295
296 # contains (*=)
297 ydl = YDL({'format': '[format_id*=bc-cb]'})
298 ydl.process_ie_result(info_dict.copy())
299 downloaded = ydl.downloaded_info_dicts[0]
300 self.assertEqual(downloaded['format_id'], 'abc-cba')
301
302 # does not contain (!*=)
303 ydl = YDL({'format': '[format_id!*=bc-cb]'})
304 ydl.process_ie_result(info_dict.copy())
305 downloaded = ydl.downloaded_info_dicts[0]
306 self.assertEqual(downloaded['format_id'], 'zxc-cxz')
307
308 ydl = YDL({'format': '[format_id!*=abc][format_id!*=zxc]'})
309 self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy())
310
311 ydl = YDL({'format': '[format_id!*=-]'})
312 self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy())
313
314 def test_youtube_format_selection(self):
315 # FIXME: Rewrite in accordance with the new format sorting options
316 return
317
318 order = [
319 '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '17', '36', '13',
320 # Apple HTTP Live Streaming
321 '96', '95', '94', '93', '92', '132', '151',
322 # 3D
323 '85', '84', '102', '83', '101', '82', '100',
324 # Dash video
325 '137', '248', '136', '247', '135', '246',
326 '245', '244', '134', '243', '133', '242', '160',
327 # Dash audio
328 '141', '172', '140', '171', '139',
329 ]
330
331 def format_info(f_id):
332 info = YoutubeIE._formats[f_id].copy()
333
334 # XXX: In real cases InfoExtractor._parse_mpd_formats() fills up 'acodec'
335 # and 'vcodec', while in tests such information is incomplete since
336 # commit a6c2c24479e5f4827ceb06f64d855329c0a6f593
337 # test_YoutubeDL.test_youtube_format_selection is broken without
338 # this fix
339 if 'acodec' in info and 'vcodec' not in info:
340 info['vcodec'] = 'none'
341 elif 'vcodec' in info and 'acodec' not in info:
342 info['acodec'] = 'none'
343
344 info['format_id'] = f_id
345 info['url'] = 'url:' + f_id
346 return info
347 formats_order = [format_info(f_id) for f_id in order]
348
349 info_dict = _make_result(list(formats_order), extractor='youtube')
350 ydl = YDL({'format': 'bestvideo+bestaudio'})
351 yie = YoutubeIE(ydl)
352 yie._sort_formats(info_dict['formats'])
353 ydl.process_ie_result(info_dict)
354 downloaded = ydl.downloaded_info_dicts[0]
355 self.assertEqual(downloaded['format_id'], '248+172')
356 self.assertEqual(downloaded['ext'], 'mp4')
357
358 info_dict = _make_result(list(formats_order), extractor='youtube')
359 ydl = YDL({'format': 'bestvideo[height>=999999]+bestaudio/best'})
360 yie = YoutubeIE(ydl)
361 yie._sort_formats(info_dict['formats'])
362 ydl.process_ie_result(info_dict)
363 downloaded = ydl.downloaded_info_dicts[0]
364 self.assertEqual(downloaded['format_id'], '38')
365
366 info_dict = _make_result(list(formats_order), extractor='youtube')
367 ydl = YDL({'format': 'bestvideo/best,bestaudio'})
368 yie = YoutubeIE(ydl)
369 yie._sort_formats(info_dict['formats'])
370 ydl.process_ie_result(info_dict)
371 downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
372 self.assertEqual(downloaded_ids, ['137', '141'])
373
374 info_dict = _make_result(list(formats_order), extractor='youtube')
375 ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])+bestaudio'})
376 yie = YoutubeIE(ydl)
377 yie._sort_formats(info_dict['formats'])
378 ydl.process_ie_result(info_dict)
379 downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
380 self.assertEqual(downloaded_ids, ['137+141', '248+141'])
381
382 info_dict = _make_result(list(formats_order), extractor='youtube')
383 ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])[height<=720]+bestaudio'})
384 yie = YoutubeIE(ydl)
385 yie._sort_formats(info_dict['formats'])
386 ydl.process_ie_result(info_dict)
387 downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
388 self.assertEqual(downloaded_ids, ['136+141', '247+141'])
389
390 info_dict = _make_result(list(formats_order), extractor='youtube')
391 ydl = YDL({'format': '(bestvideo[ext=none]/bestvideo[ext=webm])+bestaudio'})
392 yie = YoutubeIE(ydl)
393 yie._sort_formats(info_dict['formats'])
394 ydl.process_ie_result(info_dict)
395 downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
396 self.assertEqual(downloaded_ids, ['248+141'])
397
398 for f1, f2 in zip(formats_order, formats_order[1:]):
399 info_dict = _make_result([f1, f2], extractor='youtube')
400 ydl = YDL({'format': 'best/bestvideo'})
401 yie = YoutubeIE(ydl)
402 yie._sort_formats(info_dict['formats'])
403 ydl.process_ie_result(info_dict)
404 downloaded = ydl.downloaded_info_dicts[0]
405 self.assertEqual(downloaded['format_id'], f1['format_id'])
406
407 info_dict = _make_result([f2, f1], extractor='youtube')
408 ydl = YDL({'format': 'best/bestvideo'})
409 yie = YoutubeIE(ydl)
410 yie._sort_formats(info_dict['formats'])
411 ydl.process_ie_result(info_dict)
412 downloaded = ydl.downloaded_info_dicts[0]
413 self.assertEqual(downloaded['format_id'], f1['format_id'])
414
415 def test_audio_only_extractor_format_selection(self):
416 # For extractors with incomplete formats (all formats are audio-only or
417 # video-only) best and worst should fallback to corresponding best/worst
418 # video-only or audio-only formats (as per
419 # https://github.com/ytdl-org/youtube-dl/pull/5556)
420 formats = [
421 {'format_id': 'low', 'ext': 'mp3', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL},
422 {'format_id': 'high', 'ext': 'mp3', 'preference': 2, 'vcodec': 'none', 'url': TEST_URL},
423 ]
424 info_dict = _make_result(formats)
425
426 ydl = YDL({'format': 'best'})
427 ydl.process_ie_result(info_dict.copy())
428 downloaded = ydl.downloaded_info_dicts[0]
429 self.assertEqual(downloaded['format_id'], 'high')
430
431 ydl = YDL({'format': 'worst'})
432 ydl.process_ie_result(info_dict.copy())
433 downloaded = ydl.downloaded_info_dicts[0]
434 self.assertEqual(downloaded['format_id'], 'low')
435
436 def test_format_not_available(self):
437 formats = [
438 {'format_id': 'regular', 'ext': 'mp4', 'height': 360, 'url': TEST_URL},
439 {'format_id': 'video', 'ext': 'mp4', 'height': 720, 'acodec': 'none', 'url': TEST_URL},
440 ]
441 info_dict = _make_result(formats)
442
443 # This must fail since complete video-audio format does not match filter
444 # and extractor does not provide incomplete only formats (i.e. only
445 # video-only or audio-only).
446 ydl = YDL({'format': 'best[height>360]'})
447 self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy())
448
449 def test_format_selection_issue_10083(self):
450 # See https://github.com/ytdl-org/youtube-dl/issues/10083
451 formats = [
452 {'format_id': 'regular', 'height': 360, 'url': TEST_URL},
453 {'format_id': 'video', 'height': 720, 'acodec': 'none', 'url': TEST_URL},
454 {'format_id': 'audio', 'vcodec': 'none', 'url': TEST_URL},
455 ]
456 info_dict = _make_result(formats)
457
458 ydl = YDL({'format': 'best[height>360]/bestvideo[height>360]+bestaudio'})
459 ydl.process_ie_result(info_dict.copy())
460 self.assertEqual(ydl.downloaded_info_dicts[0]['format_id'], 'video+audio')
461
462 def test_invalid_format_specs(self):
463 def assert_syntax_error(format_spec):
464 ydl = YDL({'format': format_spec})
465 info_dict = _make_result([{'format_id': 'foo', 'url': TEST_URL}])
466 self.assertRaises(SyntaxError, ydl.process_ie_result, info_dict)
467
468 assert_syntax_error('bestvideo,,best')
469 assert_syntax_error('+bestaudio')
470 assert_syntax_error('bestvideo+')
471 assert_syntax_error('/')
472
473 def test_format_filtering(self):
474 formats = [
475 {'format_id': 'A', 'filesize': 500, 'width': 1000},
476 {'format_id': 'B', 'filesize': 1000, 'width': 500},
477 {'format_id': 'C', 'filesize': 1000, 'width': 400},
478 {'format_id': 'D', 'filesize': 2000, 'width': 600},
479 {'format_id': 'E', 'filesize': 3000},
480 {'format_id': 'F'},
481 {'format_id': 'G', 'filesize': 1000000},
482 ]
483 for f in formats:
484 f['url'] = 'http://_/'
485 f['ext'] = 'unknown'
486 info_dict = _make_result(formats)
487
488 ydl = YDL({'format': 'best[filesize<3000]'})
489 ydl.process_ie_result(info_dict)
490 downloaded = ydl.downloaded_info_dicts[0]
491 self.assertEqual(downloaded['format_id'], 'D')
492
493 ydl = YDL({'format': 'best[filesize<=3000]'})
494 ydl.process_ie_result(info_dict)
495 downloaded = ydl.downloaded_info_dicts[0]
496 self.assertEqual(downloaded['format_id'], 'E')
497
498 ydl = YDL({'format': 'best[filesize <= ? 3000]'})
499 ydl.process_ie_result(info_dict)
500 downloaded = ydl.downloaded_info_dicts[0]
501 self.assertEqual(downloaded['format_id'], 'F')
502
503 ydl = YDL({'format': 'best [filesize = 1000] [width>450]'})
504 ydl.process_ie_result(info_dict)
505 downloaded = ydl.downloaded_info_dicts[0]
506 self.assertEqual(downloaded['format_id'], 'B')
507
508 ydl = YDL({'format': 'best [filesize = 1000] [width!=450]'})
509 ydl.process_ie_result(info_dict)
510 downloaded = ydl.downloaded_info_dicts[0]
511 self.assertEqual(downloaded['format_id'], 'C')
512
513 ydl = YDL({'format': '[filesize>?1]'})
514 ydl.process_ie_result(info_dict)
515 downloaded = ydl.downloaded_info_dicts[0]
516 self.assertEqual(downloaded['format_id'], 'G')
517
518 ydl = YDL({'format': '[filesize<1M]'})
519 ydl.process_ie_result(info_dict)
520 downloaded = ydl.downloaded_info_dicts[0]
521 self.assertEqual(downloaded['format_id'], 'E')
522
523 ydl = YDL({'format': '[filesize<1MiB]'})
524 ydl.process_ie_result(info_dict)
525 downloaded = ydl.downloaded_info_dicts[0]
526 self.assertEqual(downloaded['format_id'], 'G')
527
528 ydl = YDL({'format': 'all[width>=400][width<=600]'})
529 ydl.process_ie_result(info_dict)
530 downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
531 self.assertEqual(downloaded_ids, ['B', 'C', 'D'])
532
533 ydl = YDL({'format': 'best[height<40]'})
534 try:
535 ydl.process_ie_result(info_dict)
536 except ExtractorError:
537 pass
538 self.assertEqual(ydl.downloaded_info_dicts, [])
539
540 def test_default_format_spec(self):
541 ydl = YDL({'simulate': True})
542 self.assertEqual(ydl._default_format_spec({}), 'bestvideo*+bestaudio/best')
543
544 ydl = YDL({})
545 self.assertEqual(ydl._default_format_spec({'is_live': True}), 'best/bestvideo+bestaudio')
546
547 ydl = YDL({'simulate': True})
548 self.assertEqual(ydl._default_format_spec({'is_live': True}), 'bestvideo*+bestaudio/best')
549
550 ydl = YDL({'outtmpl': '-'})
551 self.assertEqual(ydl._default_format_spec({}), 'best/bestvideo+bestaudio')
552
553 ydl = YDL({})
554 self.assertEqual(ydl._default_format_spec({}, download=False), 'bestvideo*+bestaudio/best')
555 self.assertEqual(ydl._default_format_spec({'is_live': True}), 'best/bestvideo+bestaudio')
556
557
558 class TestYoutubeDL(unittest.TestCase):
559 def test_subtitles(self):
560 def s_formats(lang, autocaption=False):
561 return [{
562 'ext': ext,
563 'url': 'http://localhost/video.%s.%s' % (lang, ext),
564 '_auto': autocaption,
565 } for ext in ['vtt', 'srt', 'ass']]
566 subtitles = dict((l, s_formats(l)) for l in ['en', 'fr', 'es'])
567 auto_captions = dict((l, s_formats(l, True)) for l in ['it', 'pt', 'es'])
568 info_dict = {
569 'id': 'test',
570 'title': 'Test',
571 'url': 'http://localhost/video.mp4',
572 'subtitles': subtitles,
573 'automatic_captions': auto_captions,
574 'extractor': 'TEST',
575 'webpage_url': 'http://example.com/watch?v=shenanigans',
576 }
577
578 def get_info(params={}):
579 params.setdefault('simulate', True)
580 ydl = YDL(params)
581 ydl.report_warning = lambda *args, **kargs: None
582 return ydl.process_video_result(info_dict, download=False)
583
584 result = get_info()
585 self.assertFalse(result.get('requested_subtitles'))
586 self.assertEqual(result['subtitles'], subtitles)
587 self.assertEqual(result['automatic_captions'], auto_captions)
588
589 result = get_info({'writesubtitles': True})
590 subs = result['requested_subtitles']
591 self.assertTrue(subs)
592 self.assertEqual(set(subs.keys()), set(['en']))
593 self.assertTrue(subs['en'].get('data') is None)
594 self.assertEqual(subs['en']['ext'], 'ass')
595
596 result = get_info({'writesubtitles': True, 'subtitlesformat': 'foo/srt'})
597 subs = result['requested_subtitles']
598 self.assertEqual(subs['en']['ext'], 'srt')
599
600 result = get_info({'writesubtitles': True, 'subtitleslangs': ['es', 'fr', 'it']})
601 subs = result['requested_subtitles']
602 self.assertTrue(subs)
603 self.assertEqual(set(subs.keys()), set(['es', 'fr']))
604
605 result = get_info({'writesubtitles': True, 'subtitleslangs': ['all', '-en']})
606 subs = result['requested_subtitles']
607 self.assertTrue(subs)
608 self.assertEqual(set(subs.keys()), set(['es', 'fr']))
609
610 result = get_info({'writesubtitles': True, 'subtitleslangs': ['en', 'fr', '-en']})
611 subs = result['requested_subtitles']
612 self.assertTrue(subs)
613 self.assertEqual(set(subs.keys()), set(['fr']))
614
615 result = get_info({'writesubtitles': True, 'subtitleslangs': ['-en', 'en']})
616 subs = result['requested_subtitles']
617 self.assertTrue(subs)
618 self.assertEqual(set(subs.keys()), set(['en']))
619
620 result = get_info({'writesubtitles': True, 'subtitleslangs': ['e.+']})
621 subs = result['requested_subtitles']
622 self.assertTrue(subs)
623 self.assertEqual(set(subs.keys()), set(['es', 'en']))
624
625 result = get_info({'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
626 subs = result['requested_subtitles']
627 self.assertTrue(subs)
628 self.assertEqual(set(subs.keys()), set(['es', 'pt']))
629 self.assertFalse(subs['es']['_auto'])
630 self.assertTrue(subs['pt']['_auto'])
631
632 result = get_info({'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
633 subs = result['requested_subtitles']
634 self.assertTrue(subs)
635 self.assertEqual(set(subs.keys()), set(['es', 'pt']))
636 self.assertTrue(subs['es']['_auto'])
637 self.assertTrue(subs['pt']['_auto'])
638
639 def test_add_extra_info(self):
640 test_dict = {
641 'extractor': 'Foo',
642 }
643 extra_info = {
644 'extractor': 'Bar',
645 'playlist': 'funny videos',
646 }
647 YDL.add_extra_info(test_dict, extra_info)
648 self.assertEqual(test_dict['extractor'], 'Foo')
649 self.assertEqual(test_dict['playlist'], 'funny videos')
650
651 def test_prepare_filename(self):
652 info = {
653 'id': '1234',
654 'ext': 'mp4',
655 'width': None,
656 'height': 1080,
657 'title1': '$PATH',
658 'title2': '%PATH%',
659 'timestamp': 1618488000,
660 'formats': [{'id': 'id1'}, {'id': 'id2'}]
661 }
662
663 def fname(templ, na_placeholder='NA'):
664 params = {'outtmpl': templ}
665 if na_placeholder != 'NA':
666 params['outtmpl_na_placeholder'] = na_placeholder
667 ydl = YoutubeDL(params)
668 return ydl.prepare_filename(info)
669 self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4')
670 self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4')
671 NA_TEST_OUTTMPL = '%(uploader_date)s-%(width)d-%(id)s.%(ext)s'
672 # Replace missing fields with 'NA' by default
673 self.assertEqual(fname(NA_TEST_OUTTMPL), 'NA-NA-1234.mp4')
674 # Or by provided placeholder
675 self.assertEqual(fname(NA_TEST_OUTTMPL, na_placeholder='none'), 'none-none-1234.mp4')
676 self.assertEqual(fname(NA_TEST_OUTTMPL, na_placeholder=''), '--1234.mp4')
677 self.assertEqual(fname('%(height)s.%(ext)s'), '1080.mp4')
678 self.assertEqual(fname('%(height)d.%(ext)s'), '1080.mp4')
679 self.assertEqual(fname('%(height)6d.%(ext)s'), ' 1080.mp4')
680 self.assertEqual(fname('%(height)-6d.%(ext)s'), '1080 .mp4')
681 self.assertEqual(fname('%(height)06d.%(ext)s'), '001080.mp4')
682 self.assertEqual(fname('%(height) 06d.%(ext)s'), ' 01080.mp4')
683 self.assertEqual(fname('%(height) 06d.%(ext)s'), ' 01080.mp4')
684 self.assertEqual(fname('%(height)0 6d.%(ext)s'), ' 01080.mp4')
685 self.assertEqual(fname('%(height)0 6d.%(ext)s'), ' 01080.mp4')
686 self.assertEqual(fname('%(height) 0 6d.%(ext)s'), ' 01080.mp4')
687 self.assertEqual(fname('%%'), '%')
688 self.assertEqual(fname('%%%%'), '%%')
689 self.assertEqual(fname('%%(height)06d.%(ext)s'), '%(height)06d.mp4')
690 self.assertEqual(fname('%(width)06d.%(ext)s'), 'NA.mp4')
691 self.assertEqual(fname('%(width)06d.%%(ext)s'), 'NA.%(ext)s')
692 self.assertEqual(fname('%%(width)06d.%(ext)s'), '%(width)06d.mp4')
693 self.assertEqual(fname('Hello %(title1)s'), 'Hello $PATH')
694 self.assertEqual(fname('Hello %(title2)s'), 'Hello %PATH%')
695 self.assertEqual(fname('%(timestamp+-1000>%H-%M-%S)s'), '11-43-20')
696 self.assertEqual(fname('%(id+1)05d'), '01235')
697 self.assertEqual(fname('%(width+100)05d'), 'NA')
698 self.assertEqual(fname('%(formats.0)s').replace("u", ""), "{'id' - 'id1'}")
699 self.assertEqual(fname('%(formats.-1.id)s'), 'id2')
700 self.assertEqual(fname('%(formats.2)s'), 'NA')
701
702 def test_format_note(self):
703 ydl = YoutubeDL()
704 self.assertEqual(ydl._format_note({}), '')
705 assertRegexpMatches(self, ydl._format_note({
706 'vbr': 10,
707 }), r'^\s*10k$')
708 assertRegexpMatches(self, ydl._format_note({
709 'fps': 30,
710 }), r'^30fps$')
711
712 def test_postprocessors(self):
713 filename = 'post-processor-testfile.mp4'
714 audiofile = filename + '.mp3'
715
716 class SimplePP(PostProcessor):
717 def run(self, info):
718 with open(audiofile, 'wt') as f:
719 f.write('EXAMPLE')
720 return [info['filepath']], info
721
722 def run_pp(params, PP):
723 with open(filename, 'wt') as f:
724 f.write('EXAMPLE')
725 ydl = YoutubeDL(params)
726 ydl.add_post_processor(PP())
727 ydl.post_process(filename, {'filepath': filename})
728
729 run_pp({'keepvideo': True}, SimplePP)
730 self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)
731 self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
732 os.unlink(filename)
733 os.unlink(audiofile)
734
735 run_pp({'keepvideo': False}, SimplePP)
736 self.assertFalse(os.path.exists(filename), '%s exists' % filename)
737 self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
738 os.unlink(audiofile)
739
740 class ModifierPP(PostProcessor):
741 def run(self, info):
742 with open(info['filepath'], 'wt') as f:
743 f.write('MODIFIED')
744 return [], info
745
746 run_pp({'keepvideo': False}, ModifierPP)
747 self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)
748 os.unlink(filename)
749
750 def test_match_filter(self):
751 class FilterYDL(YDL):
752 def __init__(self, *args, **kwargs):
753 super(FilterYDL, self).__init__(*args, **kwargs)
754 self.params['simulate'] = True
755
756 def process_info(self, info_dict):
757 super(YDL, self).process_info(info_dict)
758
759 def _match_entry(self, info_dict, incomplete=False):
760 res = super(FilterYDL, self)._match_entry(info_dict, incomplete)
761 if res is None:
762 self.downloaded_info_dicts.append(info_dict)
763 return res
764
765 first = {
766 'id': '1',
767 'url': TEST_URL,
768 'title': 'one',
769 'extractor': 'TEST',
770 'duration': 30,
771 'filesize': 10 * 1024,
772 'playlist_id': '42',
773 'uploader': "่ฎŠๆ…‹ๅฆๅญ—ๅน•็‰ˆ ๅคชๅฆ ั‚ะตัั‚",
774 'creator': "ั‚ะตัั‚ ' 123 ' ั‚ะตัั‚--",
775 'webpage_url': 'http://example.com/watch?v=shenanigans',
776 }
777 second = {
778 'id': '2',
779 'url': TEST_URL,
780 'title': 'two',
781 'extractor': 'TEST',
782 'duration': 10,
783 'description': 'foo',
784 'filesize': 5 * 1024,
785 'playlist_id': '43',
786 'uploader': "ั‚ะตัั‚ 123",
787 'webpage_url': 'http://example.com/watch?v=SHENANIGANS',
788 }
789 videos = [first, second]
790
791 def get_videos(filter_=None):
792 ydl = FilterYDL({'match_filter': filter_})
793 for v in videos:
794 ydl.process_ie_result(v, download=True)
795 return [v['id'] for v in ydl.downloaded_info_dicts]
796
797 res = get_videos()
798 self.assertEqual(res, ['1', '2'])
799
800 def f(v):
801 if v['id'] == '1':
802 return None
803 else:
804 return 'Video id is not 1'
805 res = get_videos(f)
806 self.assertEqual(res, ['1'])
807
808 f = match_filter_func('duration < 30')
809 res = get_videos(f)
810 self.assertEqual(res, ['2'])
811
812 f = match_filter_func('description = foo')
813 res = get_videos(f)
814 self.assertEqual(res, ['2'])
815
816 f = match_filter_func('description =? foo')
817 res = get_videos(f)
818 self.assertEqual(res, ['1', '2'])
819
820 f = match_filter_func('filesize > 5KiB')
821 res = get_videos(f)
822 self.assertEqual(res, ['1'])
823
824 f = match_filter_func('playlist_id = 42')
825 res = get_videos(f)
826 self.assertEqual(res, ['1'])
827
828 f = match_filter_func('uploader = "่ฎŠๆ…‹ๅฆๅญ—ๅน•็‰ˆ ๅคชๅฆ ั‚ะตัั‚"')
829 res = get_videos(f)
830 self.assertEqual(res, ['1'])
831
832 f = match_filter_func('uploader != "่ฎŠๆ…‹ๅฆๅญ—ๅน•็‰ˆ ๅคชๅฆ ั‚ะตัั‚"')
833 res = get_videos(f)
834 self.assertEqual(res, ['2'])
835
836 f = match_filter_func('creator = "ั‚ะตัั‚ \' 123 \' ั‚ะตัั‚--"')
837 res = get_videos(f)
838 self.assertEqual(res, ['1'])
839
840 f = match_filter_func("creator = 'ั‚ะตัั‚ \\' 123 \\' ั‚ะตัั‚--'")
841 res = get_videos(f)
842 self.assertEqual(res, ['1'])
843
844 f = match_filter_func(r"creator = 'ั‚ะตัั‚ \' 123 \' ั‚ะตัั‚--' & duration > 30")
845 res = get_videos(f)
846 self.assertEqual(res, [])
847
848 def test_playlist_items_selection(self):
849 entries = [{
850 'id': compat_str(i),
851 'title': compat_str(i),
852 'url': TEST_URL,
853 } for i in range(1, 5)]
854 playlist = {
855 '_type': 'playlist',
856 'id': 'test',
857 'entries': entries,
858 'extractor': 'test:playlist',
859 'extractor_key': 'test:playlist',
860 'webpage_url': 'http://example.com',
861 }
862
863 def get_downloaded_info_dicts(params):
864 ydl = YDL(params)
865 # make a deep copy because the dictionary and nested entries
866 # can be modified
867 ydl.process_ie_result(copy.deepcopy(playlist))
868 return ydl.downloaded_info_dicts
869
870 def get_ids(params):
871 return [int(v['id']) for v in get_downloaded_info_dicts(params)]
872
873 result = get_ids({})
874 self.assertEqual(result, [1, 2, 3, 4])
875
876 result = get_ids({'playlistend': 10})
877 self.assertEqual(result, [1, 2, 3, 4])
878
879 result = get_ids({'playlistend': 2})
880 self.assertEqual(result, [1, 2])
881
882 result = get_ids({'playliststart': 10})
883 self.assertEqual(result, [])
884
885 result = get_ids({'playliststart': 2})
886 self.assertEqual(result, [2, 3, 4])
887
888 result = get_ids({'playlist_items': '2-4'})
889 self.assertEqual(result, [2, 3, 4])
890
891 result = get_ids({'playlist_items': '2,4'})
892 self.assertEqual(result, [2, 4])
893
894 result = get_ids({'playlist_items': '10'})
895 self.assertEqual(result, [])
896
897 result = get_ids({'playlist_items': '3-10'})
898 self.assertEqual(result, [3, 4])
899
900 result = get_ids({'playlist_items': '2-4,3-4,3'})
901 self.assertEqual(result, [2, 3, 4])
902
903 # Tests for https://github.com/ytdl-org/youtube-dl/issues/10591
904 # @{
905 result = get_downloaded_info_dicts({'playlist_items': '2-4,3-4,3'})
906 self.assertEqual(result[0]['playlist_index'], 2)
907 self.assertEqual(result[1]['playlist_index'], 3)
908
909 result = get_downloaded_info_dicts({'playlist_items': '2-4,3-4,3'})
910 self.assertEqual(result[0]['playlist_index'], 2)
911 self.assertEqual(result[1]['playlist_index'], 3)
912 self.assertEqual(result[2]['playlist_index'], 4)
913
914 result = get_downloaded_info_dicts({'playlist_items': '4,2'})
915 self.assertEqual(result[0]['playlist_index'], 4)
916 self.assertEqual(result[1]['playlist_index'], 2)
917 # @}
918
919 def test_urlopen_no_file_protocol(self):
920 # see https://github.com/ytdl-org/youtube-dl/issues/8227
921 ydl = YDL()
922 self.assertRaises(compat_urllib_error.URLError, ydl.urlopen, 'file:///etc/passwd')
923
924 def test_do_not_override_ie_key_in_url_transparent(self):
925 ydl = YDL()
926
927 class Foo1IE(InfoExtractor):
928 _VALID_URL = r'foo1:'
929
930 def _real_extract(self, url):
931 return {
932 '_type': 'url_transparent',
933 'url': 'foo2:',
934 'ie_key': 'Foo2',
935 'title': 'foo1 title',
936 'id': 'foo1_id',
937 }
938
939 class Foo2IE(InfoExtractor):
940 _VALID_URL = r'foo2:'
941
942 def _real_extract(self, url):
943 return {
944 '_type': 'url',
945 'url': 'foo3:',
946 'ie_key': 'Foo3',
947 }
948
949 class Foo3IE(InfoExtractor):
950 _VALID_URL = r'foo3:'
951
952 def _real_extract(self, url):
953 return _make_result([{'url': TEST_URL}], title='foo3 title')
954
955 ydl.add_info_extractor(Foo1IE(ydl))
956 ydl.add_info_extractor(Foo2IE(ydl))
957 ydl.add_info_extractor(Foo3IE(ydl))
958 ydl.extract_info('foo1:')
959 downloaded = ydl.downloaded_info_dicts[0]
960 self.assertEqual(downloaded['url'], TEST_URL)
961 self.assertEqual(downloaded['title'], 'foo1 title')
962 self.assertEqual(downloaded['id'], 'testid')
963 self.assertEqual(downloaded['extractor'], 'testex')
964 self.assertEqual(downloaded['extractor_key'], 'TestEx')
965
966 # Test case for https://github.com/ytdl-org/youtube-dl/issues/27064
967 def test_ignoreerrors_for_playlist_with_url_transparent_iterable_entries(self):
968
969 class _YDL(YDL):
970 def __init__(self, *args, **kwargs):
971 super(_YDL, self).__init__(*args, **kwargs)
972
973 def trouble(self, s, tb=None):
974 pass
975
976 ydl = _YDL({
977 'format': 'extra',
978 'ignoreerrors': True,
979 })
980
981 class VideoIE(InfoExtractor):
982 _VALID_URL = r'video:(?P<id>\d+)'
983
984 def _real_extract(self, url):
985 video_id = self._match_id(url)
986 formats = [{
987 'format_id': 'default',
988 'url': 'url:',
989 }]
990 if video_id == '0':
991 raise ExtractorError('foo')
992 if video_id == '2':
993 formats.append({
994 'format_id': 'extra',
995 'url': TEST_URL,
996 })
997 return {
998 'id': video_id,
999 'title': 'Video %s' % video_id,
1000 'formats': formats,
1001 }
1002
1003 class PlaylistIE(InfoExtractor):
1004 _VALID_URL = r'playlist:'
1005
1006 def _entries(self):
1007 for n in range(3):
1008 video_id = compat_str(n)
1009 yield {
1010 '_type': 'url_transparent',
1011 'ie_key': VideoIE.ie_key(),
1012 'id': video_id,
1013 'url': 'video:%s' % video_id,
1014 'title': 'Video Transparent %s' % video_id,
1015 }
1016
1017 def _real_extract(self, url):
1018 return self.playlist_result(self._entries())
1019
1020 ydl.add_info_extractor(VideoIE(ydl))
1021 ydl.add_info_extractor(PlaylistIE(ydl))
1022 info = ydl.extract_info('playlist:')
1023 entries = info['entries']
1024 self.assertEqual(len(entries), 3)
1025 self.assertTrue(entries[0] is None)
1026 self.assertTrue(entries[1] is None)
1027 self.assertEqual(len(ydl.downloaded_info_dicts), 1)
1028 downloaded = ydl.downloaded_info_dicts[0]
1029 self.assertEqual(entries[2], downloaded)
1030 self.assertEqual(downloaded['url'], TEST_URL)
1031 self.assertEqual(downloaded['title'], 'Video Transparent 2')
1032 self.assertEqual(downloaded['id'], '2')
1033 self.assertEqual(downloaded['extractor'], 'Video')
1034 self.assertEqual(downloaded['extractor_key'], 'Video')
1035
1036
1037 if __name__ == '__main__':
1038 unittest.main()