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