]> jfr.im git - yt-dlp.git/blame - test/test_YoutubeDL.py
Update YoutubeDL.py
[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
3d4a70b8
PH
224 def test_youtube_format_selection(self):
225 order = [
226 '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '36', '17', '13',
227 # Apple HTTP Live Streaming
228 '96', '95', '94', '93', '92', '132', '151',
229 # 3D
230 '85', '84', '102', '83', '101', '82', '100',
231 # Dash video
c11125f9 232 '137', '248', '136', '247', '135', '246',
3d4a70b8
PH
233 '245', '244', '134', '243', '133', '242', '160',
234 # Dash audio
a053c349 235 '141', '172', '140', '171', '139',
3d4a70b8
PH
236 ]
237
67134eab
JMF
238 def format_info(f_id):
239 info = YoutubeIE._formats[f_id].copy()
240 info['format_id'] = f_id
241 info['url'] = 'url:' + f_id
242 return info
243 formats_order = [format_info(f_id) for f_id in order]
244
245 info_dict = _make_result(list(formats_order), extractor='youtube')
246 ydl = YDL({'format': 'bestvideo+bestaudio'})
247 yie = YoutubeIE(ydl)
248 yie._sort_formats(info_dict['formats'])
249 ydl.process_ie_result(info_dict)
250 downloaded = ydl.downloaded_info_dicts[0]
251 self.assertEqual(downloaded['format_id'], '137+141')
252 self.assertEqual(downloaded['ext'], 'mp4')
3d4a70b8 253
cf2ac6df
JMF
254 info_dict = _make_result(list(formats_order), extractor='youtube')
255 ydl = YDL({'format': 'bestvideo[height>=999999]+bestaudio/best'})
256 yie = YoutubeIE(ydl)
257 yie._sort_formats(info_dict['formats'])
258 ydl.process_ie_result(info_dict)
259 downloaded = ydl.downloaded_info_dicts[0]
260 self.assertEqual(downloaded['format_id'], '38')
261
f5f4a27a
JMF
262 info_dict = _make_result(list(formats_order), extractor='youtube')
263 ydl = YDL({'format': 'bestvideo/best,bestaudio'})
264 yie = YoutubeIE(ydl)
265 yie._sort_formats(info_dict['formats'])
266 ydl.process_ie_result(info_dict)
267 downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
268 self.assertEqual(downloaded_ids, ['137', '141'])
269
0130afb7
JMF
270 info_dict = _make_result(list(formats_order), extractor='youtube')
271 ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])+bestaudio'})
272 yie = YoutubeIE(ydl)
273 yie._sort_formats(info_dict['formats'])
274 ydl.process_ie_result(info_dict)
275 downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
276 self.assertEqual(downloaded_ids, ['137+141', '248+141'])
277
278 info_dict = _make_result(list(formats_order), extractor='youtube')
279 ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])[height<=720]+bestaudio'})
280 yie = YoutubeIE(ydl)
281 yie._sort_formats(info_dict['formats'])
282 ydl.process_ie_result(info_dict)
283 downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
284 self.assertEqual(downloaded_ids, ['136+141', '247+141'])
285
286 info_dict = _make_result(list(formats_order), extractor='youtube')
287 ydl = YDL({'format': '(bestvideo[ext=none]/bestvideo[ext=webm])+bestaudio'})
288 yie = YoutubeIE(ydl)
289 yie._sort_formats(info_dict['formats'])
290 ydl.process_ie_result(info_dict)
291 downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
292 self.assertEqual(downloaded_ids, ['248+141'])
293
67134eab 294 for f1, f2 in zip(formats_order, formats_order[1:]):
3537b93d 295 info_dict = _make_result([f1, f2], extractor='youtube')
8dd54188 296 ydl = YDL({'format': 'best/bestvideo'})
3d4a70b8
PH
297 yie = YoutubeIE(ydl)
298 yie._sort_formats(info_dict['formats'])
299 ydl.process_ie_result(info_dict)
300 downloaded = ydl.downloaded_info_dicts[0]
67134eab 301 self.assertEqual(downloaded['format_id'], f1['format_id'])
3d4a70b8 302
3537b93d 303 info_dict = _make_result([f2, f1], extractor='youtube')
8dd54188 304 ydl = YDL({'format': 'best/bestvideo'})
3d4a70b8
PH
305 yie = YoutubeIE(ydl)
306 yie._sort_formats(info_dict['formats'])
307 ydl.process_ie_result(info_dict)
308 downloaded = ydl.downloaded_info_dicts[0]
67134eab 309 self.assertEqual(downloaded['format_id'], f1['format_id'])
3d4a70b8 310
0a31a350
JMF
311 def test_invalid_format_specs(self):
312 def assert_syntax_error(format_spec):
313 ydl = YDL({'format': format_spec})
314 info_dict = _make_result([{'format_id': 'foo', 'url': TEST_URL}])
315 self.assertRaises(SyntaxError, ydl.process_ie_result, info_dict)
316
317 assert_syntax_error('bestvideo,,best')
318 assert_syntax_error('+bestaudio')
319 assert_syntax_error('bestvideo+')
d96d604e 320 assert_syntax_error('/')
0a31a350 321
083c9df9
PH
322 def test_format_filtering(self):
323 formats = [
324 {'format_id': 'A', 'filesize': 500, 'width': 1000},
325 {'format_id': 'B', 'filesize': 1000, 'width': 500},
326 {'format_id': 'C', 'filesize': 1000, 'width': 400},
327 {'format_id': 'D', 'filesize': 2000, 'width': 600},
328 {'format_id': 'E', 'filesize': 3000},
329 {'format_id': 'F'},
330 {'format_id': 'G', 'filesize': 1000000},
331 ]
332 for f in formats:
333 f['url'] = 'http://_/'
334 f['ext'] = 'unknown'
335 info_dict = _make_result(formats)
336
337 ydl = YDL({'format': 'best[filesize<3000]'})
338 ydl.process_ie_result(info_dict)
339 downloaded = ydl.downloaded_info_dicts[0]
340 self.assertEqual(downloaded['format_id'], 'D')
341
342 ydl = YDL({'format': 'best[filesize<=3000]'})
343 ydl.process_ie_result(info_dict)
344 downloaded = ydl.downloaded_info_dicts[0]
345 self.assertEqual(downloaded['format_id'], 'E')
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'], 'F')
351
352 ydl = YDL({'format': 'best [filesize = 1000] [width>450]'})
353 ydl.process_ie_result(info_dict)
354 downloaded = ydl.downloaded_info_dicts[0]
355 self.assertEqual(downloaded['format_id'], 'B')
356
357 ydl = YDL({'format': 'best [filesize = 1000] [width!=450]'})
358 ydl.process_ie_result(info_dict)
359 downloaded = ydl.downloaded_info_dicts[0]
360 self.assertEqual(downloaded['format_id'], 'C')
361
362 ydl = YDL({'format': '[filesize>?1]'})
363 ydl.process_ie_result(info_dict)
364 downloaded = ydl.downloaded_info_dicts[0]
365 self.assertEqual(downloaded['format_id'], 'G')
366
367 ydl = YDL({'format': '[filesize<1M]'})
368 ydl.process_ie_result(info_dict)
369 downloaded = ydl.downloaded_info_dicts[0]
370 self.assertEqual(downloaded['format_id'], 'E')
371
372 ydl = YDL({'format': '[filesize<1MiB]'})
373 ydl.process_ie_result(info_dict)
374 downloaded = ydl.downloaded_info_dicts[0]
375 self.assertEqual(downloaded['format_id'], 'G')
376
5acfa126
JMF
377 ydl = YDL({'format': 'all[width>=400][width<=600]'})
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, ['B', 'C', 'D'])
381
bb8e5536
JMF
382 ydl = YDL({'format': 'best[height<40]'})
383 try:
384 ydl.process_ie_result(info_dict)
385 except ExtractorError:
386 pass
387 self.assertEqual(ydl.downloaded_info_dicts, [])
388
f20bf146
JMF
389
390class TestYoutubeDL(unittest.TestCase):
ab84349b
JMF
391 def test_subtitles(self):
392 def s_formats(lang, autocaption=False):
393 return [{
394 'ext': ext,
395 'url': 'http://localhost/video.%s.%s' % (lang, ext),
396 '_auto': autocaption,
397 } for ext in ['vtt', 'srt', 'ass']]
398 subtitles = dict((l, s_formats(l)) for l in ['en', 'fr', 'es'])
399 auto_captions = dict((l, s_formats(l, True)) for l in ['it', 'pt', 'es'])
400 info_dict = {
401 'id': 'test',
402 'title': 'Test',
403 'url': 'http://localhost/video.mp4',
404 'subtitles': subtitles,
405 'automatic_captions': auto_captions,
406 'extractor': 'TEST',
407 }
408
409 def get_info(params={}):
410 params.setdefault('simulate', True)
411 ydl = YDL(params)
412 ydl.report_warning = lambda *args, **kargs: None
413 return ydl.process_video_result(info_dict, download=False)
414
415 result = get_info()
416 self.assertFalse(result.get('requested_subtitles'))
417 self.assertEqual(result['subtitles'], subtitles)
418 self.assertEqual(result['automatic_captions'], auto_captions)
419
420 result = get_info({'writesubtitles': True})
421 subs = result['requested_subtitles']
422 self.assertTrue(subs)
423 self.assertEqual(set(subs.keys()), set(['en']))
424 self.assertTrue(subs['en'].get('data') is None)
425 self.assertEqual(subs['en']['ext'], 'ass')
426
427 result = get_info({'writesubtitles': True, 'subtitlesformat': 'foo/srt'})
428 subs = result['requested_subtitles']
429 self.assertEqual(subs['en']['ext'], 'srt')
430
431 result = get_info({'writesubtitles': True, 'subtitleslangs': ['es', 'fr', 'it']})
432 subs = result['requested_subtitles']
433 self.assertTrue(subs)
434 self.assertEqual(set(subs.keys()), set(['es', 'fr']))
435
436 result = get_info({'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
437 subs = result['requested_subtitles']
438 self.assertTrue(subs)
439 self.assertEqual(set(subs.keys()), set(['es', 'pt']))
440 self.assertFalse(subs['es']['_auto'])
441 self.assertTrue(subs['pt']['_auto'])
442
98c70d6f
JMF
443 result = get_info({'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
444 subs = result['requested_subtitles']
445 self.assertTrue(subs)
446 self.assertEqual(set(subs.keys()), set(['es', 'pt']))
447 self.assertTrue(subs['es']['_auto'])
448 self.assertTrue(subs['pt']['_auto'])
449
b6c45014
JMF
450 def test_add_extra_info(self):
451 test_dict = {
452 'extractor': 'Foo',
453 }
454 extra_info = {
455 'extractor': 'Bar',
456 'playlist': 'funny videos',
457 }
458 YDL.add_extra_info(test_dict, extra_info)
459 self.assertEqual(test_dict['extractor'], 'Foo')
460 self.assertEqual(test_dict['playlist'], 'funny videos')
461
26e63931
JMF
462 def test_prepare_filename(self):
463 info = {
89087418
PH
464 'id': '1234',
465 'ext': 'mp4',
466 'width': None,
26e63931 467 }
5f6a1245 468
26e63931
JMF
469 def fname(templ):
470 ydl = YoutubeDL({'outtmpl': templ})
471 return ydl.prepare_filename(info)
89087418
PH
472 self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4')
473 self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4')
26e63931 474 # Replace missing fields with 'NA'
89087418 475 self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4')
26e63931 476
c57f7757
PH
477 def test_format_note(self):
478 ydl = YoutubeDL()
479 self.assertEqual(ydl._format_note({}), '')
480 assertRegexpMatches(self, ydl._format_note({
481 'vbr': 10,
1c783bca 482 }), '^\s*10k$')
f4d96df0 483
2b4ecde2
JMF
484 def test_postprocessors(self):
485 filename = 'post-processor-testfile.mp4'
486 audiofile = filename + '.mp3'
487
488 class SimplePP(PostProcessor):
489 def run(self, info):
2b4ecde2
JMF
490 with open(audiofile, 'wt') as f:
491 f.write('EXAMPLE')
592e97e8 492 return [info['filepath']], info
2b4ecde2 493
592e97e8 494 def run_pp(params, PP):
2b4ecde2
JMF
495 with open(filename, 'wt') as f:
496 f.write('EXAMPLE')
497 ydl = YoutubeDL(params)
592e97e8 498 ydl.add_post_processor(PP())
2b4ecde2
JMF
499 ydl.post_process(filename, {'filepath': filename})
500
592e97e8 501 run_pp({'keepvideo': True}, SimplePP)
2b4ecde2
JMF
502 self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)
503 self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
504 os.unlink(filename)
505 os.unlink(audiofile)
506
592e97e8 507 run_pp({'keepvideo': False}, SimplePP)
2b4ecde2
JMF
508 self.assertFalse(os.path.exists(filename), '%s exists' % filename)
509 self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
510 os.unlink(audiofile)
511
592e97e8
JMF
512 class ModifierPP(PostProcessor):
513 def run(self, info):
514 with open(info['filepath'], 'wt') as f:
515 f.write('MODIFIED')
516 return [], info
517
518 run_pp({'keepvideo': False}, ModifierPP)
519 self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)
520 os.unlink(filename)
521
531980d8
JMF
522 def test_match_filter(self):
523 class FilterYDL(YDL):
524 def __init__(self, *args, **kwargs):
525 super(FilterYDL, self).__init__(*args, **kwargs)
526 self.params['simulate'] = True
527
528 def process_info(self, info_dict):
529 super(YDL, self).process_info(info_dict)
530
531 def _match_entry(self, info_dict, incomplete):
532 res = super(FilterYDL, self)._match_entry(info_dict, incomplete)
533 if res is None:
534 self.downloaded_info_dicts.append(info_dict)
535 return res
536
537 first = {
538 'id': '1',
539 'url': TEST_URL,
540 'title': 'one',
541 'extractor': 'TEST',
542 'duration': 30,
543 'filesize': 10 * 1024,
544 }
545 second = {
546 'id': '2',
547 'url': TEST_URL,
548 'title': 'two',
549 'extractor': 'TEST',
550 'duration': 10,
551 'description': 'foo',
552 'filesize': 5 * 1024,
553 }
554 videos = [first, second]
555
556 def get_videos(filter_=None):
557 ydl = FilterYDL({'match_filter': filter_})
558 for v in videos:
559 ydl.process_ie_result(v, download=True)
560 return [v['id'] for v in ydl.downloaded_info_dicts]
561
562 res = get_videos()
563 self.assertEqual(res, ['1', '2'])
564
565 def f(v):
566 if v['id'] == '1':
567 return None
568 else:
569 return 'Video id is not 1'
570 res = get_videos(f)
571 self.assertEqual(res, ['1'])
572
573 f = match_filter_func('duration < 30')
574 res = get_videos(f)
575 self.assertEqual(res, ['2'])
576
577 f = match_filter_func('description = foo')
578 res = get_videos(f)
579 self.assertEqual(res, ['2'])
580
581 f = match_filter_func('description =? foo')
582 res = get_videos(f)
583 self.assertEqual(res, ['1', '2'])
584
585 f = match_filter_func('filesize > 5KiB')
586 res = get_videos(f)
587 self.assertEqual(res, ['1'])
588
e9eaf3fb
JMF
589 def test_playlist_items_selection(self):
590 entries = [{
591 'id': compat_str(i),
592 'title': compat_str(i),
593 'url': TEST_URL,
594 } for i in range(1, 5)]
595 playlist = {
596 '_type': 'playlist',
597 'id': 'test',
598 'entries': entries,
599 'extractor': 'test:playlist',
600 'extractor_key': 'test:playlist',
601 'webpage_url': 'http://example.com',
602 }
603
604 def get_ids(params):
605 ydl = YDL(params)
606 # make a copy because the dictionary can be modified
607 ydl.process_ie_result(playlist.copy())
608 return [int(v['id']) for v in ydl.downloaded_info_dicts]
609
610 result = get_ids({})
611 self.assertEqual(result, [1, 2, 3, 4])
612
613 result = get_ids({'playlistend': 10})
614 self.assertEqual(result, [1, 2, 3, 4])
615
616 result = get_ids({'playlistend': 2})
617 self.assertEqual(result, [1, 2])
618
619 result = get_ids({'playliststart': 10})
620 self.assertEqual(result, [])
621
622 result = get_ids({'playliststart': 2})
623 self.assertEqual(result, [2, 3, 4])
624
625 result = get_ids({'playlist_items': '2-4'})
626 self.assertEqual(result, [2, 3, 4])
627
628 result = get_ids({'playlist_items': '2,4'})
629 self.assertEqual(result, [2, 4])
630
631 result = get_ids({'playlist_items': '10'})
632 self.assertEqual(result, [])
633
e37afbe0
JMF
634 def test_urlopen_no_file_protocol(self):
635 # see https://github.com/rg3/youtube-dl/issues/8227
636 ydl = YDL()
637 self.assertRaises(compat_urllib_error.URLError, ydl.urlopen, 'file:///etc/passwd')
638
2b4ecde2 639
e028d0d1
JMF
640if __name__ == '__main__':
641 unittest.main()