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