]>
Commit | Line | Data |
---|---|---|
e028d0d1 | 1 | #!/usr/bin/env python |
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 |
26e63931 | 15 | from youtube_dl import YoutubeDL |
e37afbe0 | 16 | from youtube_dl.compat import compat_str, compat_urllib_error |
3d4a70b8 | 17 | from youtube_dl.extractor import YoutubeIE |
b286f201 | 18 | from youtube_dl.extractor.common import InfoExtractor |
2b4ecde2 | 19 | from youtube_dl.postprocessor.common import PostProcessor |
bb8e5536 | 20 | from youtube_dl.utils import ExtractorError, 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 JMF |
31 | def process_info(self, info_dict): |
32 | self.downloaded_info_dicts.append(info_dict) | |
33 | ||
f4d96df0 PH |
34 | def to_screen(self, msg): |
35 | self.msgs.append(msg) | |
36 | ||
5d254f77 | 37 | |
3537b93d PH |
38 | def _make_result(formats, **kwargs): |
39 | res = { | |
40 | 'formats': formats, | |
41 | 'id': 'testid', | |
42 | 'title': 'testttitle', | |
43 | 'extractor': 'testex', | |
44 | } | |
45 | res.update(**kwargs) | |
46 | return res | |
47 | ||
48 | ||
e028d0d1 JMF |
49 | class TestFormatSelection(unittest.TestCase): |
50 | def test_prefer_free_formats(self): | |
51 | # Same resolution => download webm | |
52 | ydl = YDL() | |
53 | ydl.params['prefer_free_formats'] = True | |
5d254f77 | 54 | formats = [ |
8508557e JMF |
55 | {'ext': 'webm', 'height': 460, 'url': TEST_URL}, |
56 | {'ext': 'mp4', 'height': 460, 'url': TEST_URL}, | |
5d254f77 | 57 | ] |
3537b93d | 58 | info_dict = _make_result(formats) |
3d4a70b8 PH |
59 | yie = YoutubeIE(ydl) |
60 | yie._sort_formats(info_dict['formats']) | |
e028d0d1 JMF |
61 | ydl.process_ie_result(info_dict) |
62 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 63 | self.assertEqual(downloaded['ext'], 'webm') |
e028d0d1 JMF |
64 | |
65 | # Different resolution => download best quality (mp4) | |
66 | ydl = YDL() | |
67 | ydl.params['prefer_free_formats'] = True | |
5d254f77 | 68 | formats = [ |
8508557e JMF |
69 | {'ext': 'webm', 'height': 720, 'url': TEST_URL}, |
70 | {'ext': 'mp4', 'height': 1080, 'url': TEST_URL}, | |
5d254f77 | 71 | ] |
89087418 | 72 | info_dict['formats'] = formats |
3d4a70b8 PH |
73 | yie = YoutubeIE(ydl) |
74 | yie._sort_formats(info_dict['formats']) | |
e028d0d1 JMF |
75 | ydl.process_ie_result(info_dict) |
76 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 77 | self.assertEqual(downloaded['ext'], 'mp4') |
e028d0d1 | 78 | |
1c783bca | 79 | # No prefer_free_formats => prefer mp4 and flv for greater compatibility |
e028d0d1 JMF |
80 | ydl = YDL() |
81 | ydl.params['prefer_free_formats'] = False | |
5d254f77 | 82 | formats = [ |
8508557e JMF |
83 | {'ext': 'webm', 'height': 720, 'url': TEST_URL}, |
84 | {'ext': 'mp4', 'height': 720, 'url': TEST_URL}, | |
85 | {'ext': 'flv', 'height': 720, 'url': TEST_URL}, | |
5d254f77 | 86 | ] |
89087418 | 87 | info_dict['formats'] = formats |
3d4a70b8 PH |
88 | yie = YoutubeIE(ydl) |
89 | yie._sort_formats(info_dict['formats']) | |
90 | ydl.process_ie_result(info_dict) | |
91 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 92 | self.assertEqual(downloaded['ext'], 'mp4') |
3d4a70b8 PH |
93 | |
94 | ydl = YDL() | |
95 | ydl.params['prefer_free_formats'] = False | |
96 | formats = [ | |
8508557e JMF |
97 | {'ext': 'flv', 'height': 720, 'url': TEST_URL}, |
98 | {'ext': 'webm', 'height': 720, 'url': TEST_URL}, | |
3d4a70b8 | 99 | ] |
89087418 | 100 | info_dict['formats'] = formats |
3d4a70b8 PH |
101 | yie = YoutubeIE(ydl) |
102 | yie._sort_formats(info_dict['formats']) | |
e028d0d1 JMF |
103 | ydl.process_ie_result(info_dict) |
104 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 105 | self.assertEqual(downloaded['ext'], 'flv') |
e028d0d1 | 106 | |
a9c58ad9 JMF |
107 | def test_format_selection(self): |
108 | formats = [ | |
8508557e | 109 | {'format_id': '35', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL}, |
232541df | 110 | {'format_id': 'example-with-dashes', 'ext': 'webm', 'preference': 1, 'url': TEST_URL}, |
8508557e JMF |
111 | {'format_id': '45', 'ext': 'webm', 'preference': 2, 'url': TEST_URL}, |
112 | {'format_id': '47', 'ext': 'webm', 'preference': 3, 'url': TEST_URL}, | |
113 | {'format_id': '2', 'ext': 'flv', 'preference': 4, 'url': TEST_URL}, | |
a9c58ad9 | 114 | ] |
3537b93d | 115 | info_dict = _make_result(formats) |
a9c58ad9 | 116 | |
89087418 | 117 | ydl = YDL({'format': '20/47'}) |
8e3e0322 | 118 | ydl.process_ie_result(info_dict.copy()) |
a9c58ad9 | 119 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 120 | self.assertEqual(downloaded['format_id'], '47') |
a9c58ad9 | 121 | |
89087418 | 122 | ydl = YDL({'format': '20/71/worst'}) |
8e3e0322 | 123 | ydl.process_ie_result(info_dict.copy()) |
a9c58ad9 | 124 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 125 | self.assertEqual(downloaded['format_id'], '35') |
a9c58ad9 JMF |
126 | |
127 | ydl = YDL() | |
8e3e0322 | 128 | ydl.process_ie_result(info_dict.copy()) |
a9c58ad9 | 129 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 130 | self.assertEqual(downloaded['format_id'], '2') |
a9c58ad9 | 131 | |
89087418 | 132 | ydl = YDL({'format': 'webm/mp4'}) |
8e3e0322 | 133 | ydl.process_ie_result(info_dict.copy()) |
49e86983 | 134 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 135 | self.assertEqual(downloaded['format_id'], '47') |
49e86983 | 136 | |
89087418 | 137 | ydl = YDL({'format': '3gp/40/mp4'}) |
8e3e0322 | 138 | ydl.process_ie_result(info_dict.copy()) |
49e86983 | 139 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 140 | self.assertEqual(downloaded['format_id'], '35') |
49e86983 | 141 | |
232541df JMF |
142 | ydl = YDL({'format': 'example-with-dashes'}) |
143 | ydl.process_ie_result(info_dict.copy()) | |
144 | downloaded = ydl.downloaded_info_dicts[0] | |
145 | self.assertEqual(downloaded['format_id'], 'example-with-dashes') | |
146 | ||
ba7678f9 PH |
147 | def test_format_selection_audio(self): |
148 | formats = [ | |
8508557e JMF |
149 | {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL}, |
150 | {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none', 'url': TEST_URL}, | |
151 | {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none', 'url': TEST_URL}, | |
152 | {'format_id': 'vid', 'ext': 'mp4', 'preference': 4, 'url': TEST_URL}, | |
ba7678f9 | 153 | ] |
3537b93d | 154 | info_dict = _make_result(formats) |
ba7678f9 | 155 | |
89087418 | 156 | ydl = YDL({'format': 'bestaudio'}) |
ba7678f9 PH |
157 | ydl.process_ie_result(info_dict.copy()) |
158 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 159 | self.assertEqual(downloaded['format_id'], 'audio-high') |
ba7678f9 | 160 | |
89087418 | 161 | ydl = YDL({'format': 'worstaudio'}) |
ba7678f9 PH |
162 | ydl.process_ie_result(info_dict.copy()) |
163 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 164 | self.assertEqual(downloaded['format_id'], 'audio-low') |
ba7678f9 PH |
165 | |
166 | formats = [ | |
8508557e JMF |
167 | {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL}, |
168 | {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': TEST_URL}, | |
ba7678f9 | 169 | ] |
3537b93d | 170 | info_dict = _make_result(formats) |
ba7678f9 | 171 | |
89087418 | 172 | ydl = YDL({'format': 'bestaudio/worstaudio/best'}) |
ba7678f9 PH |
173 | ydl.process_ie_result(info_dict.copy()) |
174 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 175 | self.assertEqual(downloaded['format_id'], 'vid-high') |
ba7678f9 | 176 | |
0217c783 PH |
177 | def test_format_selection_audio_exts(self): |
178 | formats = [ | |
179 | {'format_id': 'mp3-64', 'ext': 'mp3', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, | |
180 | {'format_id': 'ogg-64', 'ext': 'ogg', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, | |
181 | {'format_id': 'aac-64', 'ext': 'aac', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, | |
182 | {'format_id': 'mp3-32', 'ext': 'mp3', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'}, | |
183 | {'format_id': 'aac-32', 'ext': 'aac', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'}, | |
184 | ] | |
185 | ||
186 | info_dict = _make_result(formats) | |
187 | ydl = YDL({'format': 'best'}) | |
188 | ie = YoutubeIE(ydl) | |
189 | ie._sort_formats(info_dict['formats']) | |
190 | ydl.process_ie_result(copy.deepcopy(info_dict)) | |
191 | downloaded = ydl.downloaded_info_dicts[0] | |
192 | self.assertEqual(downloaded['format_id'], 'aac-64') | |
193 | ||
194 | ydl = YDL({'format': 'mp3'}) | |
195 | ie = YoutubeIE(ydl) | |
196 | ie._sort_formats(info_dict['formats']) | |
197 | ydl.process_ie_result(copy.deepcopy(info_dict)) | |
198 | downloaded = ydl.downloaded_info_dicts[0] | |
199 | self.assertEqual(downloaded['format_id'], 'mp3-64') | |
200 | ||
201 | ydl = YDL({'prefer_free_formats': True}) | |
202 | ie = YoutubeIE(ydl) | |
203 | ie._sort_formats(info_dict['formats']) | |
204 | ydl.process_ie_result(copy.deepcopy(info_dict)) | |
205 | downloaded = ydl.downloaded_info_dicts[0] | |
206 | self.assertEqual(downloaded['format_id'], 'ogg-64') | |
207 | ||
bc6d5978 JMF |
208 | def test_format_selection_video(self): |
209 | formats = [ | |
8508557e JMF |
210 | {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none', 'url': TEST_URL}, |
211 | {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none', 'url': TEST_URL}, | |
212 | {'format_id': 'vid', 'ext': 'mp4', 'preference': 3, 'url': TEST_URL}, | |
bc6d5978 | 213 | ] |
3537b93d | 214 | info_dict = _make_result(formats) |
bc6d5978 JMF |
215 | |
216 | ydl = YDL({'format': 'bestvideo'}) | |
217 | ydl.process_ie_result(info_dict.copy()) | |
218 | downloaded = ydl.downloaded_info_dicts[0] | |
219 | self.assertEqual(downloaded['format_id'], 'dash-video-high') | |
220 | ||
221 | ydl = YDL({'format': 'worstvideo'}) | |
222 | ydl.process_ie_result(info_dict.copy()) | |
223 | downloaded = ydl.downloaded_info_dicts[0] | |
224 | self.assertEqual(downloaded['format_id'], 'dash-video-low') | |
225 | ||
4c3b16d5 S |
226 | ydl = YDL({'format': 'bestvideo[format_id^=dash][format_id$=low]'}) |
227 | ydl.process_ie_result(info_dict.copy()) | |
228 | downloaded = ydl.downloaded_info_dicts[0] | |
229 | self.assertEqual(downloaded['format_id'], 'dash-video-low') | |
230 | ||
b913348d | 231 | formats = [ |
232 | {'format_id': 'vid-vcodec-dot', 'ext': 'mp4', 'preference': 1, 'vcodec': 'avc1.123456', 'acodec': 'none', 'url': TEST_URL}, | |
233 | ] | |
234 | info_dict = _make_result(formats) | |
235 | ||
236 | ydl = YDL({'format': 'bestvideo[vcodec=avc1.123456]'}) | |
237 | ydl.process_ie_result(info_dict.copy()) | |
238 | downloaded = ydl.downloaded_info_dicts[0] | |
239 | self.assertEqual(downloaded['format_id'], 'vid-vcodec-dot') | |
240 | ||
3d4a70b8 PH |
241 | def test_youtube_format_selection(self): |
242 | order = [ | |
86bf2905 | 243 | '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '17', '36', '13', |
3d4a70b8 PH |
244 | # Apple HTTP Live Streaming |
245 | '96', '95', '94', '93', '92', '132', '151', | |
246 | # 3D | |
247 | '85', '84', '102', '83', '101', '82', '100', | |
248 | # Dash video | |
c11125f9 | 249 | '137', '248', '136', '247', '135', '246', |
3d4a70b8 PH |
250 | '245', '244', '134', '243', '133', '242', '160', |
251 | # Dash audio | |
a053c349 | 252 | '141', '172', '140', '171', '139', |
3d4a70b8 PH |
253 | ] |
254 | ||
67134eab JMF |
255 | def format_info(f_id): |
256 | info = YoutubeIE._formats[f_id].copy() | |
1df41411 | 257 | |
91cb6b50 | 258 | # XXX: In real cases InfoExtractor._parse_mpd_formats() fills up 'acodec' |
1df41411 YCH |
259 | # and 'vcodec', while in tests such information is incomplete since |
260 | # commit a6c2c24479e5f4827ceb06f64d855329c0a6f593 | |
261 | # test_YoutubeDL.test_youtube_format_selection is broken without | |
262 | # this fix | |
263 | if 'acodec' in info and 'vcodec' not in info: | |
264 | info['vcodec'] = 'none' | |
265 | elif 'vcodec' in info and 'acodec' not in info: | |
266 | info['acodec'] = 'none' | |
267 | ||
67134eab JMF |
268 | info['format_id'] = f_id |
269 | info['url'] = 'url:' + f_id | |
270 | return info | |
271 | formats_order = [format_info(f_id) for f_id in order] | |
272 | ||
273 | info_dict = _make_result(list(formats_order), extractor='youtube') | |
274 | ydl = YDL({'format': 'bestvideo+bestaudio'}) | |
275 | yie = YoutubeIE(ydl) | |
276 | yie._sort_formats(info_dict['formats']) | |
277 | ydl.process_ie_result(info_dict) | |
278 | downloaded = ydl.downloaded_info_dicts[0] | |
279 | self.assertEqual(downloaded['format_id'], '137+141') | |
280 | self.assertEqual(downloaded['ext'], 'mp4') | |
3d4a70b8 | 281 | |
cf2ac6df JMF |
282 | info_dict = _make_result(list(formats_order), extractor='youtube') |
283 | ydl = YDL({'format': 'bestvideo[height>=999999]+bestaudio/best'}) | |
284 | yie = YoutubeIE(ydl) | |
285 | yie._sort_formats(info_dict['formats']) | |
286 | ydl.process_ie_result(info_dict) | |
287 | downloaded = ydl.downloaded_info_dicts[0] | |
288 | self.assertEqual(downloaded['format_id'], '38') | |
289 | ||
f5f4a27a JMF |
290 | info_dict = _make_result(list(formats_order), extractor='youtube') |
291 | ydl = YDL({'format': 'bestvideo/best,bestaudio'}) | |
292 | yie = YoutubeIE(ydl) | |
293 | yie._sort_formats(info_dict['formats']) | |
294 | ydl.process_ie_result(info_dict) | |
295 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
296 | self.assertEqual(downloaded_ids, ['137', '141']) | |
297 | ||
0130afb7 JMF |
298 | info_dict = _make_result(list(formats_order), extractor='youtube') |
299 | ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])+bestaudio'}) | |
300 | yie = YoutubeIE(ydl) | |
301 | yie._sort_formats(info_dict['formats']) | |
302 | ydl.process_ie_result(info_dict) | |
303 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
304 | self.assertEqual(downloaded_ids, ['137+141', '248+141']) | |
305 | ||
306 | info_dict = _make_result(list(formats_order), extractor='youtube') | |
307 | ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])[height<=720]+bestaudio'}) | |
308 | yie = YoutubeIE(ydl) | |
309 | yie._sort_formats(info_dict['formats']) | |
310 | ydl.process_ie_result(info_dict) | |
311 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
312 | self.assertEqual(downloaded_ids, ['136+141', '247+141']) | |
313 | ||
314 | info_dict = _make_result(list(formats_order), extractor='youtube') | |
315 | ydl = YDL({'format': '(bestvideo[ext=none]/bestvideo[ext=webm])+bestaudio'}) | |
316 | yie = YoutubeIE(ydl) | |
317 | yie._sort_formats(info_dict['formats']) | |
318 | ydl.process_ie_result(info_dict) | |
319 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
320 | self.assertEqual(downloaded_ids, ['248+141']) | |
321 | ||
67134eab | 322 | for f1, f2 in zip(formats_order, formats_order[1:]): |
3537b93d | 323 | info_dict = _make_result([f1, f2], extractor='youtube') |
8dd54188 | 324 | ydl = YDL({'format': 'best/bestvideo'}) |
3d4a70b8 PH |
325 | yie = YoutubeIE(ydl) |
326 | yie._sort_formats(info_dict['formats']) | |
327 | ydl.process_ie_result(info_dict) | |
328 | downloaded = ydl.downloaded_info_dicts[0] | |
67134eab | 329 | self.assertEqual(downloaded['format_id'], f1['format_id']) |
3d4a70b8 | 330 | |
3537b93d | 331 | info_dict = _make_result([f2, f1], extractor='youtube') |
8dd54188 | 332 | ydl = YDL({'format': 'best/bestvideo'}) |
3d4a70b8 PH |
333 | yie = YoutubeIE(ydl) |
334 | yie._sort_formats(info_dict['formats']) | |
335 | ydl.process_ie_result(info_dict) | |
336 | downloaded = ydl.downloaded_info_dicts[0] | |
67134eab | 337 | self.assertEqual(downloaded['format_id'], f1['format_id']) |
3d4a70b8 | 338 | |
317f7ab6 S |
339 | def test_audio_only_extractor_format_selection(self): |
340 | # For extractors with incomplete formats (all formats are audio-only or | |
341 | # video-only) best and worst should fallback to corresponding best/worst | |
342 | # video-only or audio-only formats (as per | |
343 | # https://github.com/rg3/youtube-dl/pull/5556) | |
344 | formats = [ | |
345 | {'format_id': 'low', 'ext': 'mp3', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL}, | |
346 | {'format_id': 'high', 'ext': 'mp3', 'preference': 2, 'vcodec': 'none', 'url': TEST_URL}, | |
347 | ] | |
348 | info_dict = _make_result(formats) | |
349 | ||
350 | ydl = YDL({'format': 'best'}) | |
351 | ydl.process_ie_result(info_dict.copy()) | |
352 | downloaded = ydl.downloaded_info_dicts[0] | |
353 | self.assertEqual(downloaded['format_id'], 'high') | |
354 | ||
355 | ydl = YDL({'format': 'worst'}) | |
356 | ydl.process_ie_result(info_dict.copy()) | |
357 | downloaded = ydl.downloaded_info_dicts[0] | |
358 | self.assertEqual(downloaded['format_id'], 'low') | |
359 | ||
360 | def test_format_not_available(self): | |
361 | formats = [ | |
362 | {'format_id': 'regular', 'ext': 'mp4', 'height': 360, 'url': TEST_URL}, | |
363 | {'format_id': 'video', 'ext': 'mp4', 'height': 720, 'acodec': 'none', 'url': TEST_URL}, | |
364 | ] | |
365 | info_dict = _make_result(formats) | |
366 | ||
367 | # This must fail since complete video-audio format does not match filter | |
368 | # and extractor does not provide incomplete only formats (i.e. only | |
369 | # video-only or audio-only). | |
370 | ydl = YDL({'format': 'best[height>360]'}) | |
371 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) | |
372 | ||
0a31a350 JMF |
373 | def test_invalid_format_specs(self): |
374 | def assert_syntax_error(format_spec): | |
375 | ydl = YDL({'format': format_spec}) | |
376 | info_dict = _make_result([{'format_id': 'foo', 'url': TEST_URL}]) | |
377 | self.assertRaises(SyntaxError, ydl.process_ie_result, info_dict) | |
378 | ||
379 | assert_syntax_error('bestvideo,,best') | |
380 | assert_syntax_error('+bestaudio') | |
381 | assert_syntax_error('bestvideo+') | |
d96d604e | 382 | assert_syntax_error('/') |
0a31a350 | 383 | |
083c9df9 PH |
384 | def test_format_filtering(self): |
385 | formats = [ | |
386 | {'format_id': 'A', 'filesize': 500, 'width': 1000}, | |
387 | {'format_id': 'B', 'filesize': 1000, 'width': 500}, | |
388 | {'format_id': 'C', 'filesize': 1000, 'width': 400}, | |
389 | {'format_id': 'D', 'filesize': 2000, 'width': 600}, | |
390 | {'format_id': 'E', 'filesize': 3000}, | |
391 | {'format_id': 'F'}, | |
392 | {'format_id': 'G', 'filesize': 1000000}, | |
393 | ] | |
394 | for f in formats: | |
395 | f['url'] = 'http://_/' | |
396 | f['ext'] = 'unknown' | |
397 | info_dict = _make_result(formats) | |
398 | ||
399 | ydl = YDL({'format': 'best[filesize<3000]'}) | |
400 | ydl.process_ie_result(info_dict) | |
401 | downloaded = ydl.downloaded_info_dicts[0] | |
402 | self.assertEqual(downloaded['format_id'], 'D') | |
403 | ||
404 | ydl = YDL({'format': 'best[filesize<=3000]'}) | |
405 | ydl.process_ie_result(info_dict) | |
406 | downloaded = ydl.downloaded_info_dicts[0] | |
407 | self.assertEqual(downloaded['format_id'], 'E') | |
408 | ||
409 | ydl = YDL({'format': 'best[filesize <= ? 3000]'}) | |
410 | ydl.process_ie_result(info_dict) | |
411 | downloaded = ydl.downloaded_info_dicts[0] | |
412 | self.assertEqual(downloaded['format_id'], 'F') | |
413 | ||
414 | ydl = YDL({'format': 'best [filesize = 1000] [width>450]'}) | |
415 | ydl.process_ie_result(info_dict) | |
416 | downloaded = ydl.downloaded_info_dicts[0] | |
417 | self.assertEqual(downloaded['format_id'], 'B') | |
418 | ||
419 | ydl = YDL({'format': 'best [filesize = 1000] [width!=450]'}) | |
420 | ydl.process_ie_result(info_dict) | |
421 | downloaded = ydl.downloaded_info_dicts[0] | |
422 | self.assertEqual(downloaded['format_id'], 'C') | |
423 | ||
424 | ydl = YDL({'format': '[filesize>?1]'}) | |
425 | ydl.process_ie_result(info_dict) | |
426 | downloaded = ydl.downloaded_info_dicts[0] | |
427 | self.assertEqual(downloaded['format_id'], 'G') | |
428 | ||
429 | ydl = YDL({'format': '[filesize<1M]'}) | |
430 | ydl.process_ie_result(info_dict) | |
431 | downloaded = ydl.downloaded_info_dicts[0] | |
432 | self.assertEqual(downloaded['format_id'], 'E') | |
433 | ||
434 | ydl = YDL({'format': '[filesize<1MiB]'}) | |
435 | ydl.process_ie_result(info_dict) | |
436 | downloaded = ydl.downloaded_info_dicts[0] | |
437 | self.assertEqual(downloaded['format_id'], 'G') | |
438 | ||
5acfa126 JMF |
439 | ydl = YDL({'format': 'all[width>=400][width<=600]'}) |
440 | ydl.process_ie_result(info_dict) | |
441 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
442 | self.assertEqual(downloaded_ids, ['B', 'C', 'D']) | |
443 | ||
bb8e5536 JMF |
444 | ydl = YDL({'format': 'best[height<40]'}) |
445 | try: | |
446 | ydl.process_ie_result(info_dict) | |
447 | except ExtractorError: | |
448 | pass | |
449 | self.assertEqual(ydl.downloaded_info_dicts, []) | |
450 | ||
f20bf146 JMF |
451 | |
452 | class TestYoutubeDL(unittest.TestCase): | |
ab84349b JMF |
453 | def test_subtitles(self): |
454 | def s_formats(lang, autocaption=False): | |
455 | return [{ | |
456 | 'ext': ext, | |
457 | 'url': 'http://localhost/video.%s.%s' % (lang, ext), | |
458 | '_auto': autocaption, | |
459 | } for ext in ['vtt', 'srt', 'ass']] | |
460 | subtitles = dict((l, s_formats(l)) for l in ['en', 'fr', 'es']) | |
461 | auto_captions = dict((l, s_formats(l, True)) for l in ['it', 'pt', 'es']) | |
462 | info_dict = { | |
463 | 'id': 'test', | |
464 | 'title': 'Test', | |
465 | 'url': 'http://localhost/video.mp4', | |
466 | 'subtitles': subtitles, | |
467 | 'automatic_captions': auto_captions, | |
468 | 'extractor': 'TEST', | |
469 | } | |
470 | ||
471 | def get_info(params={}): | |
472 | params.setdefault('simulate', True) | |
473 | ydl = YDL(params) | |
474 | ydl.report_warning = lambda *args, **kargs: None | |
475 | return ydl.process_video_result(info_dict, download=False) | |
476 | ||
477 | result = get_info() | |
478 | self.assertFalse(result.get('requested_subtitles')) | |
479 | self.assertEqual(result['subtitles'], subtitles) | |
480 | self.assertEqual(result['automatic_captions'], auto_captions) | |
481 | ||
482 | result = get_info({'writesubtitles': True}) | |
483 | subs = result['requested_subtitles'] | |
484 | self.assertTrue(subs) | |
485 | self.assertEqual(set(subs.keys()), set(['en'])) | |
486 | self.assertTrue(subs['en'].get('data') is None) | |
487 | self.assertEqual(subs['en']['ext'], 'ass') | |
488 | ||
489 | result = get_info({'writesubtitles': True, 'subtitlesformat': 'foo/srt'}) | |
490 | subs = result['requested_subtitles'] | |
491 | self.assertEqual(subs['en']['ext'], 'srt') | |
492 | ||
493 | result = get_info({'writesubtitles': True, 'subtitleslangs': ['es', 'fr', 'it']}) | |
494 | subs = result['requested_subtitles'] | |
495 | self.assertTrue(subs) | |
496 | self.assertEqual(set(subs.keys()), set(['es', 'fr'])) | |
497 | ||
498 | result = get_info({'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']}) | |
499 | subs = result['requested_subtitles'] | |
500 | self.assertTrue(subs) | |
501 | self.assertEqual(set(subs.keys()), set(['es', 'pt'])) | |
502 | self.assertFalse(subs['es']['_auto']) | |
503 | self.assertTrue(subs['pt']['_auto']) | |
504 | ||
98c70d6f JMF |
505 | result = get_info({'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']}) |
506 | subs = result['requested_subtitles'] | |
507 | self.assertTrue(subs) | |
508 | self.assertEqual(set(subs.keys()), set(['es', 'pt'])) | |
509 | self.assertTrue(subs['es']['_auto']) | |
510 | self.assertTrue(subs['pt']['_auto']) | |
511 | ||
b6c45014 JMF |
512 | def test_add_extra_info(self): |
513 | test_dict = { | |
514 | 'extractor': 'Foo', | |
515 | } | |
516 | extra_info = { | |
517 | 'extractor': 'Bar', | |
518 | 'playlist': 'funny videos', | |
519 | } | |
520 | YDL.add_extra_info(test_dict, extra_info) | |
521 | self.assertEqual(test_dict['extractor'], 'Foo') | |
522 | self.assertEqual(test_dict['playlist'], 'funny videos') | |
523 | ||
26e63931 JMF |
524 | def test_prepare_filename(self): |
525 | info = { | |
89087418 PH |
526 | 'id': '1234', |
527 | 'ext': 'mp4', | |
528 | 'width': None, | |
d0d9ade4 | 529 | 'height': 1080, |
15da37c7 S |
530 | 'title1': '$PATH', |
531 | 'title2': '%PATH%', | |
26e63931 | 532 | } |
5f6a1245 | 533 | |
26e63931 JMF |
534 | def fname(templ): |
535 | ydl = YoutubeDL({'outtmpl': templ}) | |
536 | return ydl.prepare_filename(info) | |
89087418 PH |
537 | self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4') |
538 | self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4') | |
26e63931 | 539 | # Replace missing fields with 'NA' |
89087418 | 540 | self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4') |
d0d9ade4 S |
541 | self.assertEqual(fname('%(height)d.%(ext)s'), '1080.mp4') |
542 | self.assertEqual(fname('%(height)6d.%(ext)s'), ' 1080.mp4') | |
543 | self.assertEqual(fname('%(height)-6d.%(ext)s'), '1080 .mp4') | |
544 | self.assertEqual(fname('%(height)06d.%(ext)s'), '001080.mp4') | |
545 | self.assertEqual(fname('%(height) 06d.%(ext)s'), ' 01080.mp4') | |
546 | self.assertEqual(fname('%(height) 06d.%(ext)s'), ' 01080.mp4') | |
547 | self.assertEqual(fname('%(height)0 6d.%(ext)s'), ' 01080.mp4') | |
548 | self.assertEqual(fname('%(height)0 6d.%(ext)s'), ' 01080.mp4') | |
549 | self.assertEqual(fname('%(height) 0 6d.%(ext)s'), ' 01080.mp4') | |
15da37c7 S |
550 | self.assertEqual(fname('%%'), '%') |
551 | self.assertEqual(fname('%%%%'), '%%') | |
d0d9ade4 S |
552 | self.assertEqual(fname('%%(height)06d.%(ext)s'), '%(height)06d.mp4') |
553 | self.assertEqual(fname('%(width)06d.%(ext)s'), 'NA.mp4') | |
554 | self.assertEqual(fname('%(width)06d.%%(ext)s'), 'NA.%(ext)s') | |
555 | self.assertEqual(fname('%%(width)06d.%(ext)s'), '%(width)06d.mp4') | |
15da37c7 S |
556 | self.assertEqual(fname('Hello %(title1)s'), 'Hello $PATH') |
557 | self.assertEqual(fname('Hello %(title2)s'), 'Hello %PATH%') | |
26e63931 | 558 | |
c57f7757 PH |
559 | def test_format_note(self): |
560 | ydl = YoutubeDL() | |
561 | self.assertEqual(ydl._format_note({}), '') | |
562 | assertRegexpMatches(self, ydl._format_note({ | |
563 | 'vbr': 10, | |
398dea32 | 564 | }), r'^\s*10k$') |
5d583bdf S |
565 | assertRegexpMatches(self, ydl._format_note({ |
566 | 'fps': 30, | |
398dea32 | 567 | }), r'^30fps$') |
5d583bdf | 568 | |
2b4ecde2 JMF |
569 | def test_postprocessors(self): |
570 | filename = 'post-processor-testfile.mp4' | |
571 | audiofile = filename + '.mp3' | |
572 | ||
573 | class SimplePP(PostProcessor): | |
574 | def run(self, info): | |
2b4ecde2 JMF |
575 | with open(audiofile, 'wt') as f: |
576 | f.write('EXAMPLE') | |
592e97e8 | 577 | return [info['filepath']], info |
2b4ecde2 | 578 | |
592e97e8 | 579 | def run_pp(params, PP): |
2b4ecde2 JMF |
580 | with open(filename, 'wt') as f: |
581 | f.write('EXAMPLE') | |
582 | ydl = YoutubeDL(params) | |
592e97e8 | 583 | ydl.add_post_processor(PP()) |
2b4ecde2 JMF |
584 | ydl.post_process(filename, {'filepath': filename}) |
585 | ||
592e97e8 | 586 | run_pp({'keepvideo': True}, SimplePP) |
2b4ecde2 JMF |
587 | self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename) |
588 | self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile) | |
589 | os.unlink(filename) | |
590 | os.unlink(audiofile) | |
591 | ||
592e97e8 | 592 | run_pp({'keepvideo': False}, SimplePP) |
2b4ecde2 JMF |
593 | self.assertFalse(os.path.exists(filename), '%s exists' % filename) |
594 | self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile) | |
595 | os.unlink(audiofile) | |
596 | ||
592e97e8 JMF |
597 | class ModifierPP(PostProcessor): |
598 | def run(self, info): | |
599 | with open(info['filepath'], 'wt') as f: | |
600 | f.write('MODIFIED') | |
601 | return [], info | |
602 | ||
603 | run_pp({'keepvideo': False}, ModifierPP) | |
604 | self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename) | |
605 | os.unlink(filename) | |
606 | ||
531980d8 JMF |
607 | def test_match_filter(self): |
608 | class FilterYDL(YDL): | |
609 | def __init__(self, *args, **kwargs): | |
610 | super(FilterYDL, self).__init__(*args, **kwargs) | |
611 | self.params['simulate'] = True | |
612 | ||
613 | def process_info(self, info_dict): | |
614 | super(YDL, self).process_info(info_dict) | |
615 | ||
616 | def _match_entry(self, info_dict, incomplete): | |
617 | res = super(FilterYDL, self)._match_entry(info_dict, incomplete) | |
618 | if res is None: | |
619 | self.downloaded_info_dicts.append(info_dict) | |
620 | return res | |
621 | ||
622 | first = { | |
623 | 'id': '1', | |
624 | 'url': TEST_URL, | |
625 | 'title': 'one', | |
626 | 'extractor': 'TEST', | |
627 | 'duration': 30, | |
628 | 'filesize': 10 * 1024, | |
e5a088dc | 629 | 'playlist_id': '42', |
db13c16e S |
630 | 'uploader': "變態妍字幕版 太妍 тест", |
631 | 'creator': "тест ' 123 ' тест--", | |
531980d8 JMF |
632 | } |
633 | second = { | |
634 | 'id': '2', | |
635 | 'url': TEST_URL, | |
636 | 'title': 'two', | |
637 | 'extractor': 'TEST', | |
638 | 'duration': 10, | |
639 | 'description': 'foo', | |
640 | 'filesize': 5 * 1024, | |
e5a088dc | 641 | 'playlist_id': '43', |
db13c16e | 642 | 'uploader': "тест 123", |
531980d8 JMF |
643 | } |
644 | videos = [first, second] | |
645 | ||
646 | def get_videos(filter_=None): | |
647 | ydl = FilterYDL({'match_filter': filter_}) | |
648 | for v in videos: | |
649 | ydl.process_ie_result(v, download=True) | |
650 | return [v['id'] for v in ydl.downloaded_info_dicts] | |
651 | ||
652 | res = get_videos() | |
653 | self.assertEqual(res, ['1', '2']) | |
654 | ||
655 | def f(v): | |
656 | if v['id'] == '1': | |
657 | return None | |
658 | else: | |
659 | return 'Video id is not 1' | |
660 | res = get_videos(f) | |
661 | self.assertEqual(res, ['1']) | |
662 | ||
663 | f = match_filter_func('duration < 30') | |
664 | res = get_videos(f) | |
665 | self.assertEqual(res, ['2']) | |
666 | ||
667 | f = match_filter_func('description = foo') | |
668 | res = get_videos(f) | |
669 | self.assertEqual(res, ['2']) | |
670 | ||
671 | f = match_filter_func('description =? foo') | |
672 | res = get_videos(f) | |
673 | self.assertEqual(res, ['1', '2']) | |
674 | ||
675 | f = match_filter_func('filesize > 5KiB') | |
676 | res = get_videos(f) | |
677 | self.assertEqual(res, ['1']) | |
678 | ||
e5a088dc S |
679 | f = match_filter_func('playlist_id = 42') |
680 | res = get_videos(f) | |
681 | self.assertEqual(res, ['1']) | |
682 | ||
db13c16e S |
683 | f = match_filter_func('uploader = "變態妍字幕版 太妍 тест"') |
684 | res = get_videos(f) | |
685 | self.assertEqual(res, ['1']) | |
686 | ||
687 | f = match_filter_func('uploader != "變態妍字幕版 太妍 тест"') | |
688 | res = get_videos(f) | |
689 | self.assertEqual(res, ['2']) | |
690 | ||
691 | f = match_filter_func('creator = "тест \' 123 \' тест--"') | |
692 | res = get_videos(f) | |
693 | self.assertEqual(res, ['1']) | |
694 | ||
695 | f = match_filter_func("creator = 'тест \\' 123 \\' тест--'") | |
696 | res = get_videos(f) | |
697 | self.assertEqual(res, ['1']) | |
698 | ||
699 | f = match_filter_func(r"creator = 'тест \' 123 \' тест--' & duration > 30") | |
700 | res = get_videos(f) | |
701 | self.assertEqual(res, []) | |
702 | ||
e9eaf3fb JMF |
703 | def test_playlist_items_selection(self): |
704 | entries = [{ | |
705 | 'id': compat_str(i), | |
706 | 'title': compat_str(i), | |
707 | 'url': TEST_URL, | |
708 | } for i in range(1, 5)] | |
709 | playlist = { | |
710 | '_type': 'playlist', | |
711 | 'id': 'test', | |
712 | 'entries': entries, | |
713 | 'extractor': 'test:playlist', | |
714 | 'extractor_key': 'test:playlist', | |
715 | 'webpage_url': 'http://example.com', | |
716 | } | |
717 | ||
718 | def get_ids(params): | |
719 | ydl = YDL(params) | |
720 | # make a copy because the dictionary can be modified | |
721 | ydl.process_ie_result(playlist.copy()) | |
722 | return [int(v['id']) for v in ydl.downloaded_info_dicts] | |
723 | ||
724 | result = get_ids({}) | |
725 | self.assertEqual(result, [1, 2, 3, 4]) | |
726 | ||
727 | result = get_ids({'playlistend': 10}) | |
728 | self.assertEqual(result, [1, 2, 3, 4]) | |
729 | ||
730 | result = get_ids({'playlistend': 2}) | |
731 | self.assertEqual(result, [1, 2]) | |
732 | ||
733 | result = get_ids({'playliststart': 10}) | |
734 | self.assertEqual(result, []) | |
735 | ||
736 | result = get_ids({'playliststart': 2}) | |
737 | self.assertEqual(result, [2, 3, 4]) | |
738 | ||
739 | result = get_ids({'playlist_items': '2-4'}) | |
740 | self.assertEqual(result, [2, 3, 4]) | |
741 | ||
742 | result = get_ids({'playlist_items': '2,4'}) | |
743 | self.assertEqual(result, [2, 4]) | |
744 | ||
745 | result = get_ids({'playlist_items': '10'}) | |
746 | self.assertEqual(result, []) | |
747 | ||
e37afbe0 JMF |
748 | def test_urlopen_no_file_protocol(self): |
749 | # see https://github.com/rg3/youtube-dl/issues/8227 | |
750 | ydl = YDL() | |
751 | self.assertRaises(compat_urllib_error.URLError, ydl.urlopen, 'file:///etc/passwd') | |
752 | ||
b286f201 YCH |
753 | def test_do_not_override_ie_key_in_url_transparent(self): |
754 | ydl = YDL() | |
755 | ||
756 | class Foo1IE(InfoExtractor): | |
757 | _VALID_URL = r'foo1:' | |
758 | ||
759 | def _real_extract(self, url): | |
760 | return { | |
761 | '_type': 'url_transparent', | |
762 | 'url': 'foo2:', | |
763 | 'ie_key': 'Foo2', | |
51350db5 | 764 | 'title': 'foo1 title' |
b286f201 YCH |
765 | } |
766 | ||
767 | class Foo2IE(InfoExtractor): | |
768 | _VALID_URL = r'foo2:' | |
769 | ||
770 | def _real_extract(self, url): | |
771 | return { | |
772 | '_type': 'url', | |
773 | 'url': 'foo3:', | |
774 | 'ie_key': 'Foo3', | |
775 | } | |
776 | ||
777 | class Foo3IE(InfoExtractor): | |
778 | _VALID_URL = r'foo3:' | |
779 | ||
780 | def _real_extract(self, url): | |
51350db5 | 781 | return _make_result([{'url': TEST_URL}], title='foo3 title') |
b286f201 YCH |
782 | |
783 | ydl.add_info_extractor(Foo1IE(ydl)) | |
784 | ydl.add_info_extractor(Foo2IE(ydl)) | |
785 | ydl.add_info_extractor(Foo3IE(ydl)) | |
786 | ydl.extract_info('foo1:') | |
787 | downloaded = ydl.downloaded_info_dicts[0] | |
788 | self.assertEqual(downloaded['url'], TEST_URL) | |
51350db5 | 789 | self.assertEqual(downloaded['title'], 'foo1 title') |
b286f201 | 790 | |
2b4ecde2 | 791 | |
e028d0d1 JMF |
792 | if __name__ == '__main__': |
793 | unittest.main() |