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