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