]> Raphaƫl G. Git Repositories - youtubedl/blob - test/test_YoutubeDL.py
Imported Upstream version 2014.10.30
[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 from test.helper import FakeYDL, assertRegexpMatches
12 from youtube_dl import YoutubeDL
13 from youtube_dl.extractor import YoutubeIE
14
15
16 class YDL(FakeYDL):
17 def __init__(self, *args, **kwargs):
18 super(YDL, self).__init__(*args, **kwargs)
19 self.downloaded_info_dicts = []
20 self.msgs = []
21
22 def process_info(self, info_dict):
23 self.downloaded_info_dicts.append(info_dict)
24
25 def to_screen(self, msg):
26 self.msgs.append(msg)
27
28
29 def _make_result(formats, **kwargs):
30 res = {
31 'formats': formats,
32 'id': 'testid',
33 'title': 'testttitle',
34 'extractor': 'testex',
35 }
36 res.update(**kwargs)
37 return res
38
39
40 class TestFormatSelection(unittest.TestCase):
41 def test_prefer_free_formats(self):
42 # Same resolution => download webm
43 ydl = YDL()
44 ydl.params['prefer_free_formats'] = True
45 formats = [
46 {'ext': 'webm', 'height': 460, 'url': 'x'},
47 {'ext': 'mp4', 'height': 460, 'url': 'y'},
48 ]
49 info_dict = _make_result(formats)
50 yie = YoutubeIE(ydl)
51 yie._sort_formats(info_dict['formats'])
52 ydl.process_ie_result(info_dict)
53 downloaded = ydl.downloaded_info_dicts[0]
54 self.assertEqual(downloaded['ext'], 'webm')
55
56 # Different resolution => download best quality (mp4)
57 ydl = YDL()
58 ydl.params['prefer_free_formats'] = True
59 formats = [
60 {'ext': 'webm', 'height': 720, 'url': 'a'},
61 {'ext': 'mp4', 'height': 1080, 'url': 'b'},
62 ]
63 info_dict['formats'] = formats
64 yie = YoutubeIE(ydl)
65 yie._sort_formats(info_dict['formats'])
66 ydl.process_ie_result(info_dict)
67 downloaded = ydl.downloaded_info_dicts[0]
68 self.assertEqual(downloaded['ext'], 'mp4')
69
70 # No prefer_free_formats => prefer mp4 and flv for greater compatibility
71 ydl = YDL()
72 ydl.params['prefer_free_formats'] = False
73 formats = [
74 {'ext': 'webm', 'height': 720, 'url': '_'},
75 {'ext': 'mp4', 'height': 720, 'url': '_'},
76 {'ext': 'flv', 'height': 720, 'url': '_'},
77 ]
78 info_dict['formats'] = formats
79 yie = YoutubeIE(ydl)
80 yie._sort_formats(info_dict['formats'])
81 ydl.process_ie_result(info_dict)
82 downloaded = ydl.downloaded_info_dicts[0]
83 self.assertEqual(downloaded['ext'], 'mp4')
84
85 ydl = YDL()
86 ydl.params['prefer_free_formats'] = False
87 formats = [
88 {'ext': 'flv', 'height': 720, 'url': '_'},
89 {'ext': 'webm', 'height': 720, 'url': '_'},
90 ]
91 info_dict['formats'] = formats
92 yie = YoutubeIE(ydl)
93 yie._sort_formats(info_dict['formats'])
94 ydl.process_ie_result(info_dict)
95 downloaded = ydl.downloaded_info_dicts[0]
96 self.assertEqual(downloaded['ext'], 'flv')
97
98 def test_format_limit(self):
99 formats = [
100 {'format_id': 'meh', 'url': 'http://example.com/meh', 'preference': 1},
101 {'format_id': 'good', 'url': 'http://example.com/good', 'preference': 2},
102 {'format_id': 'great', 'url': 'http://example.com/great', 'preference': 3},
103 {'format_id': 'excellent', 'url': 'http://example.com/exc', 'preference': 4},
104 ]
105 info_dict = _make_result(formats)
106
107 ydl = YDL()
108 ydl.process_ie_result(info_dict)
109 downloaded = ydl.downloaded_info_dicts[0]
110 self.assertEqual(downloaded['format_id'], 'excellent')
111
112 ydl = YDL({'format_limit': 'good'})
113 assert ydl.params['format_limit'] == 'good'
114 ydl.process_ie_result(info_dict.copy())
115 downloaded = ydl.downloaded_info_dicts[0]
116 self.assertEqual(downloaded['format_id'], 'good')
117
118 ydl = YDL({'format_limit': 'great', 'format': 'all'})
119 ydl.process_ie_result(info_dict.copy())
120 self.assertEqual(ydl.downloaded_info_dicts[0]['format_id'], 'meh')
121 self.assertEqual(ydl.downloaded_info_dicts[1]['format_id'], 'good')
122 self.assertEqual(ydl.downloaded_info_dicts[2]['format_id'], 'great')
123 self.assertTrue('3' in ydl.msgs[0])
124
125 ydl = YDL()
126 ydl.params['format_limit'] = 'excellent'
127 ydl.process_ie_result(info_dict.copy())
128 downloaded = ydl.downloaded_info_dicts[0]
129 self.assertEqual(downloaded['format_id'], 'excellent')
130
131 def test_format_selection(self):
132 formats = [
133 {'format_id': '35', 'ext': 'mp4', 'preference': 1, 'url': '_'},
134 {'format_id': '45', 'ext': 'webm', 'preference': 2, 'url': '_'},
135 {'format_id': '47', 'ext': 'webm', 'preference': 3, 'url': '_'},
136 {'format_id': '2', 'ext': 'flv', 'preference': 4, 'url': '_'},
137 ]
138 info_dict = _make_result(formats)
139
140 ydl = YDL({'format': '20/47'})
141 ydl.process_ie_result(info_dict.copy())
142 downloaded = ydl.downloaded_info_dicts[0]
143 self.assertEqual(downloaded['format_id'], '47')
144
145 ydl = YDL({'format': '20/71/worst'})
146 ydl.process_ie_result(info_dict.copy())
147 downloaded = ydl.downloaded_info_dicts[0]
148 self.assertEqual(downloaded['format_id'], '35')
149
150 ydl = YDL()
151 ydl.process_ie_result(info_dict.copy())
152 downloaded = ydl.downloaded_info_dicts[0]
153 self.assertEqual(downloaded['format_id'], '2')
154
155 ydl = YDL({'format': 'webm/mp4'})
156 ydl.process_ie_result(info_dict.copy())
157 downloaded = ydl.downloaded_info_dicts[0]
158 self.assertEqual(downloaded['format_id'], '47')
159
160 ydl = YDL({'format': '3gp/40/mp4'})
161 ydl.process_ie_result(info_dict.copy())
162 downloaded = ydl.downloaded_info_dicts[0]
163 self.assertEqual(downloaded['format_id'], '35')
164
165 def test_format_selection_audio(self):
166 formats = [
167 {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none', 'url': '_'},
168 {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none', 'url': '_'},
169 {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none', 'url': '_'},
170 {'format_id': 'vid', 'ext': 'mp4', 'preference': 4, 'url': '_'},
171 ]
172 info_dict = _make_result(formats)
173
174 ydl = YDL({'format': 'bestaudio'})
175 ydl.process_ie_result(info_dict.copy())
176 downloaded = ydl.downloaded_info_dicts[0]
177 self.assertEqual(downloaded['format_id'], 'audio-high')
178
179 ydl = YDL({'format': 'worstaudio'})
180 ydl.process_ie_result(info_dict.copy())
181 downloaded = ydl.downloaded_info_dicts[0]
182 self.assertEqual(downloaded['format_id'], 'audio-low')
183
184 formats = [
185 {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': '_'},
186 {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': '_'},
187 ]
188 info_dict = _make_result(formats)
189
190 ydl = YDL({'format': 'bestaudio/worstaudio/best'})
191 ydl.process_ie_result(info_dict.copy())
192 downloaded = ydl.downloaded_info_dicts[0]
193 self.assertEqual(downloaded['format_id'], 'vid-high')
194
195 def test_format_selection_video(self):
196 formats = [
197 {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none', 'url': '_'},
198 {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none', 'url': '_'},
199 {'format_id': 'vid', 'ext': 'mp4', 'preference': 3, 'url': '_'},
200 ]
201 info_dict = _make_result(formats)
202
203 ydl = YDL({'format': 'bestvideo'})
204 ydl.process_ie_result(info_dict.copy())
205 downloaded = ydl.downloaded_info_dicts[0]
206 self.assertEqual(downloaded['format_id'], 'dash-video-high')
207
208 ydl = YDL({'format': 'worstvideo'})
209 ydl.process_ie_result(info_dict.copy())
210 downloaded = ydl.downloaded_info_dicts[0]
211 self.assertEqual(downloaded['format_id'], 'dash-video-low')
212
213 def test_youtube_format_selection(self):
214 order = [
215 '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '36', '17', '13',
216 # Apple HTTP Live Streaming
217 '96', '95', '94', '93', '92', '132', '151',
218 # 3D
219 '85', '84', '102', '83', '101', '82', '100',
220 # Dash video
221 '138', '137', '248', '136', '247', '135', '246',
222 '245', '244', '134', '243', '133', '242', '160',
223 # Dash audio
224 '141', '172', '140', '171', '139',
225 ]
226
227 for f1id, f2id in zip(order, order[1:]):
228 f1 = YoutubeIE._formats[f1id].copy()
229 f1['format_id'] = f1id
230 f1['url'] = 'url:' + f1id
231 f2 = YoutubeIE._formats[f2id].copy()
232 f2['format_id'] = f2id
233 f2['url'] = 'url:' + f2id
234
235 info_dict = _make_result([f1, f2], extractor='youtube')
236 ydl = YDL()
237 yie = YoutubeIE(ydl)
238 yie._sort_formats(info_dict['formats'])
239 ydl.process_ie_result(info_dict)
240 downloaded = ydl.downloaded_info_dicts[0]
241 self.assertEqual(downloaded['format_id'], f1id)
242
243 info_dict = _make_result([f2, f1], extractor='youtube')
244 ydl = YDL()
245 yie = YoutubeIE(ydl)
246 yie._sort_formats(info_dict['formats'])
247 ydl.process_ie_result(info_dict)
248 downloaded = ydl.downloaded_info_dicts[0]
249 self.assertEqual(downloaded['format_id'], f1id)
250
251 def test_add_extra_info(self):
252 test_dict = {
253 'extractor': 'Foo',
254 }
255 extra_info = {
256 'extractor': 'Bar',
257 'playlist': 'funny videos',
258 }
259 YDL.add_extra_info(test_dict, extra_info)
260 self.assertEqual(test_dict['extractor'], 'Foo')
261 self.assertEqual(test_dict['playlist'], 'funny videos')
262
263 def test_prepare_filename(self):
264 info = {
265 'id': '1234',
266 'ext': 'mp4',
267 'width': None,
268 }
269 def fname(templ):
270 ydl = YoutubeDL({'outtmpl': templ})
271 return ydl.prepare_filename(info)
272 self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4')
273 self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4')
274 # Replace missing fields with 'NA'
275 self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4')
276
277 def test_format_note(self):
278 ydl = YoutubeDL()
279 self.assertEqual(ydl._format_note({}), '')
280 assertRegexpMatches(self, ydl._format_note({
281 'vbr': 10,
282 }), '^\s*10k$')
283
284 if __name__ == '__main__':
285 unittest.main()