]>
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 |
e37afbe0 | 15 | from youtube_dl.compat import compat_str, compat_urllib_error |
3d4a70b8 | 16 | from youtube_dl.extractor import YoutubeIE |
b286f201 | 17 | from youtube_dl.extractor.common import InfoExtractor |
2b4ecde2 | 18 | from youtube_dl.postprocessor.common import PostProcessor |
bb8e5536 | 19 | from youtube_dl.utils import ExtractorError, match_filter_func |
e028d0d1 | 20 | |
8508557e JMF |
21 | TEST_URL = 'http://localhost/sample.mp4' |
22 | ||
e028d0d1 JMF |
23 | |
24 | class 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 |
37 | def _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 |
48 | class 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 = [ | |
86bf2905 | 237 | '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '17', '36', '13', |
3d4a70b8 PH |
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() | |
1df41411 | 251 | |
91cb6b50 | 252 | # XXX: In real cases InfoExtractor._parse_mpd_formats() fills up 'acodec' |
1df41411 YCH |
253 | # and 'vcodec', while in tests such information is incomplete since |
254 | # commit a6c2c24479e5f4827ceb06f64d855329c0a6f593 | |
255 | # test_YoutubeDL.test_youtube_format_selection is broken without | |
256 | # this fix | |
257 | if 'acodec' in info and 'vcodec' not in info: | |
258 | info['vcodec'] = 'none' | |
259 | elif 'vcodec' in info and 'acodec' not in info: | |
260 | info['acodec'] = 'none' | |
261 | ||
67134eab JMF |
262 | info['format_id'] = f_id |
263 | info['url'] = 'url:' + f_id | |
264 | return info | |
265 | formats_order = [format_info(f_id) for f_id in order] | |
266 | ||
267 | info_dict = _make_result(list(formats_order), extractor='youtube') | |
268 | ydl = YDL({'format': 'bestvideo+bestaudio'}) | |
269 | yie = YoutubeIE(ydl) | |
270 | yie._sort_formats(info_dict['formats']) | |
271 | ydl.process_ie_result(info_dict) | |
272 | downloaded = ydl.downloaded_info_dicts[0] | |
273 | self.assertEqual(downloaded['format_id'], '137+141') | |
274 | self.assertEqual(downloaded['ext'], 'mp4') | |
3d4a70b8 | 275 | |
cf2ac6df JMF |
276 | info_dict = _make_result(list(formats_order), extractor='youtube') |
277 | ydl = YDL({'format': 'bestvideo[height>=999999]+bestaudio/best'}) | |
278 | yie = YoutubeIE(ydl) | |
279 | yie._sort_formats(info_dict['formats']) | |
280 | ydl.process_ie_result(info_dict) | |
281 | downloaded = ydl.downloaded_info_dicts[0] | |
282 | self.assertEqual(downloaded['format_id'], '38') | |
283 | ||
f5f4a27a JMF |
284 | info_dict = _make_result(list(formats_order), extractor='youtube') |
285 | ydl = YDL({'format': 'bestvideo/best,bestaudio'}) | |
286 | yie = YoutubeIE(ydl) | |
287 | yie._sort_formats(info_dict['formats']) | |
288 | ydl.process_ie_result(info_dict) | |
289 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
290 | self.assertEqual(downloaded_ids, ['137', '141']) | |
291 | ||
0130afb7 JMF |
292 | info_dict = _make_result(list(formats_order), extractor='youtube') |
293 | ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])+bestaudio'}) | |
294 | yie = YoutubeIE(ydl) | |
295 | yie._sort_formats(info_dict['formats']) | |
296 | ydl.process_ie_result(info_dict) | |
297 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
298 | self.assertEqual(downloaded_ids, ['137+141', '248+141']) | |
299 | ||
300 | info_dict = _make_result(list(formats_order), extractor='youtube') | |
301 | ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])[height<=720]+bestaudio'}) | |
302 | yie = YoutubeIE(ydl) | |
303 | yie._sort_formats(info_dict['formats']) | |
304 | ydl.process_ie_result(info_dict) | |
305 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
306 | self.assertEqual(downloaded_ids, ['136+141', '247+141']) | |
307 | ||
308 | info_dict = _make_result(list(formats_order), extractor='youtube') | |
309 | ydl = YDL({'format': '(bestvideo[ext=none]/bestvideo[ext=webm])+bestaudio'}) | |
310 | yie = YoutubeIE(ydl) | |
311 | yie._sort_formats(info_dict['formats']) | |
312 | ydl.process_ie_result(info_dict) | |
313 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
314 | self.assertEqual(downloaded_ids, ['248+141']) | |
315 | ||
67134eab | 316 | for f1, f2 in zip(formats_order, formats_order[1:]): |
3537b93d | 317 | info_dict = _make_result([f1, f2], extractor='youtube') |
8dd54188 | 318 | ydl = YDL({'format': 'best/bestvideo'}) |
3d4a70b8 PH |
319 | yie = YoutubeIE(ydl) |
320 | yie._sort_formats(info_dict['formats']) | |
321 | ydl.process_ie_result(info_dict) | |
322 | downloaded = ydl.downloaded_info_dicts[0] | |
67134eab | 323 | self.assertEqual(downloaded['format_id'], f1['format_id']) |
3d4a70b8 | 324 | |
3537b93d | 325 | info_dict = _make_result([f2, f1], extractor='youtube') |
8dd54188 | 326 | ydl = YDL({'format': 'best/bestvideo'}) |
3d4a70b8 PH |
327 | yie = YoutubeIE(ydl) |
328 | yie._sort_formats(info_dict['formats']) | |
329 | ydl.process_ie_result(info_dict) | |
330 | downloaded = ydl.downloaded_info_dicts[0] | |
67134eab | 331 | self.assertEqual(downloaded['format_id'], f1['format_id']) |
3d4a70b8 | 332 | |
0a31a350 JMF |
333 | def test_invalid_format_specs(self): |
334 | def assert_syntax_error(format_spec): | |
335 | ydl = YDL({'format': format_spec}) | |
336 | info_dict = _make_result([{'format_id': 'foo', 'url': TEST_URL}]) | |
337 | self.assertRaises(SyntaxError, ydl.process_ie_result, info_dict) | |
338 | ||
339 | assert_syntax_error('bestvideo,,best') | |
340 | assert_syntax_error('+bestaudio') | |
341 | assert_syntax_error('bestvideo+') | |
d96d604e | 342 | assert_syntax_error('/') |
0a31a350 | 343 | |
083c9df9 PH |
344 | def test_format_filtering(self): |
345 | formats = [ | |
346 | {'format_id': 'A', 'filesize': 500, 'width': 1000}, | |
347 | {'format_id': 'B', 'filesize': 1000, 'width': 500}, | |
348 | {'format_id': 'C', 'filesize': 1000, 'width': 400}, | |
349 | {'format_id': 'D', 'filesize': 2000, 'width': 600}, | |
350 | {'format_id': 'E', 'filesize': 3000}, | |
351 | {'format_id': 'F'}, | |
352 | {'format_id': 'G', 'filesize': 1000000}, | |
353 | ] | |
354 | for f in formats: | |
355 | f['url'] = 'http://_/' | |
356 | f['ext'] = 'unknown' | |
357 | info_dict = _make_result(formats) | |
358 | ||
359 | ydl = YDL({'format': 'best[filesize<3000]'}) | |
360 | ydl.process_ie_result(info_dict) | |
361 | downloaded = ydl.downloaded_info_dicts[0] | |
362 | self.assertEqual(downloaded['format_id'], 'D') | |
363 | ||
364 | ydl = YDL({'format': 'best[filesize<=3000]'}) | |
365 | ydl.process_ie_result(info_dict) | |
366 | downloaded = ydl.downloaded_info_dicts[0] | |
367 | self.assertEqual(downloaded['format_id'], 'E') | |
368 | ||
369 | ydl = YDL({'format': 'best[filesize <= ? 3000]'}) | |
370 | ydl.process_ie_result(info_dict) | |
371 | downloaded = ydl.downloaded_info_dicts[0] | |
372 | self.assertEqual(downloaded['format_id'], 'F') | |
373 | ||
374 | ydl = YDL({'format': 'best [filesize = 1000] [width>450]'}) | |
375 | ydl.process_ie_result(info_dict) | |
376 | downloaded = ydl.downloaded_info_dicts[0] | |
377 | self.assertEqual(downloaded['format_id'], 'B') | |
378 | ||
379 | ydl = YDL({'format': 'best [filesize = 1000] [width!=450]'}) | |
380 | ydl.process_ie_result(info_dict) | |
381 | downloaded = ydl.downloaded_info_dicts[0] | |
382 | self.assertEqual(downloaded['format_id'], 'C') | |
383 | ||
384 | ydl = YDL({'format': '[filesize>?1]'}) | |
385 | ydl.process_ie_result(info_dict) | |
386 | downloaded = ydl.downloaded_info_dicts[0] | |
387 | self.assertEqual(downloaded['format_id'], 'G') | |
388 | ||
389 | ydl = YDL({'format': '[filesize<1M]'}) | |
390 | ydl.process_ie_result(info_dict) | |
391 | downloaded = ydl.downloaded_info_dicts[0] | |
392 | self.assertEqual(downloaded['format_id'], 'E') | |
393 | ||
394 | ydl = YDL({'format': '[filesize<1MiB]'}) | |
395 | ydl.process_ie_result(info_dict) | |
396 | downloaded = ydl.downloaded_info_dicts[0] | |
397 | self.assertEqual(downloaded['format_id'], 'G') | |
398 | ||
5acfa126 JMF |
399 | ydl = YDL({'format': 'all[width>=400][width<=600]'}) |
400 | ydl.process_ie_result(info_dict) | |
401 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
402 | self.assertEqual(downloaded_ids, ['B', 'C', 'D']) | |
403 | ||
bb8e5536 JMF |
404 | ydl = YDL({'format': 'best[height<40]'}) |
405 | try: | |
406 | ydl.process_ie_result(info_dict) | |
407 | except ExtractorError: | |
408 | pass | |
409 | self.assertEqual(ydl.downloaded_info_dicts, []) | |
410 | ||
f20bf146 JMF |
411 | |
412 | class TestYoutubeDL(unittest.TestCase): | |
ab84349b JMF |
413 | def test_subtitles(self): |
414 | def s_formats(lang, autocaption=False): | |
415 | return [{ | |
416 | 'ext': ext, | |
417 | 'url': 'http://localhost/video.%s.%s' % (lang, ext), | |
418 | '_auto': autocaption, | |
419 | } for ext in ['vtt', 'srt', 'ass']] | |
420 | subtitles = dict((l, s_formats(l)) for l in ['en', 'fr', 'es']) | |
421 | auto_captions = dict((l, s_formats(l, True)) for l in ['it', 'pt', 'es']) | |
422 | info_dict = { | |
423 | 'id': 'test', | |
424 | 'title': 'Test', | |
425 | 'url': 'http://localhost/video.mp4', | |
426 | 'subtitles': subtitles, | |
427 | 'automatic_captions': auto_captions, | |
428 | 'extractor': 'TEST', | |
429 | } | |
430 | ||
431 | def get_info(params={}): | |
432 | params.setdefault('simulate', True) | |
433 | ydl = YDL(params) | |
434 | ydl.report_warning = lambda *args, **kargs: None | |
435 | return ydl.process_video_result(info_dict, download=False) | |
436 | ||
437 | result = get_info() | |
438 | self.assertFalse(result.get('requested_subtitles')) | |
439 | self.assertEqual(result['subtitles'], subtitles) | |
440 | self.assertEqual(result['automatic_captions'], auto_captions) | |
441 | ||
442 | result = get_info({'writesubtitles': True}) | |
443 | subs = result['requested_subtitles'] | |
444 | self.assertTrue(subs) | |
445 | self.assertEqual(set(subs.keys()), set(['en'])) | |
446 | self.assertTrue(subs['en'].get('data') is None) | |
447 | self.assertEqual(subs['en']['ext'], 'ass') | |
448 | ||
449 | result = get_info({'writesubtitles': True, 'subtitlesformat': 'foo/srt'}) | |
450 | subs = result['requested_subtitles'] | |
451 | self.assertEqual(subs['en']['ext'], 'srt') | |
452 | ||
453 | result = get_info({'writesubtitles': True, 'subtitleslangs': ['es', 'fr', 'it']}) | |
454 | subs = result['requested_subtitles'] | |
455 | self.assertTrue(subs) | |
456 | self.assertEqual(set(subs.keys()), set(['es', 'fr'])) | |
457 | ||
458 | result = get_info({'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']}) | |
459 | subs = result['requested_subtitles'] | |
460 | self.assertTrue(subs) | |
461 | self.assertEqual(set(subs.keys()), set(['es', 'pt'])) | |
462 | self.assertFalse(subs['es']['_auto']) | |
463 | self.assertTrue(subs['pt']['_auto']) | |
464 | ||
98c70d6f JMF |
465 | result = get_info({'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']}) |
466 | subs = result['requested_subtitles'] | |
467 | self.assertTrue(subs) | |
468 | self.assertEqual(set(subs.keys()), set(['es', 'pt'])) | |
469 | self.assertTrue(subs['es']['_auto']) | |
470 | self.assertTrue(subs['pt']['_auto']) | |
471 | ||
b6c45014 JMF |
472 | def test_add_extra_info(self): |
473 | test_dict = { | |
474 | 'extractor': 'Foo', | |
475 | } | |
476 | extra_info = { | |
477 | 'extractor': 'Bar', | |
478 | 'playlist': 'funny videos', | |
479 | } | |
480 | YDL.add_extra_info(test_dict, extra_info) | |
481 | self.assertEqual(test_dict['extractor'], 'Foo') | |
482 | self.assertEqual(test_dict['playlist'], 'funny videos') | |
483 | ||
26e63931 JMF |
484 | def test_prepare_filename(self): |
485 | info = { | |
89087418 PH |
486 | 'id': '1234', |
487 | 'ext': 'mp4', | |
488 | 'width': None, | |
26e63931 | 489 | } |
5f6a1245 | 490 | |
26e63931 JMF |
491 | def fname(templ): |
492 | ydl = YoutubeDL({'outtmpl': templ}) | |
493 | return ydl.prepare_filename(info) | |
89087418 PH |
494 | self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4') |
495 | self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4') | |
26e63931 | 496 | # Replace missing fields with 'NA' |
89087418 | 497 | self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4') |
26e63931 | 498 | |
c57f7757 PH |
499 | def test_format_note(self): |
500 | ydl = YoutubeDL() | |
501 | self.assertEqual(ydl._format_note({}), '') | |
502 | assertRegexpMatches(self, ydl._format_note({ | |
503 | 'vbr': 10, | |
1c783bca | 504 | }), '^\s*10k$') |
5d583bdf S |
505 | assertRegexpMatches(self, ydl._format_note({ |
506 | 'fps': 30, | |
507 | }), '^30fps$') | |
508 | ||
f4d96df0 | 509 | |
2b4ecde2 JMF |
510 | def test_postprocessors(self): |
511 | filename = 'post-processor-testfile.mp4' | |
512 | audiofile = filename + '.mp3' | |
513 | ||
514 | class SimplePP(PostProcessor): | |
515 | def run(self, info): | |
2b4ecde2 JMF |
516 | with open(audiofile, 'wt') as f: |
517 | f.write('EXAMPLE') | |
592e97e8 | 518 | return [info['filepath']], info |
2b4ecde2 | 519 | |
592e97e8 | 520 | def run_pp(params, PP): |
2b4ecde2 JMF |
521 | with open(filename, 'wt') as f: |
522 | f.write('EXAMPLE') | |
523 | ydl = YoutubeDL(params) | |
592e97e8 | 524 | ydl.add_post_processor(PP()) |
2b4ecde2 JMF |
525 | ydl.post_process(filename, {'filepath': filename}) |
526 | ||
592e97e8 | 527 | run_pp({'keepvideo': True}, SimplePP) |
2b4ecde2 JMF |
528 | self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename) |
529 | self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile) | |
530 | os.unlink(filename) | |
531 | os.unlink(audiofile) | |
532 | ||
592e97e8 | 533 | run_pp({'keepvideo': False}, SimplePP) |
2b4ecde2 JMF |
534 | self.assertFalse(os.path.exists(filename), '%s exists' % filename) |
535 | self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile) | |
536 | os.unlink(audiofile) | |
537 | ||
592e97e8 JMF |
538 | class ModifierPP(PostProcessor): |
539 | def run(self, info): | |
540 | with open(info['filepath'], 'wt') as f: | |
541 | f.write('MODIFIED') | |
542 | return [], info | |
543 | ||
544 | run_pp({'keepvideo': False}, ModifierPP) | |
545 | self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename) | |
546 | os.unlink(filename) | |
547 | ||
531980d8 JMF |
548 | def test_match_filter(self): |
549 | class FilterYDL(YDL): | |
550 | def __init__(self, *args, **kwargs): | |
551 | super(FilterYDL, self).__init__(*args, **kwargs) | |
552 | self.params['simulate'] = True | |
553 | ||
554 | def process_info(self, info_dict): | |
555 | super(YDL, self).process_info(info_dict) | |
556 | ||
557 | def _match_entry(self, info_dict, incomplete): | |
558 | res = super(FilterYDL, self)._match_entry(info_dict, incomplete) | |
559 | if res is None: | |
560 | self.downloaded_info_dicts.append(info_dict) | |
561 | return res | |
562 | ||
563 | first = { | |
564 | 'id': '1', | |
565 | 'url': TEST_URL, | |
566 | 'title': 'one', | |
567 | 'extractor': 'TEST', | |
568 | 'duration': 30, | |
569 | 'filesize': 10 * 1024, | |
570 | } | |
571 | second = { | |
572 | 'id': '2', | |
573 | 'url': TEST_URL, | |
574 | 'title': 'two', | |
575 | 'extractor': 'TEST', | |
576 | 'duration': 10, | |
577 | 'description': 'foo', | |
578 | 'filesize': 5 * 1024, | |
579 | } | |
580 | videos = [first, second] | |
581 | ||
582 | def get_videos(filter_=None): | |
583 | ydl = FilterYDL({'match_filter': filter_}) | |
584 | for v in videos: | |
585 | ydl.process_ie_result(v, download=True) | |
586 | return [v['id'] for v in ydl.downloaded_info_dicts] | |
587 | ||
588 | res = get_videos() | |
589 | self.assertEqual(res, ['1', '2']) | |
590 | ||
591 | def f(v): | |
592 | if v['id'] == '1': | |
593 | return None | |
594 | else: | |
595 | return 'Video id is not 1' | |
596 | res = get_videos(f) | |
597 | self.assertEqual(res, ['1']) | |
598 | ||
599 | f = match_filter_func('duration < 30') | |
600 | res = get_videos(f) | |
601 | self.assertEqual(res, ['2']) | |
602 | ||
603 | f = match_filter_func('description = foo') | |
604 | res = get_videos(f) | |
605 | self.assertEqual(res, ['2']) | |
606 | ||
607 | f = match_filter_func('description =? foo') | |
608 | res = get_videos(f) | |
609 | self.assertEqual(res, ['1', '2']) | |
610 | ||
611 | f = match_filter_func('filesize > 5KiB') | |
612 | res = get_videos(f) | |
613 | self.assertEqual(res, ['1']) | |
614 | ||
e9eaf3fb JMF |
615 | def test_playlist_items_selection(self): |
616 | entries = [{ | |
617 | 'id': compat_str(i), | |
618 | 'title': compat_str(i), | |
619 | 'url': TEST_URL, | |
620 | } for i in range(1, 5)] | |
621 | playlist = { | |
622 | '_type': 'playlist', | |
623 | 'id': 'test', | |
624 | 'entries': entries, | |
625 | 'extractor': 'test:playlist', | |
626 | 'extractor_key': 'test:playlist', | |
627 | 'webpage_url': 'http://example.com', | |
628 | } | |
629 | ||
630 | def get_ids(params): | |
631 | ydl = YDL(params) | |
632 | # make a copy because the dictionary can be modified | |
633 | ydl.process_ie_result(playlist.copy()) | |
634 | return [int(v['id']) for v in ydl.downloaded_info_dicts] | |
635 | ||
636 | result = get_ids({}) | |
637 | self.assertEqual(result, [1, 2, 3, 4]) | |
638 | ||
639 | result = get_ids({'playlistend': 10}) | |
640 | self.assertEqual(result, [1, 2, 3, 4]) | |
641 | ||
642 | result = get_ids({'playlistend': 2}) | |
643 | self.assertEqual(result, [1, 2]) | |
644 | ||
645 | result = get_ids({'playliststart': 10}) | |
646 | self.assertEqual(result, []) | |
647 | ||
648 | result = get_ids({'playliststart': 2}) | |
649 | self.assertEqual(result, [2, 3, 4]) | |
650 | ||
651 | result = get_ids({'playlist_items': '2-4'}) | |
652 | self.assertEqual(result, [2, 3, 4]) | |
653 | ||
654 | result = get_ids({'playlist_items': '2,4'}) | |
655 | self.assertEqual(result, [2, 4]) | |
656 | ||
657 | result = get_ids({'playlist_items': '10'}) | |
658 | self.assertEqual(result, []) | |
659 | ||
e37afbe0 JMF |
660 | def test_urlopen_no_file_protocol(self): |
661 | # see https://github.com/rg3/youtube-dl/issues/8227 | |
662 | ydl = YDL() | |
663 | self.assertRaises(compat_urllib_error.URLError, ydl.urlopen, 'file:///etc/passwd') | |
664 | ||
b286f201 YCH |
665 | def test_do_not_override_ie_key_in_url_transparent(self): |
666 | ydl = YDL() | |
667 | ||
668 | class Foo1IE(InfoExtractor): | |
669 | _VALID_URL = r'foo1:' | |
670 | ||
671 | def _real_extract(self, url): | |
672 | return { | |
673 | '_type': 'url_transparent', | |
674 | 'url': 'foo2:', | |
675 | 'ie_key': 'Foo2', | |
676 | } | |
677 | ||
678 | class Foo2IE(InfoExtractor): | |
679 | _VALID_URL = r'foo2:' | |
680 | ||
681 | def _real_extract(self, url): | |
682 | return { | |
683 | '_type': 'url', | |
684 | 'url': 'foo3:', | |
685 | 'ie_key': 'Foo3', | |
686 | } | |
687 | ||
688 | class Foo3IE(InfoExtractor): | |
689 | _VALID_URL = r'foo3:' | |
690 | ||
691 | def _real_extract(self, url): | |
692 | return _make_result([{'url': TEST_URL}]) | |
693 | ||
694 | ydl.add_info_extractor(Foo1IE(ydl)) | |
695 | ydl.add_info_extractor(Foo2IE(ydl)) | |
696 | ydl.add_info_extractor(Foo3IE(ydl)) | |
697 | ydl.extract_info('foo1:') | |
698 | downloaded = ydl.downloaded_info_dicts[0] | |
699 | self.assertEqual(downloaded['url'], TEST_URL) | |
700 | ||
2b4ecde2 | 701 | |
e028d0d1 JMF |
702 | if __name__ == '__main__': |
703 | unittest.main() |