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