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