]> jfr.im git - yt-dlp.git/blame - test/test_YoutubeDL.py
Merge pull request #8354 from remitamine/m3u8_metadata
[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
e37afbe0 15from youtube_dl.compat import compat_str, compat_urllib_error
3d4a70b8 16from youtube_dl.extractor import YoutubeIE
b286f201 17from youtube_dl.extractor.common import InfoExtractor
2b4ecde2 18from youtube_dl.postprocessor.common import PostProcessor
bb8e5536 19from youtube_dl.utils import ExtractorError, match_filter_func
e028d0d1 20
8508557e
JMF
21TEST_URL = 'http://localhost/sample.mp4'
22
e028d0d1
JMF
23
24class YDL(FakeYDL):
f4d96df0
PH
25 def __init__(self, *args, **kwargs):
26 super(YDL, self).__init__(*args, **kwargs)
e028d0d1 27 self.downloaded_info_dicts = []
f4d96df0 28 self.msgs = []
5d254f77 29
e028d0d1
JMF
30 def process_info(self, info_dict):
31 self.downloaded_info_dicts.append(info_dict)
32
f4d96df0
PH
33 def to_screen(self, msg):
34 self.msgs.append(msg)
35
5d254f77 36
3537b93d
PH
37def _make_result(formats, **kwargs):
38 res = {
39 'formats': formats,
40 'id': 'testid',
41 'title': 'testttitle',
42 'extractor': 'testex',
43 }
44 res.update(**kwargs)
45 return res
46
47
e028d0d1
JMF
48class TestFormatSelection(unittest.TestCase):
49 def test_prefer_free_formats(self):
50 # Same resolution => download webm
51 ydl = YDL()
52 ydl.params['prefer_free_formats'] = True
5d254f77 53 formats = [
8508557e
JMF
54 {'ext': 'webm', 'height': 460, 'url': TEST_URL},
55 {'ext': 'mp4', 'height': 460, 'url': TEST_URL},
5d254f77 56 ]
3537b93d 57 info_dict = _make_result(formats)
3d4a70b8
PH
58 yie = YoutubeIE(ydl)
59 yie._sort_formats(info_dict['formats'])
e028d0d1
JMF
60 ydl.process_ie_result(info_dict)
61 downloaded = ydl.downloaded_info_dicts[0]
89087418 62 self.assertEqual(downloaded['ext'], 'webm')
e028d0d1
JMF
63
64 # Different resolution => download best quality (mp4)
65 ydl = YDL()
66 ydl.params['prefer_free_formats'] = True
5d254f77 67 formats = [
8508557e
JMF
68 {'ext': 'webm', 'height': 720, 'url': TEST_URL},
69 {'ext': 'mp4', 'height': 1080, 'url': TEST_URL},
5d254f77 70 ]
89087418 71 info_dict['formats'] = formats
3d4a70b8
PH
72 yie = YoutubeIE(ydl)
73 yie._sort_formats(info_dict['formats'])
e028d0d1
JMF
74 ydl.process_ie_result(info_dict)
75 downloaded = ydl.downloaded_info_dicts[0]
89087418 76 self.assertEqual(downloaded['ext'], 'mp4')
e028d0d1 77
1c783bca 78 # No prefer_free_formats => prefer mp4 and flv for greater compatibility
e028d0d1
JMF
79 ydl = YDL()
80 ydl.params['prefer_free_formats'] = False
5d254f77 81 formats = [
8508557e
JMF
82 {'ext': 'webm', 'height': 720, 'url': TEST_URL},
83 {'ext': 'mp4', 'height': 720, 'url': TEST_URL},
84 {'ext': 'flv', 'height': 720, 'url': TEST_URL},
5d254f77 85 ]
89087418 86 info_dict['formats'] = formats
3d4a70b8
PH
87 yie = YoutubeIE(ydl)
88 yie._sort_formats(info_dict['formats'])
89 ydl.process_ie_result(info_dict)
90 downloaded = ydl.downloaded_info_dicts[0]
89087418 91 self.assertEqual(downloaded['ext'], 'mp4')
3d4a70b8
PH
92
93 ydl = YDL()
94 ydl.params['prefer_free_formats'] = False
95 formats = [
8508557e
JMF
96 {'ext': 'flv', 'height': 720, 'url': TEST_URL},
97 {'ext': 'webm', 'height': 720, 'url': TEST_URL},
3d4a70b8 98 ]
89087418 99 info_dict['formats'] = formats
3d4a70b8
PH
100 yie = YoutubeIE(ydl)
101 yie._sort_formats(info_dict['formats'])
e028d0d1
JMF
102 ydl.process_ie_result(info_dict)
103 downloaded = ydl.downloaded_info_dicts[0]
89087418 104 self.assertEqual(downloaded['ext'], 'flv')
e028d0d1 105
a9c58ad9
JMF
106 def test_format_selection(self):
107 formats = [
8508557e 108 {'format_id': '35', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL},
232541df 109 {'format_id': 'example-with-dashes', 'ext': 'webm', 'preference': 1, 'url': TEST_URL},
8508557e
JMF
110 {'format_id': '45', 'ext': 'webm', 'preference': 2, 'url': TEST_URL},
111 {'format_id': '47', 'ext': 'webm', 'preference': 3, 'url': TEST_URL},
112 {'format_id': '2', 'ext': 'flv', 'preference': 4, 'url': TEST_URL},
a9c58ad9 113 ]
3537b93d 114 info_dict = _make_result(formats)
a9c58ad9 115
89087418 116 ydl = YDL({'format': '20/47'})
8e3e0322 117 ydl.process_ie_result(info_dict.copy())
a9c58ad9 118 downloaded = ydl.downloaded_info_dicts[0]
89087418 119 self.assertEqual(downloaded['format_id'], '47')
a9c58ad9 120
89087418 121 ydl = YDL({'format': '20/71/worst'})
8e3e0322 122 ydl.process_ie_result(info_dict.copy())
a9c58ad9 123 downloaded = ydl.downloaded_info_dicts[0]
89087418 124 self.assertEqual(downloaded['format_id'], '35')
a9c58ad9
JMF
125
126 ydl = YDL()
8e3e0322 127 ydl.process_ie_result(info_dict.copy())
a9c58ad9 128 downloaded = ydl.downloaded_info_dicts[0]
89087418 129 self.assertEqual(downloaded['format_id'], '2')
a9c58ad9 130
89087418 131 ydl = YDL({'format': 'webm/mp4'})
8e3e0322 132 ydl.process_ie_result(info_dict.copy())
49e86983 133 downloaded = ydl.downloaded_info_dicts[0]
89087418 134 self.assertEqual(downloaded['format_id'], '47')
49e86983 135
89087418 136 ydl = YDL({'format': '3gp/40/mp4'})
8e3e0322 137 ydl.process_ie_result(info_dict.copy())
49e86983 138 downloaded = ydl.downloaded_info_dicts[0]
89087418 139 self.assertEqual(downloaded['format_id'], '35')
49e86983 140
232541df
JMF
141 ydl = YDL({'format': 'example-with-dashes'})
142 ydl.process_ie_result(info_dict.copy())
143 downloaded = ydl.downloaded_info_dicts[0]
144 self.assertEqual(downloaded['format_id'], 'example-with-dashes')
145
ba7678f9
PH
146 def test_format_selection_audio(self):
147 formats = [
8508557e
JMF
148 {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL},
149 {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none', 'url': TEST_URL},
150 {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none', 'url': TEST_URL},
151 {'format_id': 'vid', 'ext': 'mp4', 'preference': 4, 'url': TEST_URL},
ba7678f9 152 ]
3537b93d 153 info_dict = _make_result(formats)
ba7678f9 154
89087418 155 ydl = YDL({'format': 'bestaudio'})
ba7678f9
PH
156 ydl.process_ie_result(info_dict.copy())
157 downloaded = ydl.downloaded_info_dicts[0]
89087418 158 self.assertEqual(downloaded['format_id'], 'audio-high')
ba7678f9 159
89087418 160 ydl = YDL({'format': 'worstaudio'})
ba7678f9
PH
161 ydl.process_ie_result(info_dict.copy())
162 downloaded = ydl.downloaded_info_dicts[0]
89087418 163 self.assertEqual(downloaded['format_id'], 'audio-low')
ba7678f9
PH
164
165 formats = [
8508557e
JMF
166 {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL},
167 {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': TEST_URL},
ba7678f9 168 ]
3537b93d 169 info_dict = _make_result(formats)
ba7678f9 170
89087418 171 ydl = YDL({'format': 'bestaudio/worstaudio/best'})
ba7678f9
PH
172 ydl.process_ie_result(info_dict.copy())
173 downloaded = ydl.downloaded_info_dicts[0]
89087418 174 self.assertEqual(downloaded['format_id'], 'vid-high')
ba7678f9 175
0217c783
PH
176 def test_format_selection_audio_exts(self):
177 formats = [
178 {'format_id': 'mp3-64', 'ext': 'mp3', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
179 {'format_id': 'ogg-64', 'ext': 'ogg', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
180 {'format_id': 'aac-64', 'ext': 'aac', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
181 {'format_id': 'mp3-32', 'ext': 'mp3', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'},
182 {'format_id': 'aac-32', 'ext': 'aac', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'},
183 ]
184
185 info_dict = _make_result(formats)
186 ydl = YDL({'format': 'best'})
187 ie = YoutubeIE(ydl)
188 ie._sort_formats(info_dict['formats'])
189 ydl.process_ie_result(copy.deepcopy(info_dict))
190 downloaded = ydl.downloaded_info_dicts[0]
191 self.assertEqual(downloaded['format_id'], 'aac-64')
192
193 ydl = YDL({'format': 'mp3'})
194 ie = YoutubeIE(ydl)
195 ie._sort_formats(info_dict['formats'])
196 ydl.process_ie_result(copy.deepcopy(info_dict))
197 downloaded = ydl.downloaded_info_dicts[0]
198 self.assertEqual(downloaded['format_id'], 'mp3-64')
199
200 ydl = YDL({'prefer_free_formats': True})
201 ie = YoutubeIE(ydl)
202 ie._sort_formats(info_dict['formats'])
203 ydl.process_ie_result(copy.deepcopy(info_dict))
204 downloaded = ydl.downloaded_info_dicts[0]
205 self.assertEqual(downloaded['format_id'], 'ogg-64')
206
bc6d5978
JMF
207 def test_format_selection_video(self):
208 formats = [
8508557e
JMF
209 {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none', 'url': TEST_URL},
210 {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none', 'url': TEST_URL},
211 {'format_id': 'vid', 'ext': 'mp4', 'preference': 3, 'url': TEST_URL},
bc6d5978 212 ]
3537b93d 213 info_dict = _make_result(formats)
bc6d5978
JMF
214
215 ydl = YDL({'format': 'bestvideo'})
216 ydl.process_ie_result(info_dict.copy())
217 downloaded = ydl.downloaded_info_dicts[0]
218 self.assertEqual(downloaded['format_id'], 'dash-video-high')
219
220 ydl = YDL({'format': 'worstvideo'})
221 ydl.process_ie_result(info_dict.copy())
222 downloaded = ydl.downloaded_info_dicts[0]
223 self.assertEqual(downloaded['format_id'], 'dash-video-low')
224
b913348d 225 formats = [
226 {'format_id': 'vid-vcodec-dot', 'ext': 'mp4', 'preference': 1, 'vcodec': 'avc1.123456', 'acodec': 'none', 'url': TEST_URL},
227 ]
228 info_dict = _make_result(formats)
229
230 ydl = YDL({'format': 'bestvideo[vcodec=avc1.123456]'})
231 ydl.process_ie_result(info_dict.copy())
232 downloaded = ydl.downloaded_info_dicts[0]
233 self.assertEqual(downloaded['format_id'], 'vid-vcodec-dot')
234
3d4a70b8
PH
235 def test_youtube_format_selection(self):
236 order = [
237 '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '36', '17', '13',
238 # Apple HTTP Live Streaming
239 '96', '95', '94', '93', '92', '132', '151',
240 # 3D
241 '85', '84', '102', '83', '101', '82', '100',
242 # Dash video
c11125f9 243 '137', '248', '136', '247', '135', '246',
3d4a70b8
PH
244 '245', '244', '134', '243', '133', '242', '160',
245 # Dash audio
a053c349 246 '141', '172', '140', '171', '139',
3d4a70b8
PH
247 ]
248
67134eab
JMF
249 def format_info(f_id):
250 info = YoutubeIE._formats[f_id].copy()
251 info['format_id'] = f_id
252 info['url'] = 'url:' + f_id
253 return info
254 formats_order = [format_info(f_id) for f_id in order]
255
256 info_dict = _make_result(list(formats_order), extractor='youtube')
257 ydl = YDL({'format': 'bestvideo+bestaudio'})
258 yie = YoutubeIE(ydl)
259 yie._sort_formats(info_dict['formats'])
260 ydl.process_ie_result(info_dict)
261 downloaded = ydl.downloaded_info_dicts[0]
262 self.assertEqual(downloaded['format_id'], '137+141')
263 self.assertEqual(downloaded['ext'], 'mp4')
3d4a70b8 264
cf2ac6df
JMF
265 info_dict = _make_result(list(formats_order), extractor='youtube')
266 ydl = YDL({'format': 'bestvideo[height>=999999]+bestaudio/best'})
267 yie = YoutubeIE(ydl)
268 yie._sort_formats(info_dict['formats'])
269 ydl.process_ie_result(info_dict)
270 downloaded = ydl.downloaded_info_dicts[0]
271 self.assertEqual(downloaded['format_id'], '38')
272
f5f4a27a
JMF
273 info_dict = _make_result(list(formats_order), extractor='youtube')
274 ydl = YDL({'format': 'bestvideo/best,bestaudio'})
275 yie = YoutubeIE(ydl)
276 yie._sort_formats(info_dict['formats'])
277 ydl.process_ie_result(info_dict)
278 downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
279 self.assertEqual(downloaded_ids, ['137', '141'])
280
0130afb7
JMF
281 info_dict = _make_result(list(formats_order), extractor='youtube')
282 ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])+bestaudio'})
283 yie = YoutubeIE(ydl)
284 yie._sort_formats(info_dict['formats'])
285 ydl.process_ie_result(info_dict)
286 downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
287 self.assertEqual(downloaded_ids, ['137+141', '248+141'])
288
289 info_dict = _make_result(list(formats_order), extractor='youtube')
290 ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])[height<=720]+bestaudio'})
291 yie = YoutubeIE(ydl)
292 yie._sort_formats(info_dict['formats'])
293 ydl.process_ie_result(info_dict)
294 downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
295 self.assertEqual(downloaded_ids, ['136+141', '247+141'])
296
297 info_dict = _make_result(list(formats_order), extractor='youtube')
298 ydl = YDL({'format': '(bestvideo[ext=none]/bestvideo[ext=webm])+bestaudio'})
299 yie = YoutubeIE(ydl)
300 yie._sort_formats(info_dict['formats'])
301 ydl.process_ie_result(info_dict)
302 downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
303 self.assertEqual(downloaded_ids, ['248+141'])
304
67134eab 305 for f1, f2 in zip(formats_order, formats_order[1:]):
3537b93d 306 info_dict = _make_result([f1, f2], extractor='youtube')
8dd54188 307 ydl = YDL({'format': 'best/bestvideo'})
3d4a70b8
PH
308 yie = YoutubeIE(ydl)
309 yie._sort_formats(info_dict['formats'])
310 ydl.process_ie_result(info_dict)
311 downloaded = ydl.downloaded_info_dicts[0]
67134eab 312 self.assertEqual(downloaded['format_id'], f1['format_id'])
3d4a70b8 313
3537b93d 314 info_dict = _make_result([f2, f1], extractor='youtube')
8dd54188 315 ydl = YDL({'format': 'best/bestvideo'})
3d4a70b8
PH
316 yie = YoutubeIE(ydl)
317 yie._sort_formats(info_dict['formats'])
318 ydl.process_ie_result(info_dict)
319 downloaded = ydl.downloaded_info_dicts[0]
67134eab 320 self.assertEqual(downloaded['format_id'], f1['format_id'])
3d4a70b8 321
0a31a350
JMF
322 def test_invalid_format_specs(self):
323 def assert_syntax_error(format_spec):
324 ydl = YDL({'format': format_spec})
325 info_dict = _make_result([{'format_id': 'foo', 'url': TEST_URL}])
326 self.assertRaises(SyntaxError, ydl.process_ie_result, info_dict)
327
328 assert_syntax_error('bestvideo,,best')
329 assert_syntax_error('+bestaudio')
330 assert_syntax_error('bestvideo+')
d96d604e 331 assert_syntax_error('/')
0a31a350 332
083c9df9
PH
333 def test_format_filtering(self):
334 formats = [
335 {'format_id': 'A', 'filesize': 500, 'width': 1000},
336 {'format_id': 'B', 'filesize': 1000, 'width': 500},
337 {'format_id': 'C', 'filesize': 1000, 'width': 400},
338 {'format_id': 'D', 'filesize': 2000, 'width': 600},
339 {'format_id': 'E', 'filesize': 3000},
340 {'format_id': 'F'},
341 {'format_id': 'G', 'filesize': 1000000},
342 ]
343 for f in formats:
344 f['url'] = 'http://_/'
345 f['ext'] = 'unknown'
346 info_dict = _make_result(formats)
347
348 ydl = YDL({'format': 'best[filesize<3000]'})
349 ydl.process_ie_result(info_dict)
350 downloaded = ydl.downloaded_info_dicts[0]
351 self.assertEqual(downloaded['format_id'], 'D')
352
353 ydl = YDL({'format': 'best[filesize<=3000]'})
354 ydl.process_ie_result(info_dict)
355 downloaded = ydl.downloaded_info_dicts[0]
356 self.assertEqual(downloaded['format_id'], 'E')
357
358 ydl = YDL({'format': 'best[filesize <= ? 3000]'})
359 ydl.process_ie_result(info_dict)
360 downloaded = ydl.downloaded_info_dicts[0]
361 self.assertEqual(downloaded['format_id'], 'F')
362
363 ydl = YDL({'format': 'best [filesize = 1000] [width>450]'})
364 ydl.process_ie_result(info_dict)
365 downloaded = ydl.downloaded_info_dicts[0]
366 self.assertEqual(downloaded['format_id'], 'B')
367
368 ydl = YDL({'format': 'best [filesize = 1000] [width!=450]'})
369 ydl.process_ie_result(info_dict)
370 downloaded = ydl.downloaded_info_dicts[0]
371 self.assertEqual(downloaded['format_id'], 'C')
372
373 ydl = YDL({'format': '[filesize>?1]'})
374 ydl.process_ie_result(info_dict)
375 downloaded = ydl.downloaded_info_dicts[0]
376 self.assertEqual(downloaded['format_id'], 'G')
377
378 ydl = YDL({'format': '[filesize<1M]'})
379 ydl.process_ie_result(info_dict)
380 downloaded = ydl.downloaded_info_dicts[0]
381 self.assertEqual(downloaded['format_id'], 'E')
382
383 ydl = YDL({'format': '[filesize<1MiB]'})
384 ydl.process_ie_result(info_dict)
385 downloaded = ydl.downloaded_info_dicts[0]
386 self.assertEqual(downloaded['format_id'], 'G')
387
5acfa126
JMF
388 ydl = YDL({'format': 'all[width>=400][width<=600]'})
389 ydl.process_ie_result(info_dict)
390 downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
391 self.assertEqual(downloaded_ids, ['B', 'C', 'D'])
392
bb8e5536
JMF
393 ydl = YDL({'format': 'best[height<40]'})
394 try:
395 ydl.process_ie_result(info_dict)
396 except ExtractorError:
397 pass
398 self.assertEqual(ydl.downloaded_info_dicts, [])
399
f20bf146
JMF
400
401class TestYoutubeDL(unittest.TestCase):
ab84349b
JMF
402 def test_subtitles(self):
403 def s_formats(lang, autocaption=False):
404 return [{
405 'ext': ext,
406 'url': 'http://localhost/video.%s.%s' % (lang, ext),
407 '_auto': autocaption,
408 } for ext in ['vtt', 'srt', 'ass']]
409 subtitles = dict((l, s_formats(l)) for l in ['en', 'fr', 'es'])
410 auto_captions = dict((l, s_formats(l, True)) for l in ['it', 'pt', 'es'])
411 info_dict = {
412 'id': 'test',
413 'title': 'Test',
414 'url': 'http://localhost/video.mp4',
415 'subtitles': subtitles,
416 'automatic_captions': auto_captions,
417 'extractor': 'TEST',
418 }
419
420 def get_info(params={}):
421 params.setdefault('simulate', True)
422 ydl = YDL(params)
423 ydl.report_warning = lambda *args, **kargs: None
424 return ydl.process_video_result(info_dict, download=False)
425
426 result = get_info()
427 self.assertFalse(result.get('requested_subtitles'))
428 self.assertEqual(result['subtitles'], subtitles)
429 self.assertEqual(result['automatic_captions'], auto_captions)
430
431 result = get_info({'writesubtitles': True})
432 subs = result['requested_subtitles']
433 self.assertTrue(subs)
434 self.assertEqual(set(subs.keys()), set(['en']))
435 self.assertTrue(subs['en'].get('data') is None)
436 self.assertEqual(subs['en']['ext'], 'ass')
437
438 result = get_info({'writesubtitles': True, 'subtitlesformat': 'foo/srt'})
439 subs = result['requested_subtitles']
440 self.assertEqual(subs['en']['ext'], 'srt')
441
442 result = get_info({'writesubtitles': True, 'subtitleslangs': ['es', 'fr', 'it']})
443 subs = result['requested_subtitles']
444 self.assertTrue(subs)
445 self.assertEqual(set(subs.keys()), set(['es', 'fr']))
446
447 result = get_info({'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
448 subs = result['requested_subtitles']
449 self.assertTrue(subs)
450 self.assertEqual(set(subs.keys()), set(['es', 'pt']))
451 self.assertFalse(subs['es']['_auto'])
452 self.assertTrue(subs['pt']['_auto'])
453
98c70d6f
JMF
454 result = get_info({'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
455 subs = result['requested_subtitles']
456 self.assertTrue(subs)
457 self.assertEqual(set(subs.keys()), set(['es', 'pt']))
458 self.assertTrue(subs['es']['_auto'])
459 self.assertTrue(subs['pt']['_auto'])
460
b6c45014
JMF
461 def test_add_extra_info(self):
462 test_dict = {
463 'extractor': 'Foo',
464 }
465 extra_info = {
466 'extractor': 'Bar',
467 'playlist': 'funny videos',
468 }
469 YDL.add_extra_info(test_dict, extra_info)
470 self.assertEqual(test_dict['extractor'], 'Foo')
471 self.assertEqual(test_dict['playlist'], 'funny videos')
472
26e63931
JMF
473 def test_prepare_filename(self):
474 info = {
89087418
PH
475 'id': '1234',
476 'ext': 'mp4',
477 'width': None,
26e63931 478 }
5f6a1245 479
26e63931
JMF
480 def fname(templ):
481 ydl = YoutubeDL({'outtmpl': templ})
482 return ydl.prepare_filename(info)
89087418
PH
483 self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4')
484 self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4')
26e63931 485 # Replace missing fields with 'NA'
89087418 486 self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4')
26e63931 487
c57f7757
PH
488 def test_format_note(self):
489 ydl = YoutubeDL()
490 self.assertEqual(ydl._format_note({}), '')
491 assertRegexpMatches(self, ydl._format_note({
492 'vbr': 10,
1c783bca 493 }), '^\s*10k$')
f4d96df0 494
2b4ecde2
JMF
495 def test_postprocessors(self):
496 filename = 'post-processor-testfile.mp4'
497 audiofile = filename + '.mp3'
498
499 class SimplePP(PostProcessor):
500 def run(self, info):
2b4ecde2
JMF
501 with open(audiofile, 'wt') as f:
502 f.write('EXAMPLE')
592e97e8 503 return [info['filepath']], info
2b4ecde2 504
592e97e8 505 def run_pp(params, PP):
2b4ecde2
JMF
506 with open(filename, 'wt') as f:
507 f.write('EXAMPLE')
508 ydl = YoutubeDL(params)
592e97e8 509 ydl.add_post_processor(PP())
2b4ecde2
JMF
510 ydl.post_process(filename, {'filepath': filename})
511
592e97e8 512 run_pp({'keepvideo': True}, SimplePP)
2b4ecde2
JMF
513 self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)
514 self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
515 os.unlink(filename)
516 os.unlink(audiofile)
517
592e97e8 518 run_pp({'keepvideo': False}, SimplePP)
2b4ecde2
JMF
519 self.assertFalse(os.path.exists(filename), '%s exists' % filename)
520 self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
521 os.unlink(audiofile)
522
592e97e8
JMF
523 class ModifierPP(PostProcessor):
524 def run(self, info):
525 with open(info['filepath'], 'wt') as f:
526 f.write('MODIFIED')
527 return [], info
528
529 run_pp({'keepvideo': False}, ModifierPP)
530 self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)
531 os.unlink(filename)
532
531980d8
JMF
533 def test_match_filter(self):
534 class FilterYDL(YDL):
535 def __init__(self, *args, **kwargs):
536 super(FilterYDL, self).__init__(*args, **kwargs)
537 self.params['simulate'] = True
538
539 def process_info(self, info_dict):
540 super(YDL, self).process_info(info_dict)
541
542 def _match_entry(self, info_dict, incomplete):
543 res = super(FilterYDL, self)._match_entry(info_dict, incomplete)
544 if res is None:
545 self.downloaded_info_dicts.append(info_dict)
546 return res
547
548 first = {
549 'id': '1',
550 'url': TEST_URL,
551 'title': 'one',
552 'extractor': 'TEST',
553 'duration': 30,
554 'filesize': 10 * 1024,
555 }
556 second = {
557 'id': '2',
558 'url': TEST_URL,
559 'title': 'two',
560 'extractor': 'TEST',
561 'duration': 10,
562 'description': 'foo',
563 'filesize': 5 * 1024,
564 }
565 videos = [first, second]
566
567 def get_videos(filter_=None):
568 ydl = FilterYDL({'match_filter': filter_})
569 for v in videos:
570 ydl.process_ie_result(v, download=True)
571 return [v['id'] for v in ydl.downloaded_info_dicts]
572
573 res = get_videos()
574 self.assertEqual(res, ['1', '2'])
575
576 def f(v):
577 if v['id'] == '1':
578 return None
579 else:
580 return 'Video id is not 1'
581 res = get_videos(f)
582 self.assertEqual(res, ['1'])
583
584 f = match_filter_func('duration < 30')
585 res = get_videos(f)
586 self.assertEqual(res, ['2'])
587
588 f = match_filter_func('description = foo')
589 res = get_videos(f)
590 self.assertEqual(res, ['2'])
591
592 f = match_filter_func('description =? foo')
593 res = get_videos(f)
594 self.assertEqual(res, ['1', '2'])
595
596 f = match_filter_func('filesize > 5KiB')
597 res = get_videos(f)
598 self.assertEqual(res, ['1'])
599
e9eaf3fb
JMF
600 def test_playlist_items_selection(self):
601 entries = [{
602 'id': compat_str(i),
603 'title': compat_str(i),
604 'url': TEST_URL,
605 } for i in range(1, 5)]
606 playlist = {
607 '_type': 'playlist',
608 'id': 'test',
609 'entries': entries,
610 'extractor': 'test:playlist',
611 'extractor_key': 'test:playlist',
612 'webpage_url': 'http://example.com',
613 }
614
615 def get_ids(params):
616 ydl = YDL(params)
617 # make a copy because the dictionary can be modified
618 ydl.process_ie_result(playlist.copy())
619 return [int(v['id']) for v in ydl.downloaded_info_dicts]
620
621 result = get_ids({})
622 self.assertEqual(result, [1, 2, 3, 4])
623
624 result = get_ids({'playlistend': 10})
625 self.assertEqual(result, [1, 2, 3, 4])
626
627 result = get_ids({'playlistend': 2})
628 self.assertEqual(result, [1, 2])
629
630 result = get_ids({'playliststart': 10})
631 self.assertEqual(result, [])
632
633 result = get_ids({'playliststart': 2})
634 self.assertEqual(result, [2, 3, 4])
635
636 result = get_ids({'playlist_items': '2-4'})
637 self.assertEqual(result, [2, 3, 4])
638
639 result = get_ids({'playlist_items': '2,4'})
640 self.assertEqual(result, [2, 4])
641
642 result = get_ids({'playlist_items': '10'})
643 self.assertEqual(result, [])
644
e37afbe0
JMF
645 def test_urlopen_no_file_protocol(self):
646 # see https://github.com/rg3/youtube-dl/issues/8227
647 ydl = YDL()
648 self.assertRaises(compat_urllib_error.URLError, ydl.urlopen, 'file:///etc/passwd')
649
b286f201
YCH
650 def test_do_not_override_ie_key_in_url_transparent(self):
651 ydl = YDL()
652
653 class Foo1IE(InfoExtractor):
654 _VALID_URL = r'foo1:'
655
656 def _real_extract(self, url):
657 return {
658 '_type': 'url_transparent',
659 'url': 'foo2:',
660 'ie_key': 'Foo2',
661 }
662
663 class Foo2IE(InfoExtractor):
664 _VALID_URL = r'foo2:'
665
666 def _real_extract(self, url):
667 return {
668 '_type': 'url',
669 'url': 'foo3:',
670 'ie_key': 'Foo3',
671 }
672
673 class Foo3IE(InfoExtractor):
674 _VALID_URL = r'foo3:'
675
676 def _real_extract(self, url):
677 return _make_result([{'url': TEST_URL}])
678
679 ydl.add_info_extractor(Foo1IE(ydl))
680 ydl.add_info_extractor(Foo2IE(ydl))
681 ydl.add_info_extractor(Foo3IE(ydl))
682 ydl.extract_info('foo1:')
683 downloaded = ydl.downloaded_info_dicts[0]
684 self.assertEqual(downloaded['url'], TEST_URL)
685
2b4ecde2 686
e028d0d1
JMF
687if __name__ == '__main__':
688 unittest.main()