]> Raphaƫl G. Git Repositories - youtubedl/blob - test/test_YoutubeDL.py
debian/control: Update list of supported sites.
[youtubedl] / test / test_YoutubeDL.py
1 #!/usr/bin/env python
2
3 from __future__ import unicode_literals
4
5 # Allow direct execution
6 import os
7 import sys
8 import unittest
9 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10
11 import copy
12
13 from test.helper import FakeYDL, assertRegexpMatches
14 from youtube_dl import YoutubeDL
15 from youtube_dl.extractor import YoutubeIE
16
17
18 class YDL(FakeYDL):
19 def __init__(self, *args, **kwargs):
20 super(YDL, self).__init__(*args, **kwargs)
21 self.downloaded_info_dicts = []
22 self.msgs = []
23
24 def process_info(self, info_dict):
25 self.downloaded_info_dicts.append(info_dict)
26
27 def to_screen(self, msg):
28 self.msgs.append(msg)
29
30
31 def _make_result(formats, **kwargs):
32 res = {
33 'formats': formats,
34 'id': 'testid',
35 'title': 'testttitle',
36 'extractor': 'testex',
37 }
38 res.update(**kwargs)
39 return res
40
41
42 class TestFormatSelection(unittest.TestCase):
43 def test_prefer_free_formats(self):
44 # Same resolution => download webm
45 ydl = YDL()
46 ydl.params['prefer_free_formats'] = True
47 formats = [
48 {'ext': 'webm', 'height': 460, 'url': 'x'},
49 {'ext': 'mp4', 'height': 460, 'url': 'y'},
50 ]
51 info_dict = _make_result(formats)
52 yie = YoutubeIE(ydl)
53 yie._sort_formats(info_dict['formats'])
54 ydl.process_ie_result(info_dict)
55 downloaded = ydl.downloaded_info_dicts[0]
56 self.assertEqual(downloaded['ext'], 'webm')
57
58 # Different resolution => download best quality (mp4)
59 ydl = YDL()
60 ydl.params['prefer_free_formats'] = True
61 formats = [
62 {'ext': 'webm', 'height': 720, 'url': 'a'},
63 {'ext': 'mp4', 'height': 1080, 'url': 'b'},
64 ]
65 info_dict['formats'] = formats
66 yie = YoutubeIE(ydl)
67 yie._sort_formats(info_dict['formats'])
68 ydl.process_ie_result(info_dict)
69 downloaded = ydl.downloaded_info_dicts[0]
70 self.assertEqual(downloaded['ext'], 'mp4')
71
72 # No prefer_free_formats => prefer mp4 and flv for greater compatibility
73 ydl = YDL()
74 ydl.params['prefer_free_formats'] = False
75 formats = [
76 {'ext': 'webm', 'height': 720, 'url': '_'},
77 {'ext': 'mp4', 'height': 720, 'url': '_'},
78 {'ext': 'flv', 'height': 720, 'url': '_'},
79 ]
80 info_dict['formats'] = formats
81 yie = YoutubeIE(ydl)
82 yie._sort_formats(info_dict['formats'])
83 ydl.process_ie_result(info_dict)
84 downloaded = ydl.downloaded_info_dicts[0]
85 self.assertEqual(downloaded['ext'], 'mp4')
86
87 ydl = YDL()
88 ydl.params['prefer_free_formats'] = False
89 formats = [
90 {'ext': 'flv', 'height': 720, 'url': '_'},
91 {'ext': 'webm', 'height': 720, 'url': '_'},
92 ]
93 info_dict['formats'] = formats
94 yie = YoutubeIE(ydl)
95 yie._sort_formats(info_dict['formats'])
96 ydl.process_ie_result(info_dict)
97 downloaded = ydl.downloaded_info_dicts[0]
98 self.assertEqual(downloaded['ext'], 'flv')
99
100 def test_format_limit(self):
101 formats = [
102 {'format_id': 'meh', 'url': 'http://example.com/meh', 'preference': 1},
103 {'format_id': 'good', 'url': 'http://example.com/good', 'preference': 2},
104 {'format_id': 'great', 'url': 'http://example.com/great', 'preference': 3},
105 {'format_id': 'excellent', 'url': 'http://example.com/exc', 'preference': 4},
106 ]
107 info_dict = _make_result(formats)
108
109 ydl = YDL()
110 ydl.process_ie_result(info_dict)
111 downloaded = ydl.downloaded_info_dicts[0]
112 self.assertEqual(downloaded['format_id'], 'excellent')
113
114 ydl = YDL({'format_limit': 'good'})
115 assert ydl.params['format_limit'] == 'good'
116 ydl.process_ie_result(info_dict.copy())
117 downloaded = ydl.downloaded_info_dicts[0]
118 self.assertEqual(downloaded['format_id'], 'good')
119
120 ydl = YDL({'format_limit': 'great', 'format': 'all'})
121 ydl.process_ie_result(info_dict.copy())
122 self.assertEqual(ydl.downloaded_info_dicts[0]['format_id'], 'meh')
123 self.assertEqual(ydl.downloaded_info_dicts[1]['format_id'], 'good')
124 self.assertEqual(ydl.downloaded_info_dicts[2]['format_id'], 'great')
125 self.assertTrue('3' in ydl.msgs[0])
126
127 ydl = YDL()
128 ydl.params['format_limit'] = 'excellent'
129 ydl.process_ie_result(info_dict.copy())
130 downloaded = ydl.downloaded_info_dicts[0]
131 self.assertEqual(downloaded['format_id'], 'excellent')
132
133 def test_format_selection(self):
134 formats = [
135 {'format_id': '35', 'ext': 'mp4', 'preference': 1, 'url': '_'},
136 {'format_id': '45', 'ext': 'webm', 'preference': 2, 'url': '_'},
137 {'format_id': '47', 'ext': 'webm', 'preference': 3, 'url': '_'},
138 {'format_id': '2', 'ext': 'flv', 'preference': 4, 'url': '_'},
139 ]
140 info_dict = _make_result(formats)
141
142 ydl = YDL({'format': '20/47'})
143 ydl.process_ie_result(info_dict.copy())
144 downloaded = ydl.downloaded_info_dicts[0]
145 self.assertEqual(downloaded['format_id'], '47')
146
147 ydl = YDL({'format': '20/71/worst'})
148 ydl.process_ie_result(info_dict.copy())
149 downloaded = ydl.downloaded_info_dicts[0]
150 self.assertEqual(downloaded['format_id'], '35')
151
152 ydl = YDL()
153 ydl.process_ie_result(info_dict.copy())
154 downloaded = ydl.downloaded_info_dicts[0]
155 self.assertEqual(downloaded['format_id'], '2')
156
157 ydl = YDL({'format': 'webm/mp4'})
158 ydl.process_ie_result(info_dict.copy())
159 downloaded = ydl.downloaded_info_dicts[0]
160 self.assertEqual(downloaded['format_id'], '47')
161
162 ydl = YDL({'format': '3gp/40/mp4'})
163 ydl.process_ie_result(info_dict.copy())
164 downloaded = ydl.downloaded_info_dicts[0]
165 self.assertEqual(downloaded['format_id'], '35')
166
167 def test_format_selection_audio(self):
168 formats = [
169 {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none', 'url': '_'},
170 {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none', 'url': '_'},
171 {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none', 'url': '_'},
172 {'format_id': 'vid', 'ext': 'mp4', 'preference': 4, 'url': '_'},
173 ]
174 info_dict = _make_result(formats)
175
176 ydl = YDL({'format': 'bestaudio'})
177 ydl.process_ie_result(info_dict.copy())
178 downloaded = ydl.downloaded_info_dicts[0]
179 self.assertEqual(downloaded['format_id'], 'audio-high')
180
181 ydl = YDL({'format': 'worstaudio'})
182 ydl.process_ie_result(info_dict.copy())
183 downloaded = ydl.downloaded_info_dicts[0]
184 self.assertEqual(downloaded['format_id'], 'audio-low')
185
186 formats = [
187 {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': '_'},
188 {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': '_'},
189 ]
190 info_dict = _make_result(formats)
191
192 ydl = YDL({'format': 'bestaudio/worstaudio/best'})
193 ydl.process_ie_result(info_dict.copy())
194 downloaded = ydl.downloaded_info_dicts[0]
195 self.assertEqual(downloaded['format_id'], 'vid-high')
196
197 def test_format_selection_audio_exts(self):
198 formats = [
199 {'format_id': 'mp3-64', 'ext': 'mp3', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
200 {'format_id': 'ogg-64', 'ext': 'ogg', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
201 {'format_id': 'aac-64', 'ext': 'aac', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
202 {'format_id': 'mp3-32', 'ext': 'mp3', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'},
203 {'format_id': 'aac-32', 'ext': 'aac', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'},
204 ]
205
206 info_dict = _make_result(formats)
207 ydl = YDL({'format': 'best'})
208 ie = YoutubeIE(ydl)
209 ie._sort_formats(info_dict['formats'])
210 ydl.process_ie_result(copy.deepcopy(info_dict))
211 downloaded = ydl.downloaded_info_dicts[0]
212 self.assertEqual(downloaded['format_id'], 'aac-64')
213
214 ydl = YDL({'format': 'mp3'})
215 ie = YoutubeIE(ydl)
216 ie._sort_formats(info_dict['formats'])
217 ydl.process_ie_result(copy.deepcopy(info_dict))
218 downloaded = ydl.downloaded_info_dicts[0]
219 self.assertEqual(downloaded['format_id'], 'mp3-64')
220
221 ydl = YDL({'prefer_free_formats': True})
222 ie = YoutubeIE(ydl)
223 ie._sort_formats(info_dict['formats'])
224 ydl.process_ie_result(copy.deepcopy(info_dict))
225 downloaded = ydl.downloaded_info_dicts[0]
226 self.assertEqual(downloaded['format_id'], 'ogg-64')
227
228 def test_format_selection_video(self):
229 formats = [
230 {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none', 'url': '_'},
231 {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none', 'url': '_'},
232 {'format_id': 'vid', 'ext': 'mp4', 'preference': 3, 'url': '_'},
233 ]
234 info_dict = _make_result(formats)
235
236 ydl = YDL({'format': 'bestvideo'})
237 ydl.process_ie_result(info_dict.copy())
238 downloaded = ydl.downloaded_info_dicts[0]
239 self.assertEqual(downloaded['format_id'], 'dash-video-high')
240
241 ydl = YDL({'format': 'worstvideo'})
242 ydl.process_ie_result(info_dict.copy())
243 downloaded = ydl.downloaded_info_dicts[0]
244 self.assertEqual(downloaded['format_id'], 'dash-video-low')
245
246 def test_youtube_format_selection(self):
247 order = [
248 '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '36', '17', '13',
249 # Apple HTTP Live Streaming
250 '96', '95', '94', '93', '92', '132', '151',
251 # 3D
252 '85', '84', '102', '83', '101', '82', '100',
253 # Dash video
254 '137', '248', '136', '247', '135', '246',
255 '245', '244', '134', '243', '133', '242', '160',
256 # Dash audio
257 '141', '172', '140', '171', '139',
258 ]
259
260 for f1id, f2id in zip(order, order[1:]):
261 f1 = YoutubeIE._formats[f1id].copy()
262 f1['format_id'] = f1id
263 f1['url'] = 'url:' + f1id
264 f2 = YoutubeIE._formats[f2id].copy()
265 f2['format_id'] = f2id
266 f2['url'] = 'url:' + f2id
267
268 info_dict = _make_result([f1, f2], extractor='youtube')
269 ydl = YDL()
270 yie = YoutubeIE(ydl)
271 yie._sort_formats(info_dict['formats'])
272 ydl.process_ie_result(info_dict)
273 downloaded = ydl.downloaded_info_dicts[0]
274 self.assertEqual(downloaded['format_id'], f1id)
275
276 info_dict = _make_result([f2, f1], extractor='youtube')
277 ydl = YDL()
278 yie = YoutubeIE(ydl)
279 yie._sort_formats(info_dict['formats'])
280 ydl.process_ie_result(info_dict)
281 downloaded = ydl.downloaded_info_dicts[0]
282 self.assertEqual(downloaded['format_id'], f1id)
283
284 def test_format_filtering(self):
285 formats = [
286 {'format_id': 'A', 'filesize': 500, 'width': 1000},
287 {'format_id': 'B', 'filesize': 1000, 'width': 500},
288 {'format_id': 'C', 'filesize': 1000, 'width': 400},
289 {'format_id': 'D', 'filesize': 2000, 'width': 600},
290 {'format_id': 'E', 'filesize': 3000},
291 {'format_id': 'F'},
292 {'format_id': 'G', 'filesize': 1000000},
293 ]
294 for f in formats:
295 f['url'] = 'http://_/'
296 f['ext'] = 'unknown'
297 info_dict = _make_result(formats)
298
299 ydl = YDL({'format': 'best[filesize<3000]'})
300 ydl.process_ie_result(info_dict)
301 downloaded = ydl.downloaded_info_dicts[0]
302 self.assertEqual(downloaded['format_id'], 'D')
303
304 ydl = YDL({'format': 'best[filesize<=3000]'})
305 ydl.process_ie_result(info_dict)
306 downloaded = ydl.downloaded_info_dicts[0]
307 self.assertEqual(downloaded['format_id'], 'E')
308
309 ydl = YDL({'format': 'best[filesize <= ? 3000]'})
310 ydl.process_ie_result(info_dict)
311 downloaded = ydl.downloaded_info_dicts[0]
312 self.assertEqual(downloaded['format_id'], 'F')
313
314 ydl = YDL({'format': 'best [filesize = 1000] [width>450]'})
315 ydl.process_ie_result(info_dict)
316 downloaded = ydl.downloaded_info_dicts[0]
317 self.assertEqual(downloaded['format_id'], 'B')
318
319 ydl = YDL({'format': 'best [filesize = 1000] [width!=450]'})
320 ydl.process_ie_result(info_dict)
321 downloaded = ydl.downloaded_info_dicts[0]
322 self.assertEqual(downloaded['format_id'], 'C')
323
324 ydl = YDL({'format': '[filesize>?1]'})
325 ydl.process_ie_result(info_dict)
326 downloaded = ydl.downloaded_info_dicts[0]
327 self.assertEqual(downloaded['format_id'], 'G')
328
329 ydl = YDL({'format': '[filesize<1M]'})
330 ydl.process_ie_result(info_dict)
331 downloaded = ydl.downloaded_info_dicts[0]
332 self.assertEqual(downloaded['format_id'], 'E')
333
334 ydl = YDL({'format': '[filesize<1MiB]'})
335 ydl.process_ie_result(info_dict)
336 downloaded = ydl.downloaded_info_dicts[0]
337 self.assertEqual(downloaded['format_id'], 'G')
338
339 def test_add_extra_info(self):
340 test_dict = {
341 'extractor': 'Foo',
342 }
343 extra_info = {
344 'extractor': 'Bar',
345 'playlist': 'funny videos',
346 }
347 YDL.add_extra_info(test_dict, extra_info)
348 self.assertEqual(test_dict['extractor'], 'Foo')
349 self.assertEqual(test_dict['playlist'], 'funny videos')
350
351 def test_prepare_filename(self):
352 info = {
353 'id': '1234',
354 'ext': 'mp4',
355 'width': None,
356 }
357
358 def fname(templ):
359 ydl = YoutubeDL({'outtmpl': templ})
360 return ydl.prepare_filename(info)
361 self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4')
362 self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4')
363 # Replace missing fields with 'NA'
364 self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4')
365
366 def test_format_note(self):
367 ydl = YoutubeDL()
368 self.assertEqual(ydl._format_note({}), '')
369 assertRegexpMatches(self, ydl._format_note({
370 'vbr': 10,
371 }), '^\s*10k$')
372
373 if __name__ == '__main__':
374 unittest.main()