]>
Commit | Line | Data |
---|---|---|
cc52de43 | 1 | #!/usr/bin/env python3 |
db13c16e | 2 | # coding: utf-8 |
e028d0d1 | 3 | |
89087418 PH |
4 | from __future__ import unicode_literals |
5 | ||
5d254f77 PH |
6 | # Allow direct execution |
7 | import os | |
e028d0d1 JMF |
8 | import sys |
9 | import unittest | |
5d254f77 | 10 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
e028d0d1 | 11 | |
0217c783 | 12 | import copy |
7d1eb38a | 13 | import json |
0217c783 | 14 | |
c57f7757 | 15 | from test.helper import FakeYDL, assertRegexpMatches |
7a5c1cfe | 16 | from yt_dlp import YoutubeDL |
901130bb | 17 | from yt_dlp.compat import compat_os_name, compat_setenv, compat_str, compat_urllib_error |
7a5c1cfe P |
18 | from yt_dlp.extractor import YoutubeIE |
19 | from yt_dlp.extractor.common import InfoExtractor | |
20 | from yt_dlp.postprocessor.common import PostProcessor | |
6e84b215 | 21 | from yt_dlp.utils import ExtractorError, int_or_none, match_filter_func, LazyList |
e028d0d1 | 22 | |
8508557e JMF |
23 | TEST_URL = 'http://localhost/sample.mp4' |
24 | ||
e028d0d1 JMF |
25 | |
26 | class YDL(FakeYDL): | |
f4d96df0 PH |
27 | def __init__(self, *args, **kwargs): |
28 | super(YDL, self).__init__(*args, **kwargs) | |
e028d0d1 | 29 | self.downloaded_info_dicts = [] |
f4d96df0 | 30 | self.msgs = [] |
5d254f77 | 31 | |
e028d0d1 | 32 | def process_info(self, info_dict): |
09b49e1f | 33 | self.downloaded_info_dicts.append(info_dict.copy()) |
e028d0d1 | 34 | |
f4d96df0 PH |
35 | def to_screen(self, msg): |
36 | self.msgs.append(msg) | |
37 | ||
b5ac45b1 | 38 | def dl(self, *args, **kwargs): |
39 | assert False, 'Downloader must not be invoked for test_YoutubeDL' | |
40 | ||
5d254f77 | 41 | |
3537b93d PH |
42 | def _make_result(formats, **kwargs): |
43 | res = { | |
44 | 'formats': formats, | |
45 | 'id': 'testid', | |
46 | 'title': 'testttitle', | |
47 | 'extractor': 'testex', | |
0396806f | 48 | 'extractor_key': 'TestEx', |
732044af | 49 | 'webpage_url': 'http://example.com/watch?v=shenanigans', |
3537b93d PH |
50 | } |
51 | res.update(**kwargs) | |
52 | return res | |
53 | ||
54 | ||
e028d0d1 JMF |
55 | class TestFormatSelection(unittest.TestCase): |
56 | def test_prefer_free_formats(self): | |
57 | # Same resolution => download webm | |
58 | ydl = YDL() | |
59 | ydl.params['prefer_free_formats'] = True | |
5d254f77 | 60 | formats = [ |
8508557e JMF |
61 | {'ext': 'webm', 'height': 460, 'url': TEST_URL}, |
62 | {'ext': 'mp4', 'height': 460, 'url': TEST_URL}, | |
5d254f77 | 63 | ] |
3537b93d | 64 | info_dict = _make_result(formats) |
3d4a70b8 PH |
65 | yie = YoutubeIE(ydl) |
66 | yie._sort_formats(info_dict['formats']) | |
e028d0d1 JMF |
67 | ydl.process_ie_result(info_dict) |
68 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 69 | self.assertEqual(downloaded['ext'], 'webm') |
e028d0d1 JMF |
70 | |
71 | # Different resolution => download best quality (mp4) | |
72 | ydl = YDL() | |
73 | ydl.params['prefer_free_formats'] = True | |
5d254f77 | 74 | formats = [ |
8508557e JMF |
75 | {'ext': 'webm', 'height': 720, 'url': TEST_URL}, |
76 | {'ext': 'mp4', 'height': 1080, 'url': TEST_URL}, | |
5d254f77 | 77 | ] |
89087418 | 78 | info_dict['formats'] = formats |
3d4a70b8 PH |
79 | yie = YoutubeIE(ydl) |
80 | yie._sort_formats(info_dict['formats']) | |
e028d0d1 JMF |
81 | ydl.process_ie_result(info_dict) |
82 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 83 | self.assertEqual(downloaded['ext'], 'mp4') |
e028d0d1 | 84 | |
5d0c5371 | 85 | # No prefer_free_formats => prefer mp4 and webm |
e028d0d1 JMF |
86 | ydl = YDL() |
87 | ydl.params['prefer_free_formats'] = False | |
5d254f77 | 88 | formats = [ |
8508557e JMF |
89 | {'ext': 'webm', 'height': 720, 'url': TEST_URL}, |
90 | {'ext': 'mp4', 'height': 720, 'url': TEST_URL}, | |
91 | {'ext': 'flv', 'height': 720, 'url': TEST_URL}, | |
5d254f77 | 92 | ] |
89087418 | 93 | info_dict['formats'] = formats |
3d4a70b8 PH |
94 | yie = YoutubeIE(ydl) |
95 | yie._sort_formats(info_dict['formats']) | |
96 | ydl.process_ie_result(info_dict) | |
97 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 98 | self.assertEqual(downloaded['ext'], 'mp4') |
3d4a70b8 PH |
99 | |
100 | ydl = YDL() | |
101 | ydl.params['prefer_free_formats'] = False | |
102 | formats = [ | |
8508557e JMF |
103 | {'ext': 'flv', 'height': 720, 'url': TEST_URL}, |
104 | {'ext': 'webm', 'height': 720, 'url': TEST_URL}, | |
3d4a70b8 | 105 | ] |
89087418 | 106 | info_dict['formats'] = formats |
3d4a70b8 PH |
107 | yie = YoutubeIE(ydl) |
108 | yie._sort_formats(info_dict['formats']) | |
e028d0d1 JMF |
109 | ydl.process_ie_result(info_dict) |
110 | downloaded = ydl.downloaded_info_dicts[0] | |
5d0c5371 | 111 | self.assertEqual(downloaded['ext'], 'webm') |
e028d0d1 | 112 | |
a9c58ad9 JMF |
113 | def test_format_selection(self): |
114 | formats = [ | |
8508557e | 115 | {'format_id': '35', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL}, |
232541df | 116 | {'format_id': 'example-with-dashes', 'ext': 'webm', 'preference': 1, 'url': TEST_URL}, |
8508557e JMF |
117 | {'format_id': '45', 'ext': 'webm', 'preference': 2, 'url': TEST_URL}, |
118 | {'format_id': '47', 'ext': 'webm', 'preference': 3, 'url': TEST_URL}, | |
119 | {'format_id': '2', 'ext': 'flv', 'preference': 4, 'url': TEST_URL}, | |
a9c58ad9 | 120 | ] |
3537b93d | 121 | info_dict = _make_result(formats) |
a9c58ad9 | 122 | |
b5ac45b1 | 123 | def test(inp, *expected, multi=False): |
124 | ydl = YDL({ | |
125 | 'format': inp, | |
126 | 'allow_multiple_video_streams': multi, | |
127 | 'allow_multiple_audio_streams': multi, | |
128 | }) | |
129 | ydl.process_ie_result(info_dict.copy()) | |
130 | downloaded = map(lambda x: x['format_id'], ydl.downloaded_info_dicts) | |
131 | self.assertEqual(list(downloaded), list(expected)) | |
132 | ||
133 | test('20/47', '47') | |
134 | test('20/71/worst', '35') | |
135 | test(None, '2') | |
136 | test('webm/mp4', '47') | |
137 | test('3gp/40/mp4', '35') | |
138 | test('example-with-dashes', 'example-with-dashes') | |
dd2a987d | 139 | test('all', '2', '47', '45', 'example-with-dashes', '35') |
b5ac45b1 | 140 | test('mergeall', '2+47+45+example-with-dashes+35', multi=True) |
232541df | 141 | |
ba7678f9 PH |
142 | def test_format_selection_audio(self): |
143 | formats = [ | |
8508557e JMF |
144 | {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL}, |
145 | {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none', 'url': TEST_URL}, | |
146 | {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none', 'url': TEST_URL}, | |
147 | {'format_id': 'vid', 'ext': 'mp4', 'preference': 4, 'url': TEST_URL}, | |
ba7678f9 | 148 | ] |
3537b93d | 149 | info_dict = _make_result(formats) |
ba7678f9 | 150 | |
89087418 | 151 | ydl = YDL({'format': 'bestaudio'}) |
ba7678f9 PH |
152 | ydl.process_ie_result(info_dict.copy()) |
153 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 154 | self.assertEqual(downloaded['format_id'], 'audio-high') |
ba7678f9 | 155 | |
89087418 | 156 | ydl = YDL({'format': 'worstaudio'}) |
ba7678f9 PH |
157 | ydl.process_ie_result(info_dict.copy()) |
158 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 159 | self.assertEqual(downloaded['format_id'], 'audio-low') |
ba7678f9 PH |
160 | |
161 | formats = [ | |
8508557e JMF |
162 | {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL}, |
163 | {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': TEST_URL}, | |
ba7678f9 | 164 | ] |
3537b93d | 165 | info_dict = _make_result(formats) |
ba7678f9 | 166 | |
89087418 | 167 | ydl = YDL({'format': 'bestaudio/worstaudio/best'}) |
ba7678f9 PH |
168 | ydl.process_ie_result(info_dict.copy()) |
169 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 170 | self.assertEqual(downloaded['format_id'], 'vid-high') |
ba7678f9 | 171 | |
0217c783 PH |
172 | def test_format_selection_audio_exts(self): |
173 | formats = [ | |
174 | {'format_id': 'mp3-64', 'ext': 'mp3', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, | |
175 | {'format_id': 'ogg-64', 'ext': 'ogg', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, | |
176 | {'format_id': 'aac-64', 'ext': 'aac', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, | |
177 | {'format_id': 'mp3-32', 'ext': 'mp3', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'}, | |
178 | {'format_id': 'aac-32', 'ext': 'aac', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'}, | |
179 | ] | |
180 | ||
181 | info_dict = _make_result(formats) | |
182 | ydl = YDL({'format': 'best'}) | |
183 | ie = YoutubeIE(ydl) | |
184 | ie._sort_formats(info_dict['formats']) | |
185 | ydl.process_ie_result(copy.deepcopy(info_dict)) | |
186 | downloaded = ydl.downloaded_info_dicts[0] | |
187 | self.assertEqual(downloaded['format_id'], 'aac-64') | |
188 | ||
189 | ydl = YDL({'format': 'mp3'}) | |
190 | ie = YoutubeIE(ydl) | |
191 | ie._sort_formats(info_dict['formats']) | |
192 | ydl.process_ie_result(copy.deepcopy(info_dict)) | |
193 | downloaded = ydl.downloaded_info_dicts[0] | |
194 | self.assertEqual(downloaded['format_id'], 'mp3-64') | |
195 | ||
196 | ydl = YDL({'prefer_free_formats': True}) | |
197 | ie = YoutubeIE(ydl) | |
198 | ie._sort_formats(info_dict['formats']) | |
199 | ydl.process_ie_result(copy.deepcopy(info_dict)) | |
200 | downloaded = ydl.downloaded_info_dicts[0] | |
201 | self.assertEqual(downloaded['format_id'], 'ogg-64') | |
202 | ||
bc6d5978 JMF |
203 | def test_format_selection_video(self): |
204 | formats = [ | |
8508557e JMF |
205 | {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none', 'url': TEST_URL}, |
206 | {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none', 'url': TEST_URL}, | |
207 | {'format_id': 'vid', 'ext': 'mp4', 'preference': 3, 'url': TEST_URL}, | |
bc6d5978 | 208 | ] |
3537b93d | 209 | info_dict = _make_result(formats) |
bc6d5978 JMF |
210 | |
211 | ydl = YDL({'format': 'bestvideo'}) | |
212 | ydl.process_ie_result(info_dict.copy()) | |
213 | downloaded = ydl.downloaded_info_dicts[0] | |
214 | self.assertEqual(downloaded['format_id'], 'dash-video-high') | |
215 | ||
216 | ydl = YDL({'format': 'worstvideo'}) | |
217 | ydl.process_ie_result(info_dict.copy()) | |
218 | downloaded = ydl.downloaded_info_dicts[0] | |
219 | self.assertEqual(downloaded['format_id'], 'dash-video-low') | |
220 | ||
4c3b16d5 S |
221 | ydl = YDL({'format': 'bestvideo[format_id^=dash][format_id$=low]'}) |
222 | ydl.process_ie_result(info_dict.copy()) | |
223 | downloaded = ydl.downloaded_info_dicts[0] | |
224 | self.assertEqual(downloaded['format_id'], 'dash-video-low') | |
225 | ||
b913348d | 226 | formats = [ |
227 | {'format_id': 'vid-vcodec-dot', 'ext': 'mp4', 'preference': 1, 'vcodec': 'avc1.123456', 'acodec': 'none', 'url': TEST_URL}, | |
228 | ] | |
229 | info_dict = _make_result(formats) | |
230 | ||
231 | ydl = YDL({'format': 'bestvideo[vcodec=avc1.123456]'}) | |
232 | ydl.process_ie_result(info_dict.copy()) | |
233 | downloaded = ydl.downloaded_info_dicts[0] | |
234 | self.assertEqual(downloaded['format_id'], 'vid-vcodec-dot') | |
235 | ||
2cc779f4 S |
236 | def test_format_selection_string_ops(self): |
237 | formats = [ | |
238 | {'format_id': 'abc-cba', 'ext': 'mp4', 'url': TEST_URL}, | |
e118a879 | 239 | {'format_id': 'zxc-cxz', 'ext': 'webm', 'url': TEST_URL}, |
2cc779f4 S |
240 | ] |
241 | info_dict = _make_result(formats) | |
242 | ||
243 | # equals (=) | |
244 | ydl = YDL({'format': '[format_id=abc-cba]'}) | |
245 | ydl.process_ie_result(info_dict.copy()) | |
246 | downloaded = ydl.downloaded_info_dicts[0] | |
247 | self.assertEqual(downloaded['format_id'], 'abc-cba') | |
248 | ||
249 | # does not equal (!=) | |
250 | ydl = YDL({'format': '[format_id!=abc-cba]'}) | |
e118a879 S |
251 | ydl.process_ie_result(info_dict.copy()) |
252 | downloaded = ydl.downloaded_info_dicts[0] | |
253 | self.assertEqual(downloaded['format_id'], 'zxc-cxz') | |
254 | ||
255 | ydl = YDL({'format': '[format_id!=abc-cba][format_id!=zxc-cxz]'}) | |
2cc779f4 S |
256 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) |
257 | ||
258 | # starts with (^=) | |
259 | ydl = YDL({'format': '[format_id^=abc]'}) | |
260 | ydl.process_ie_result(info_dict.copy()) | |
261 | downloaded = ydl.downloaded_info_dicts[0] | |
262 | self.assertEqual(downloaded['format_id'], 'abc-cba') | |
263 | ||
264 | # does not start with (!^=) | |
e118a879 S |
265 | ydl = YDL({'format': '[format_id!^=abc]'}) |
266 | ydl.process_ie_result(info_dict.copy()) | |
267 | downloaded = ydl.downloaded_info_dicts[0] | |
268 | self.assertEqual(downloaded['format_id'], 'zxc-cxz') | |
269 | ||
270 | ydl = YDL({'format': '[format_id!^=abc][format_id!^=zxc]'}) | |
2cc779f4 S |
271 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) |
272 | ||
273 | # ends with ($=) | |
274 | ydl = YDL({'format': '[format_id$=cba]'}) | |
275 | ydl.process_ie_result(info_dict.copy()) | |
276 | downloaded = ydl.downloaded_info_dicts[0] | |
277 | self.assertEqual(downloaded['format_id'], 'abc-cba') | |
278 | ||
279 | # does not end with (!$=) | |
e118a879 S |
280 | ydl = YDL({'format': '[format_id!$=cba]'}) |
281 | ydl.process_ie_result(info_dict.copy()) | |
282 | downloaded = ydl.downloaded_info_dicts[0] | |
283 | self.assertEqual(downloaded['format_id'], 'zxc-cxz') | |
284 | ||
285 | ydl = YDL({'format': '[format_id!$=cba][format_id!$=cxz]'}) | |
2cc779f4 S |
286 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) |
287 | ||
288 | # contains (*=) | |
e118a879 | 289 | ydl = YDL({'format': '[format_id*=bc-cb]'}) |
2cc779f4 S |
290 | ydl.process_ie_result(info_dict.copy()) |
291 | downloaded = ydl.downloaded_info_dicts[0] | |
292 | self.assertEqual(downloaded['format_id'], 'abc-cba') | |
293 | ||
294 | # does not contain (!*=) | |
e118a879 S |
295 | ydl = YDL({'format': '[format_id!*=bc-cb]'}) |
296 | ydl.process_ie_result(info_dict.copy()) | |
297 | downloaded = ydl.downloaded_info_dicts[0] | |
298 | self.assertEqual(downloaded['format_id'], 'zxc-cxz') | |
299 | ||
300 | ydl = YDL({'format': '[format_id!*=abc][format_id!*=zxc]'}) | |
301 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) | |
302 | ||
2cc779f4 S |
303 | ydl = YDL({'format': '[format_id!*=-]'}) |
304 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) | |
305 | ||
3d4a70b8 | 306 | def test_youtube_format_selection(self): |
a7191c6f | 307 | # FIXME: Rewrite in accordance with the new format sorting options |
5d0c5371 | 308 | return |
5d0c5371 | 309 | |
3d4a70b8 | 310 | order = [ |
86bf2905 | 311 | '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '17', '36', '13', |
3d4a70b8 PH |
312 | # Apple HTTP Live Streaming |
313 | '96', '95', '94', '93', '92', '132', '151', | |
314 | # 3D | |
315 | '85', '84', '102', '83', '101', '82', '100', | |
316 | # Dash video | |
c11125f9 | 317 | '137', '248', '136', '247', '135', '246', |
3d4a70b8 PH |
318 | '245', '244', '134', '243', '133', '242', '160', |
319 | # Dash audio | |
a053c349 | 320 | '141', '172', '140', '171', '139', |
3d4a70b8 PH |
321 | ] |
322 | ||
67134eab JMF |
323 | def format_info(f_id): |
324 | info = YoutubeIE._formats[f_id].copy() | |
1df41411 | 325 | |
91cb6b50 | 326 | # XXX: In real cases InfoExtractor._parse_mpd_formats() fills up 'acodec' |
1df41411 YCH |
327 | # and 'vcodec', while in tests such information is incomplete since |
328 | # commit a6c2c24479e5f4827ceb06f64d855329c0a6f593 | |
329 | # test_YoutubeDL.test_youtube_format_selection is broken without | |
330 | # this fix | |
331 | if 'acodec' in info and 'vcodec' not in info: | |
332 | info['vcodec'] = 'none' | |
333 | elif 'vcodec' in info and 'acodec' not in info: | |
334 | info['acodec'] = 'none' | |
335 | ||
67134eab JMF |
336 | info['format_id'] = f_id |
337 | info['url'] = 'url:' + f_id | |
338 | return info | |
339 | formats_order = [format_info(f_id) for f_id in order] | |
340 | ||
341 | info_dict = _make_result(list(formats_order), extractor='youtube') | |
342 | ydl = YDL({'format': 'bestvideo+bestaudio'}) | |
343 | yie = YoutubeIE(ydl) | |
344 | yie._sort_formats(info_dict['formats']) | |
345 | ydl.process_ie_result(info_dict) | |
346 | downloaded = ydl.downloaded_info_dicts[0] | |
5d0c5371 | 347 | self.assertEqual(downloaded['format_id'], '248+172') |
67134eab | 348 | self.assertEqual(downloaded['ext'], 'mp4') |
3d4a70b8 | 349 | |
cf2ac6df JMF |
350 | info_dict = _make_result(list(formats_order), extractor='youtube') |
351 | ydl = YDL({'format': 'bestvideo[height>=999999]+bestaudio/best'}) | |
352 | yie = YoutubeIE(ydl) | |
353 | yie._sort_formats(info_dict['formats']) | |
354 | ydl.process_ie_result(info_dict) | |
355 | downloaded = ydl.downloaded_info_dicts[0] | |
356 | self.assertEqual(downloaded['format_id'], '38') | |
357 | ||
f5f4a27a JMF |
358 | info_dict = _make_result(list(formats_order), extractor='youtube') |
359 | ydl = YDL({'format': 'bestvideo/best,bestaudio'}) | |
360 | yie = YoutubeIE(ydl) | |
361 | yie._sort_formats(info_dict['formats']) | |
362 | ydl.process_ie_result(info_dict) | |
363 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
364 | self.assertEqual(downloaded_ids, ['137', '141']) | |
365 | ||
0130afb7 JMF |
366 | info_dict = _make_result(list(formats_order), extractor='youtube') |
367 | ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])+bestaudio'}) | |
368 | yie = YoutubeIE(ydl) | |
369 | yie._sort_formats(info_dict['formats']) | |
370 | ydl.process_ie_result(info_dict) | |
371 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
372 | self.assertEqual(downloaded_ids, ['137+141', '248+141']) | |
373 | ||
374 | info_dict = _make_result(list(formats_order), extractor='youtube') | |
375 | ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])[height<=720]+bestaudio'}) | |
376 | yie = YoutubeIE(ydl) | |
377 | yie._sort_formats(info_dict['formats']) | |
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, ['136+141', '247+141']) | |
381 | ||
382 | info_dict = _make_result(list(formats_order), extractor='youtube') | |
383 | ydl = YDL({'format': '(bestvideo[ext=none]/bestvideo[ext=webm])+bestaudio'}) | |
384 | yie = YoutubeIE(ydl) | |
385 | yie._sort_formats(info_dict['formats']) | |
386 | ydl.process_ie_result(info_dict) | |
387 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
388 | self.assertEqual(downloaded_ids, ['248+141']) | |
389 | ||
67134eab | 390 | for f1, f2 in zip(formats_order, formats_order[1:]): |
3537b93d | 391 | info_dict = _make_result([f1, f2], extractor='youtube') |
8dd54188 | 392 | ydl = YDL({'format': 'best/bestvideo'}) |
3d4a70b8 PH |
393 | yie = YoutubeIE(ydl) |
394 | yie._sort_formats(info_dict['formats']) | |
395 | ydl.process_ie_result(info_dict) | |
396 | downloaded = ydl.downloaded_info_dicts[0] | |
67134eab | 397 | self.assertEqual(downloaded['format_id'], f1['format_id']) |
3d4a70b8 | 398 | |
3537b93d | 399 | info_dict = _make_result([f2, f1], extractor='youtube') |
8dd54188 | 400 | ydl = YDL({'format': 'best/bestvideo'}) |
3d4a70b8 PH |
401 | yie = YoutubeIE(ydl) |
402 | yie._sort_formats(info_dict['formats']) | |
403 | ydl.process_ie_result(info_dict) | |
404 | downloaded = ydl.downloaded_info_dicts[0] | |
67134eab | 405 | self.assertEqual(downloaded['format_id'], f1['format_id']) |
3d4a70b8 | 406 | |
317f7ab6 S |
407 | def test_audio_only_extractor_format_selection(self): |
408 | # For extractors with incomplete formats (all formats are audio-only or | |
409 | # video-only) best and worst should fallback to corresponding best/worst | |
410 | # video-only or audio-only formats (as per | |
067aa17e | 411 | # https://github.com/ytdl-org/youtube-dl/pull/5556) |
317f7ab6 S |
412 | formats = [ |
413 | {'format_id': 'low', 'ext': 'mp3', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL}, | |
414 | {'format_id': 'high', 'ext': 'mp3', 'preference': 2, 'vcodec': 'none', 'url': TEST_URL}, | |
415 | ] | |
416 | info_dict = _make_result(formats) | |
417 | ||
418 | ydl = YDL({'format': 'best'}) | |
419 | ydl.process_ie_result(info_dict.copy()) | |
420 | downloaded = ydl.downloaded_info_dicts[0] | |
421 | self.assertEqual(downloaded['format_id'], 'high') | |
422 | ||
423 | ydl = YDL({'format': 'worst'}) | |
424 | ydl.process_ie_result(info_dict.copy()) | |
425 | downloaded = ydl.downloaded_info_dicts[0] | |
426 | self.assertEqual(downloaded['format_id'], 'low') | |
427 | ||
428 | def test_format_not_available(self): | |
429 | formats = [ | |
430 | {'format_id': 'regular', 'ext': 'mp4', 'height': 360, 'url': TEST_URL}, | |
431 | {'format_id': 'video', 'ext': 'mp4', 'height': 720, 'acodec': 'none', 'url': TEST_URL}, | |
432 | ] | |
433 | info_dict = _make_result(formats) | |
434 | ||
435 | # This must fail since complete video-audio format does not match filter | |
436 | # and extractor does not provide incomplete only formats (i.e. only | |
437 | # video-only or audio-only). | |
438 | ydl = YDL({'format': 'best[height>360]'}) | |
439 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) | |
440 | ||
8cda78ef | 441 | def test_format_selection_issue_10083(self): |
067aa17e | 442 | # See https://github.com/ytdl-org/youtube-dl/issues/10083 |
8cda78ef S |
443 | formats = [ |
444 | {'format_id': 'regular', 'height': 360, 'url': TEST_URL}, | |
445 | {'format_id': 'video', 'height': 720, 'acodec': 'none', 'url': TEST_URL}, | |
446 | {'format_id': 'audio', 'vcodec': 'none', 'url': TEST_URL}, | |
447 | ] | |
448 | info_dict = _make_result(formats) | |
449 | ||
450 | ydl = YDL({'format': 'best[height>360]/bestvideo[height>360]+bestaudio'}) | |
451 | ydl.process_ie_result(info_dict.copy()) | |
452 | self.assertEqual(ydl.downloaded_info_dicts[0]['format_id'], 'video+audio') | |
453 | ||
0a31a350 JMF |
454 | def test_invalid_format_specs(self): |
455 | def assert_syntax_error(format_spec): | |
187986a8 | 456 | self.assertRaises(SyntaxError, YDL, {'format': format_spec}) |
0a31a350 JMF |
457 | |
458 | assert_syntax_error('bestvideo,,best') | |
459 | assert_syntax_error('+bestaudio') | |
460 | assert_syntax_error('bestvideo+') | |
d96d604e | 461 | assert_syntax_error('/') |
187986a8 | 462 | assert_syntax_error('[720<height]') |
0a31a350 | 463 | |
083c9df9 PH |
464 | def test_format_filtering(self): |
465 | formats = [ | |
466 | {'format_id': 'A', 'filesize': 500, 'width': 1000}, | |
467 | {'format_id': 'B', 'filesize': 1000, 'width': 500}, | |
468 | {'format_id': 'C', 'filesize': 1000, 'width': 400}, | |
469 | {'format_id': 'D', 'filesize': 2000, 'width': 600}, | |
470 | {'format_id': 'E', 'filesize': 3000}, | |
471 | {'format_id': 'F'}, | |
472 | {'format_id': 'G', 'filesize': 1000000}, | |
473 | ] | |
474 | for f in formats: | |
475 | f['url'] = 'http://_/' | |
476 | f['ext'] = 'unknown' | |
477 | info_dict = _make_result(formats) | |
478 | ||
479 | ydl = YDL({'format': 'best[filesize<3000]'}) | |
480 | ydl.process_ie_result(info_dict) | |
481 | downloaded = ydl.downloaded_info_dicts[0] | |
482 | self.assertEqual(downloaded['format_id'], 'D') | |
483 | ||
484 | ydl = YDL({'format': 'best[filesize<=3000]'}) | |
485 | ydl.process_ie_result(info_dict) | |
486 | downloaded = ydl.downloaded_info_dicts[0] | |
487 | self.assertEqual(downloaded['format_id'], 'E') | |
488 | ||
489 | ydl = YDL({'format': 'best[filesize <= ? 3000]'}) | |
490 | ydl.process_ie_result(info_dict) | |
491 | downloaded = ydl.downloaded_info_dicts[0] | |
492 | self.assertEqual(downloaded['format_id'], 'F') | |
493 | ||
494 | ydl = YDL({'format': 'best [filesize = 1000] [width>450]'}) | |
495 | ydl.process_ie_result(info_dict) | |
496 | downloaded = ydl.downloaded_info_dicts[0] | |
497 | self.assertEqual(downloaded['format_id'], 'B') | |
498 | ||
499 | ydl = YDL({'format': 'best [filesize = 1000] [width!=450]'}) | |
500 | ydl.process_ie_result(info_dict) | |
501 | downloaded = ydl.downloaded_info_dicts[0] | |
502 | self.assertEqual(downloaded['format_id'], 'C') | |
503 | ||
504 | ydl = YDL({'format': '[filesize>?1]'}) | |
505 | ydl.process_ie_result(info_dict) | |
506 | downloaded = ydl.downloaded_info_dicts[0] | |
507 | self.assertEqual(downloaded['format_id'], 'G') | |
508 | ||
509 | ydl = YDL({'format': '[filesize<1M]'}) | |
510 | ydl.process_ie_result(info_dict) | |
511 | downloaded = ydl.downloaded_info_dicts[0] | |
512 | self.assertEqual(downloaded['format_id'], 'E') | |
513 | ||
514 | ydl = YDL({'format': '[filesize<1MiB]'}) | |
515 | ydl.process_ie_result(info_dict) | |
516 | downloaded = ydl.downloaded_info_dicts[0] | |
517 | self.assertEqual(downloaded['format_id'], 'G') | |
518 | ||
5acfa126 JMF |
519 | ydl = YDL({'format': 'all[width>=400][width<=600]'}) |
520 | ydl.process_ie_result(info_dict) | |
521 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
dd2a987d | 522 | self.assertEqual(downloaded_ids, ['D', 'C', 'B']) |
5acfa126 | 523 | |
bb8e5536 JMF |
524 | ydl = YDL({'format': 'best[height<40]'}) |
525 | try: | |
526 | ydl.process_ie_result(info_dict) | |
527 | except ExtractorError: | |
528 | pass | |
529 | self.assertEqual(ydl.downloaded_info_dicts, []) | |
530 | ||
0017d9ad S |
531 | def test_default_format_spec(self): |
532 | ydl = YDL({'simulate': True}) | |
5d0c5371 | 533 | self.assertEqual(ydl._default_format_spec({}), 'bestvideo*+bestaudio/best') |
0017d9ad | 534 | |
d08dcd2d S |
535 | ydl = YDL({}) |
536 | self.assertEqual(ydl._default_format_spec({'is_live': True}), 'best/bestvideo+bestaudio') | |
af0f7428 | 537 | |
d08dcd2d | 538 | ydl = YDL({'simulate': True}) |
5d0c5371 | 539 | self.assertEqual(ydl._default_format_spec({'is_live': True}), 'bestvideo*+bestaudio/best') |
af0f7428 | 540 | |
0017d9ad | 541 | ydl = YDL({'outtmpl': '-'}) |
af0f7428 | 542 | self.assertEqual(ydl._default_format_spec({}), 'best/bestvideo+bestaudio') |
0017d9ad S |
543 | |
544 | ydl = YDL({}) | |
5d0c5371 | 545 | self.assertEqual(ydl._default_format_spec({}, download=False), 'bestvideo*+bestaudio/best') |
af0f7428 | 546 | self.assertEqual(ydl._default_format_spec({'is_live': True}), 'best/bestvideo+bestaudio') |
0017d9ad | 547 | |
f20bf146 JMF |
548 | |
549 | class TestYoutubeDL(unittest.TestCase): | |
ab84349b JMF |
550 | def test_subtitles(self): |
551 | def s_formats(lang, autocaption=False): | |
552 | return [{ | |
553 | 'ext': ext, | |
554 | 'url': 'http://localhost/video.%s.%s' % (lang, ext), | |
555 | '_auto': autocaption, | |
556 | } for ext in ['vtt', 'srt', 'ass']] | |
557 | subtitles = dict((l, s_formats(l)) for l in ['en', 'fr', 'es']) | |
558 | auto_captions = dict((l, s_formats(l, True)) for l in ['it', 'pt', 'es']) | |
559 | info_dict = { | |
560 | 'id': 'test', | |
561 | 'title': 'Test', | |
562 | 'url': 'http://localhost/video.mp4', | |
563 | 'subtitles': subtitles, | |
564 | 'automatic_captions': auto_captions, | |
565 | 'extractor': 'TEST', | |
732044af | 566 | 'webpage_url': 'http://example.com/watch?v=shenanigans', |
ab84349b JMF |
567 | } |
568 | ||
569 | def get_info(params={}): | |
570 | params.setdefault('simulate', True) | |
571 | ydl = YDL(params) | |
572 | ydl.report_warning = lambda *args, **kargs: None | |
573 | return ydl.process_video_result(info_dict, download=False) | |
574 | ||
575 | result = get_info() | |
576 | self.assertFalse(result.get('requested_subtitles')) | |
577 | self.assertEqual(result['subtitles'], subtitles) | |
578 | self.assertEqual(result['automatic_captions'], auto_captions) | |
579 | ||
580 | result = get_info({'writesubtitles': True}) | |
581 | subs = result['requested_subtitles'] | |
582 | self.assertTrue(subs) | |
583 | self.assertEqual(set(subs.keys()), set(['en'])) | |
584 | self.assertTrue(subs['en'].get('data') is None) | |
585 | self.assertEqual(subs['en']['ext'], 'ass') | |
586 | ||
587 | result = get_info({'writesubtitles': True, 'subtitlesformat': 'foo/srt'}) | |
588 | subs = result['requested_subtitles'] | |
589 | self.assertEqual(subs['en']['ext'], 'srt') | |
590 | ||
591 | result = get_info({'writesubtitles': True, 'subtitleslangs': ['es', 'fr', 'it']}) | |
592 | subs = result['requested_subtitles'] | |
593 | self.assertTrue(subs) | |
594 | self.assertEqual(set(subs.keys()), set(['es', 'fr'])) | |
595 | ||
c32b0aab | 596 | result = get_info({'writesubtitles': True, 'subtitleslangs': ['all', '-en']}) |
597 | subs = result['requested_subtitles'] | |
598 | self.assertTrue(subs) | |
599 | self.assertEqual(set(subs.keys()), set(['es', 'fr'])) | |
600 | ||
601 | result = get_info({'writesubtitles': True, 'subtitleslangs': ['en', 'fr', '-en']}) | |
602 | subs = result['requested_subtitles'] | |
603 | self.assertTrue(subs) | |
604 | self.assertEqual(set(subs.keys()), set(['fr'])) | |
605 | ||
606 | result = get_info({'writesubtitles': True, 'subtitleslangs': ['-en', 'en']}) | |
607 | subs = result['requested_subtitles'] | |
608 | self.assertTrue(subs) | |
609 | self.assertEqual(set(subs.keys()), set(['en'])) | |
610 | ||
611 | result = get_info({'writesubtitles': True, 'subtitleslangs': ['e.+']}) | |
612 | subs = result['requested_subtitles'] | |
613 | self.assertTrue(subs) | |
614 | self.assertEqual(set(subs.keys()), set(['es', 'en'])) | |
615 | ||
ab84349b JMF |
616 | result = get_info({'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']}) |
617 | subs = result['requested_subtitles'] | |
618 | self.assertTrue(subs) | |
619 | self.assertEqual(set(subs.keys()), set(['es', 'pt'])) | |
620 | self.assertFalse(subs['es']['_auto']) | |
621 | self.assertTrue(subs['pt']['_auto']) | |
622 | ||
98c70d6f JMF |
623 | result = get_info({'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']}) |
624 | subs = result['requested_subtitles'] | |
625 | self.assertTrue(subs) | |
626 | self.assertEqual(set(subs.keys()), set(['es', 'pt'])) | |
627 | self.assertTrue(subs['es']['_auto']) | |
628 | self.assertTrue(subs['pt']['_auto']) | |
629 | ||
b6c45014 JMF |
630 | def test_add_extra_info(self): |
631 | test_dict = { | |
632 | 'extractor': 'Foo', | |
633 | } | |
634 | extra_info = { | |
635 | 'extractor': 'Bar', | |
636 | 'playlist': 'funny videos', | |
637 | } | |
638 | YDL.add_extra_info(test_dict, extra_info) | |
639 | self.assertEqual(test_dict['extractor'], 'Foo') | |
640 | self.assertEqual(test_dict['playlist'], 'funny videos') | |
641 | ||
752cda38 | 642 | outtmpl_info = { |
643 | 'id': '1234', | |
644 | 'ext': 'mp4', | |
645 | 'width': None, | |
646 | 'height': 1080, | |
abbeeebc | 647 | 'filesize': 1024, |
752cda38 | 648 | 'title1': '$PATH', |
649 | 'title2': '%PATH%', | |
87ea7dfc | 650 | 'title3': 'foo/bar\\test', |
7d1eb38a | 651 | 'title4': 'foo "bar" test', |
524e2e4f | 652 | 'title5': 'áéí 𝐀', |
752cda38 | 653 | 'timestamp': 1618488000, |
654 | 'duration': 100000, | |
655 | 'playlist_index': 1, | |
e6f21b3d | 656 | 'playlist_autonumber': 2, |
752cda38 | 657 | '_last_playlist_index': 100, |
658 | 'n_entries': 10, | |
4476d2c7 | 659 | 'formats': [{'id': 'id 1'}, {'id': 'id 2'}, {'id': 'id 3'}] |
752cda38 | 660 | } |
661 | ||
5c6542ce | 662 | def test_prepare_outtmpl_and_filename(self): |
9fea350f | 663 | def test(tmpl, expected, *, info=None, **params): |
752cda38 | 664 | params['outtmpl'] = tmpl |
665 | ydl = YoutubeDL(params) | |
666 | ydl._num_downloads = 1 | |
5c6542ce | 667 | self.assertEqual(ydl.validate_outtmpl(tmpl), None) |
752cda38 | 668 | |
819e0531 | 669 | out = ydl.evaluate_outtmpl(tmpl, info or self.outtmpl_info) |
9fea350f | 670 | fname = ydl.prepare_filename(info or self.outtmpl_info) |
5c6542ce | 671 | |
2b8a2973 | 672 | if not isinstance(expected, (list, tuple)): |
673 | expected = (expected, expected) | |
674 | for (name, got), expect in zip((('outtmpl', out), ('filename', fname)), expected): | |
675 | if callable(expect): | |
676 | self.assertTrue(expect(got), f'Wrong {name} from {tmpl}') | |
677 | else: | |
678 | self.assertEqual(got, expect, f'Wrong {name} from {tmpl}') | |
5c6542ce | 679 | |
6e84b215 | 680 | # Side-effects |
681 | original_infodict = dict(self.outtmpl_info) | |
682 | test('foo.bar', 'foo.bar') | |
683 | original_infodict['epoch'] = self.outtmpl_info.get('epoch') | |
684 | self.assertTrue(isinstance(original_infodict['epoch'], int)) | |
685 | test('%(epoch)d', int_or_none) | |
686 | self.assertEqual(original_infodict, self.outtmpl_info) | |
687 | ||
5c6542ce | 688 | # Auto-generated fields |
689 | test('%(id)s.%(ext)s', '1234.mp4') | |
690 | test('%(duration_string)s', ('27:46:40', '27-46-40')) | |
5c6542ce | 691 | test('%(resolution)s', '1080p') |
692 | test('%(playlist_index)s', '001') | |
e6f21b3d | 693 | test('%(playlist_autonumber)s', '02') |
5c6542ce | 694 | test('%(autonumber)s', '00001') |
695 | test('%(autonumber+2)03d', '005', autonumber_start=3) | |
696 | test('%(autonumber)s', '001', autonumber_size=3) | |
697 | ||
698 | # Escaping % | |
901130bb | 699 | test('%', '%') |
5c6542ce | 700 | test('%%', '%') |
701 | test('%%%%', '%%') | |
901130bb | 702 | test('%s', '%s') |
703 | test('%%%s', '%%s') | |
704 | test('%d', '%d') | |
705 | test('%abc%', '%abc%') | |
5c6542ce | 706 | test('%%(width)06d.%(ext)s', '%(width)06d.mp4') |
901130bb | 707 | test('%%%(height)s', '%1080') |
5c6542ce | 708 | test('%(width)06d.%(ext)s', 'NA.mp4') |
709 | test('%(width)06d.%%(ext)s', 'NA.%(ext)s') | |
710 | test('%%(width)06d.%(ext)s', '%(width)06d.mp4') | |
711 | ||
9fea350f | 712 | # ID sanitization |
713 | test('%(id)s', '_abcd', info={'id': '_abcd'}) | |
714 | test('%(some_id)s', '_abcd', info={'some_id': '_abcd'}) | |
715 | test('%(formats.0.id)s', '_abcd', info={'formats': [{'id': '_abcd'}]}) | |
716 | test('%(id)s', '-abcd', info={'id': '-abcd'}) | |
717 | test('%(id)s', '.abcd', info={'id': '.abcd'}) | |
718 | test('%(id)s', 'ab__cd', info={'id': 'ab__cd'}) | |
719 | test('%(id)s', ('ab:cd', 'ab -cd'), info={'id': 'ab:cd'}) | |
e0fd9573 | 720 | test('%(id.0)s', '-', info={'id': '--'}) |
9fea350f | 721 | |
5c6542ce | 722 | # Invalid templates |
5c6542ce | 723 | self.assertTrue(isinstance(YoutubeDL.validate_outtmpl('%(title)'), ValueError)) |
724 | test('%(invalid@tmpl|def)s', 'none', outtmpl_na_placeholder='none') | |
2b8a2973 | 725 | test('%(..)s', 'NA') |
726 | ||
727 | # Entire info_dict | |
728 | def expect_same_infodict(out): | |
729 | got_dict = json.loads(out) | |
730 | for info_field, expected in self.outtmpl_info.items(): | |
731 | self.assertEqual(got_dict.get(info_field), expected, info_field) | |
732 | return True | |
733 | ||
734 | test('%()j', (expect_same_infodict, str)) | |
5c6542ce | 735 | |
736 | # NA placeholder | |
752cda38 | 737 | NA_TEST_OUTTMPL = '%(uploader_date)s-%(width)d-%(x|def)s-%(id)s.%(ext)s' |
5c6542ce | 738 | test(NA_TEST_OUTTMPL, 'NA-NA-def-1234.mp4') |
739 | test(NA_TEST_OUTTMPL, 'none-none-def-1234.mp4', outtmpl_na_placeholder='none') | |
740 | test(NA_TEST_OUTTMPL, '--def-1234.mp4', outtmpl_na_placeholder='') | |
582fad70 | 741 | test('%(non_existent.0)s', 'NA') |
752cda38 | 742 | |
5c6542ce | 743 | # String formatting |
752cda38 | 744 | FMT_TEST_OUTTMPL = '%%(height)%s.%%(ext)s' |
5c6542ce | 745 | test(FMT_TEST_OUTTMPL % 's', '1080.mp4') |
746 | test(FMT_TEST_OUTTMPL % 'd', '1080.mp4') | |
747 | test(FMT_TEST_OUTTMPL % '6d', ' 1080.mp4') | |
748 | test(FMT_TEST_OUTTMPL % '-6d', '1080 .mp4') | |
749 | test(FMT_TEST_OUTTMPL % '06d', '001080.mp4') | |
750 | test(FMT_TEST_OUTTMPL % ' 06d', ' 01080.mp4') | |
751 | test(FMT_TEST_OUTTMPL % ' 06d', ' 01080.mp4') | |
752 | test(FMT_TEST_OUTTMPL % '0 6d', ' 01080.mp4') | |
753 | test(FMT_TEST_OUTTMPL % '0 6d', ' 01080.mp4') | |
754 | test(FMT_TEST_OUTTMPL % ' 0 6d', ' 01080.mp4') | |
755 | ||
756 | # Type casting | |
757 | test('%(id)d', '1234') | |
758 | test('%(height)c', '1') | |
759 | test('%(ext)c', 'm') | |
760 | test('%(id)d %(id)r', "1234 '1234'") | |
761 | test('%(id)r %(height)r', "'1234' 1080") | |
762 | test('%(ext)s-%(ext|def)d', 'mp4-def') | |
763 | test('%(width|0)04d', '0000') | |
764 | test('a%(width|)d', 'a', outtmpl_na_placeholder='none') | |
765 | ||
752cda38 | 766 | FORMATS = self.outtmpl_info['formats'] |
4476d2c7 | 767 | sanitize = lambda x: x.replace(':', ' -').replace('"', "'").replace('\n', ' ') |
7d1eb38a | 768 | |
769 | # Custom type casting | |
4476d2c7 | 770 | test('%(formats.:.id)l', 'id 1, id 2, id 3') |
771 | test('%(formats.:.id)#l', ('id 1\nid 2\nid 3', 'id 1 id 2 id 3')) | |
7d1eb38a | 772 | test('%(ext)l', 'mp4') |
4476d2c7 | 773 | test('%(formats.:.id) 18l', ' id 1, id 2, id 3') |
7d1eb38a | 774 | test('%(formats)j', (json.dumps(FORMATS), sanitize(json.dumps(FORMATS)))) |
4476d2c7 | 775 | test('%(formats)#j', (json.dumps(FORMATS, indent=4), sanitize(json.dumps(FORMATS, indent=4)))) |
f5aa5cfb | 776 | test('%(title5).3B', 'á') |
524e2e4f | 777 | test('%(title5)U', 'áéí 𝐀') |
778 | test('%(title5)#U', 'a\u0301e\u0301i\u0301 𝐀') | |
779 | test('%(title5)+U', 'áéí A') | |
780 | test('%(title5)+#U', 'a\u0301e\u0301i\u0301 A') | |
abbeeebc | 781 | test('%(height)D', '1k') |
782 | test('%(filesize)#D', '1Ki') | |
783 | test('%(height)5.2D', ' 1.08k') | |
37893bb0 | 784 | test('%(title4)#S', 'foo_bar_test') |
785 | test('%(title4).10S', ('foo \'bar\' ', 'foo \'bar\'' + ('#' if compat_os_name == 'nt' else ' '))) | |
7d1eb38a | 786 | if compat_os_name == 'nt': |
787 | test('%(title4)q', ('"foo \\"bar\\" test"', "'foo _'bar_' test'")) | |
4476d2c7 | 788 | test('%(formats.:.id)#q', ('"id 1" "id 2" "id 3"', "'id 1' 'id 2' 'id 3'")) |
789 | test('%(formats.0.id)#q', ('"id 1"', "'id 1'")) | |
7d1eb38a | 790 | else: |
791 | test('%(title4)q', ('\'foo "bar" test\'', "'foo 'bar' test'")) | |
4476d2c7 | 792 | test('%(formats.:.id)#q', "'id 1' 'id 2' 'id 3'") |
793 | test('%(formats.0.id)#q', "'id 1'") | |
7d1eb38a | 794 | |
795 | # Internal formatting | |
5c6542ce | 796 | test('%(timestamp-1000>%H-%M-%S)s', '11-43-20') |
901130bb | 797 | test('%(title|%)s %(title|%%)s', '% %%') |
5c6542ce | 798 | test('%(id+1-height+3)05d', '00158') |
799 | test('%(width+100)05d', 'NA') | |
7d1eb38a | 800 | test('%(formats.0) 15s', ('% 15s' % FORMATS[0], '% 15s' % sanitize(str(FORMATS[0])))) |
801 | test('%(formats.0)r', (repr(FORMATS[0]), sanitize(repr(FORMATS[0])))) | |
5c6542ce | 802 | test('%(height.0)03d', '001') |
803 | test('%(-height.0)04d', '-001') | |
804 | test('%(formats.-1.id)s', FORMATS[-1]['id']) | |
805 | test('%(formats.0.id.-1)d', FORMATS[0]['id'][-1]) | |
806 | test('%(formats.3)s', 'NA') | |
807 | test('%(formats.:2:-1)r', repr(FORMATS[:2:-1])) | |
808 | test('%(formats.0.id.-1+id)f', '1235.000000') | |
385a27fa | 809 | test('%(formats.0.id.-1+formats.1.id.-1)d', '3') |
5c6542ce | 810 | |
7c37ff97 | 811 | # Alternates |
812 | test('%(title,id)s', '1234') | |
813 | test('%(width-100,height+20|def)d', '1100') | |
814 | test('%(width-100,height+width|def)s', 'def') | |
815 | test('%(timestamp-x>%H\\,%M\\,%S,timestamp>%H\\,%M\\,%S)s', '12,00,00') | |
816 | ||
e0fd9573 | 817 | # Replacement |
818 | test('%(id&foo)s.bar', 'foo.bar') | |
819 | test('%(title&foo)s.bar', 'NA.bar') | |
820 | test('%(title&foo|baz)s.bar', 'baz.bar') | |
821 | ||
6e84b215 | 822 | # Laziness |
823 | def gen(): | |
824 | yield from range(5) | |
825 | raise self.assertTrue(False, 'LazyList should not be evaluated till here') | |
826 | test('%(key.4)s', '4', info={'key': LazyList(gen())}) | |
827 | ||
5c6542ce | 828 | # Empty filename |
829 | test('%(foo|)s-%(bar|)s.%(ext)s', '-.mp4') | |
830 | # test('%(foo|)s.%(ext)s', ('.mp4', '_.mp4')) # fixme | |
831 | # test('%(foo|)s', ('', '_')) # fixme | |
832 | ||
901130bb | 833 | # Environment variable expansion for prepare_filename |
834 | compat_setenv('__yt_dlp_var', 'expanded') | |
835 | envvar = '%__yt_dlp_var%' if compat_os_name == 'nt' else '$__yt_dlp_var' | |
836 | test(envvar, (envvar, 'expanded')) | |
b836dc94 | 837 | if compat_os_name == 'nt': |
838 | test('%s%', ('%s%', '%s%')) | |
839 | compat_setenv('s', 'expanded') | |
840 | test('%s%', ('%s%', 'expanded')) # %s% should be expanded before escaping %s | |
841 | compat_setenv('(test)s', 'expanded') | |
842 | test('%(test)s%', ('NA%', 'expanded')) # Environment should take priority over template | |
901130bb | 843 | |
5c6542ce | 844 | # Path expansion and escaping |
845 | test('Hello %(title1)s', 'Hello $PATH') | |
846 | test('Hello %(title2)s', 'Hello %PATH%') | |
847 | test('%(title3)s', ('foo/bar\\test', 'foo_bar_test')) | |
848 | test('folder/%(title3)s', ('folder/foo/bar\\test', 'folder%sfoo_bar_test' % os.path.sep)) | |
26e63931 | 849 | |
c57f7757 PH |
850 | def test_format_note(self): |
851 | ydl = YoutubeDL() | |
852 | self.assertEqual(ydl._format_note({}), '') | |
853 | assertRegexpMatches(self, ydl._format_note({ | |
854 | 'vbr': 10, | |
398dea32 | 855 | }), r'^\s*10k$') |
5d583bdf S |
856 | assertRegexpMatches(self, ydl._format_note({ |
857 | 'fps': 30, | |
398dea32 | 858 | }), r'^30fps$') |
5d583bdf | 859 | |
2b4ecde2 JMF |
860 | def test_postprocessors(self): |
861 | filename = 'post-processor-testfile.mp4' | |
862 | audiofile = filename + '.mp3' | |
863 | ||
864 | class SimplePP(PostProcessor): | |
865 | def run(self, info): | |
2b4ecde2 JMF |
866 | with open(audiofile, 'wt') as f: |
867 | f.write('EXAMPLE') | |
592e97e8 | 868 | return [info['filepath']], info |
2b4ecde2 | 869 | |
592e97e8 | 870 | def run_pp(params, PP): |
2b4ecde2 JMF |
871 | with open(filename, 'wt') as f: |
872 | f.write('EXAMPLE') | |
873 | ydl = YoutubeDL(params) | |
592e97e8 | 874 | ydl.add_post_processor(PP()) |
2b4ecde2 JMF |
875 | ydl.post_process(filename, {'filepath': filename}) |
876 | ||
592e97e8 | 877 | run_pp({'keepvideo': True}, SimplePP) |
2b4ecde2 JMF |
878 | self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename) |
879 | self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile) | |
880 | os.unlink(filename) | |
881 | os.unlink(audiofile) | |
882 | ||
592e97e8 | 883 | run_pp({'keepvideo': False}, SimplePP) |
2b4ecde2 JMF |
884 | self.assertFalse(os.path.exists(filename), '%s exists' % filename) |
885 | self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile) | |
886 | os.unlink(audiofile) | |
887 | ||
592e97e8 JMF |
888 | class ModifierPP(PostProcessor): |
889 | def run(self, info): | |
890 | with open(info['filepath'], 'wt') as f: | |
891 | f.write('MODIFIED') | |
892 | return [], info | |
893 | ||
894 | run_pp({'keepvideo': False}, ModifierPP) | |
895 | self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename) | |
896 | os.unlink(filename) | |
897 | ||
531980d8 | 898 | def test_match_filter(self): |
531980d8 JMF |
899 | first = { |
900 | 'id': '1', | |
901 | 'url': TEST_URL, | |
902 | 'title': 'one', | |
903 | 'extractor': 'TEST', | |
904 | 'duration': 30, | |
905 | 'filesize': 10 * 1024, | |
e5a088dc | 906 | 'playlist_id': '42', |
db13c16e S |
907 | 'uploader': "變態妍字幕版 太妍 тест", |
908 | 'creator': "тест ' 123 ' тест--", | |
732044af | 909 | 'webpage_url': 'http://example.com/watch?v=shenanigans', |
531980d8 JMF |
910 | } |
911 | second = { | |
912 | 'id': '2', | |
913 | 'url': TEST_URL, | |
914 | 'title': 'two', | |
915 | 'extractor': 'TEST', | |
916 | 'duration': 10, | |
917 | 'description': 'foo', | |
918 | 'filesize': 5 * 1024, | |
e5a088dc | 919 | 'playlist_id': '43', |
db13c16e | 920 | 'uploader': "тест 123", |
732044af | 921 | 'webpage_url': 'http://example.com/watch?v=SHENANIGANS', |
531980d8 JMF |
922 | } |
923 | videos = [first, second] | |
924 | ||
925 | def get_videos(filter_=None): | |
09b49e1f | 926 | ydl = YDL({'match_filter': filter_, 'simulate': True}) |
531980d8 JMF |
927 | for v in videos: |
928 | ydl.process_ie_result(v, download=True) | |
929 | return [v['id'] for v in ydl.downloaded_info_dicts] | |
930 | ||
931 | res = get_videos() | |
932 | self.assertEqual(res, ['1', '2']) | |
933 | ||
934 | def f(v): | |
935 | if v['id'] == '1': | |
936 | return None | |
937 | else: | |
938 | return 'Video id is not 1' | |
939 | res = get_videos(f) | |
940 | self.assertEqual(res, ['1']) | |
941 | ||
942 | f = match_filter_func('duration < 30') | |
943 | res = get_videos(f) | |
944 | self.assertEqual(res, ['2']) | |
945 | ||
946 | f = match_filter_func('description = foo') | |
947 | res = get_videos(f) | |
948 | self.assertEqual(res, ['2']) | |
949 | ||
950 | f = match_filter_func('description =? foo') | |
951 | res = get_videos(f) | |
952 | self.assertEqual(res, ['1', '2']) | |
953 | ||
954 | f = match_filter_func('filesize > 5KiB') | |
955 | res = get_videos(f) | |
956 | self.assertEqual(res, ['1']) | |
957 | ||
e5a088dc S |
958 | f = match_filter_func('playlist_id = 42') |
959 | res = get_videos(f) | |
960 | self.assertEqual(res, ['1']) | |
961 | ||
db13c16e S |
962 | f = match_filter_func('uploader = "變態妍字幕版 太妍 тест"') |
963 | res = get_videos(f) | |
964 | self.assertEqual(res, ['1']) | |
965 | ||
966 | f = match_filter_func('uploader != "變態妍字幕版 太妍 тест"') | |
967 | res = get_videos(f) | |
968 | self.assertEqual(res, ['2']) | |
969 | ||
970 | f = match_filter_func('creator = "тест \' 123 \' тест--"') | |
971 | res = get_videos(f) | |
972 | self.assertEqual(res, ['1']) | |
973 | ||
974 | f = match_filter_func("creator = 'тест \\' 123 \\' тест--'") | |
975 | res = get_videos(f) | |
976 | self.assertEqual(res, ['1']) | |
977 | ||
978 | f = match_filter_func(r"creator = 'тест \' 123 \' тест--' & duration > 30") | |
979 | res = get_videos(f) | |
980 | self.assertEqual(res, []) | |
981 | ||
e9eaf3fb JMF |
982 | def test_playlist_items_selection(self): |
983 | entries = [{ | |
984 | 'id': compat_str(i), | |
985 | 'title': compat_str(i), | |
986 | 'url': TEST_URL, | |
987 | } for i in range(1, 5)] | |
988 | playlist = { | |
989 | '_type': 'playlist', | |
990 | 'id': 'test', | |
991 | 'entries': entries, | |
992 | 'extractor': 'test:playlist', | |
993 | 'extractor_key': 'test:playlist', | |
994 | 'webpage_url': 'http://example.com', | |
995 | } | |
996 | ||
4e9e1e24 | 997 | def get_downloaded_info_dicts(params): |
e9eaf3fb | 998 | ydl = YDL(params) |
e0abaab2 S |
999 | # make a deep copy because the dictionary and nested entries |
1000 | # can be modified | |
1001 | ydl.process_ie_result(copy.deepcopy(playlist)) | |
4e9e1e24 S |
1002 | return ydl.downloaded_info_dicts |
1003 | ||
9e598870 | 1004 | def test_selection(params, expected_ids): |
1005 | results = [ | |
1006 | (v['playlist_autonumber'] - 1, (int(v['id']), v['playlist_index'])) | |
1007 | for v in get_downloaded_info_dicts(params)] | |
1008 | self.assertEqual(results, list(enumerate(zip(expected_ids, expected_ids)))) | |
1009 | ||
1010 | test_selection({}, [1, 2, 3, 4]) | |
1011 | test_selection({'playlistend': 10}, [1, 2, 3, 4]) | |
1012 | test_selection({'playlistend': 2}, [1, 2]) | |
1013 | test_selection({'playliststart': 10}, []) | |
1014 | test_selection({'playliststart': 2}, [2, 3, 4]) | |
1015 | test_selection({'playlist_items': '2-4'}, [2, 3, 4]) | |
1016 | test_selection({'playlist_items': '2,4'}, [2, 4]) | |
1017 | test_selection({'playlist_items': '10'}, []) | |
ff1c7fc9 | 1018 | test_selection({'playlist_items': '0'}, []) |
cd6fc19e | 1019 | |
4e9e1e24 | 1020 | # Tests for https://github.com/ytdl-org/youtube-dl/issues/10591 |
9e598870 | 1021 | test_selection({'playlist_items': '2-4,3-4,3'}, [2, 3, 4]) |
1022 | test_selection({'playlist_items': '4,2'}, [4, 2]) | |
1023 | ||
1024 | # Tests for https://github.com/yt-dlp/yt-dlp/issues/720 | |
1025 | # https://github.com/yt-dlp/yt-dlp/issues/302 | |
1026 | test_selection({'playlistreverse': True}, [4, 3, 2, 1]) | |
1027 | test_selection({'playliststart': 2, 'playlistreverse': True}, [4, 3, 2]) | |
1028 | test_selection({'playlist_items': '2,4', 'playlistreverse': True}, [4, 2]) | |
1029 | test_selection({'playlist_items': '4,2'}, [4, 2]) | |
4e9e1e24 | 1030 | |
e37afbe0 | 1031 | def test_urlopen_no_file_protocol(self): |
067aa17e | 1032 | # see https://github.com/ytdl-org/youtube-dl/issues/8227 |
e37afbe0 JMF |
1033 | ydl = YDL() |
1034 | self.assertRaises(compat_urllib_error.URLError, ydl.urlopen, 'file:///etc/passwd') | |
1035 | ||
b286f201 YCH |
1036 | def test_do_not_override_ie_key_in_url_transparent(self): |
1037 | ydl = YDL() | |
1038 | ||
1039 | class Foo1IE(InfoExtractor): | |
1040 | _VALID_URL = r'foo1:' | |
1041 | ||
1042 | def _real_extract(self, url): | |
1043 | return { | |
1044 | '_type': 'url_transparent', | |
1045 | 'url': 'foo2:', | |
1046 | 'ie_key': 'Foo2', | |
0396806f S |
1047 | 'title': 'foo1 title', |
1048 | 'id': 'foo1_id', | |
b286f201 YCH |
1049 | } |
1050 | ||
1051 | class Foo2IE(InfoExtractor): | |
1052 | _VALID_URL = r'foo2:' | |
1053 | ||
1054 | def _real_extract(self, url): | |
1055 | return { | |
1056 | '_type': 'url', | |
1057 | 'url': 'foo3:', | |
1058 | 'ie_key': 'Foo3', | |
1059 | } | |
1060 | ||
1061 | class Foo3IE(InfoExtractor): | |
1062 | _VALID_URL = r'foo3:' | |
1063 | ||
1064 | def _real_extract(self, url): | |
51350db5 | 1065 | return _make_result([{'url': TEST_URL}], title='foo3 title') |
b286f201 YCH |
1066 | |
1067 | ydl.add_info_extractor(Foo1IE(ydl)) | |
1068 | ydl.add_info_extractor(Foo2IE(ydl)) | |
1069 | ydl.add_info_extractor(Foo3IE(ydl)) | |
1070 | ydl.extract_info('foo1:') | |
1071 | downloaded = ydl.downloaded_info_dicts[0] | |
1072 | self.assertEqual(downloaded['url'], TEST_URL) | |
51350db5 | 1073 | self.assertEqual(downloaded['title'], 'foo1 title') |
0396806f S |
1074 | self.assertEqual(downloaded['id'], 'testid') |
1075 | self.assertEqual(downloaded['extractor'], 'testex') | |
1076 | self.assertEqual(downloaded['extractor_key'], 'TestEx') | |
b286f201 | 1077 | |
a0566bbf | 1078 | # Test case for https://github.com/ytdl-org/youtube-dl/issues/27064 |
1079 | def test_ignoreerrors_for_playlist_with_url_transparent_iterable_entries(self): | |
1080 | ||
1081 | class _YDL(YDL): | |
1082 | def __init__(self, *args, **kwargs): | |
1083 | super(_YDL, self).__init__(*args, **kwargs) | |
1084 | ||
1085 | def trouble(self, s, tb=None): | |
1086 | pass | |
1087 | ||
1088 | ydl = _YDL({ | |
1089 | 'format': 'extra', | |
1090 | 'ignoreerrors': True, | |
1091 | }) | |
1092 | ||
1093 | class VideoIE(InfoExtractor): | |
1094 | _VALID_URL = r'video:(?P<id>\d+)' | |
1095 | ||
1096 | def _real_extract(self, url): | |
1097 | video_id = self._match_id(url) | |
1098 | formats = [{ | |
1099 | 'format_id': 'default', | |
1100 | 'url': 'url:', | |
1101 | }] | |
1102 | if video_id == '0': | |
1103 | raise ExtractorError('foo') | |
1104 | if video_id == '2': | |
1105 | formats.append({ | |
1106 | 'format_id': 'extra', | |
1107 | 'url': TEST_URL, | |
1108 | }) | |
1109 | return { | |
1110 | 'id': video_id, | |
1111 | 'title': 'Video %s' % video_id, | |
1112 | 'formats': formats, | |
1113 | } | |
1114 | ||
1115 | class PlaylistIE(InfoExtractor): | |
1116 | _VALID_URL = r'playlist:' | |
1117 | ||
1118 | def _entries(self): | |
1119 | for n in range(3): | |
1120 | video_id = compat_str(n) | |
1121 | yield { | |
1122 | '_type': 'url_transparent', | |
1123 | 'ie_key': VideoIE.ie_key(), | |
1124 | 'id': video_id, | |
1125 | 'url': 'video:%s' % video_id, | |
1126 | 'title': 'Video Transparent %s' % video_id, | |
1127 | } | |
1128 | ||
1129 | def _real_extract(self, url): | |
1130 | return self.playlist_result(self._entries()) | |
1131 | ||
1132 | ydl.add_info_extractor(VideoIE(ydl)) | |
1133 | ydl.add_info_extractor(PlaylistIE(ydl)) | |
1134 | info = ydl.extract_info('playlist:') | |
1135 | entries = info['entries'] | |
1136 | self.assertEqual(len(entries), 3) | |
1137 | self.assertTrue(entries[0] is None) | |
1138 | self.assertTrue(entries[1] is None) | |
1139 | self.assertEqual(len(ydl.downloaded_info_dicts), 1) | |
1140 | downloaded = ydl.downloaded_info_dicts[0] | |
9e907ebd | 1141 | entries[2].pop('requested_downloads', None) |
a0566bbf | 1142 | self.assertEqual(entries[2], downloaded) |
1143 | self.assertEqual(downloaded['url'], TEST_URL) | |
1144 | self.assertEqual(downloaded['title'], 'Video Transparent 2') | |
1145 | self.assertEqual(downloaded['id'], '2') | |
1146 | self.assertEqual(downloaded['extractor'], 'Video') | |
1147 | self.assertEqual(downloaded['extractor_key'], 'Video') | |
1148 | ||
2b4ecde2 | 1149 | |
e028d0d1 JMF |
1150 | if __name__ == '__main__': |
1151 | unittest.main() |