]>
Commit | Line | Data |
---|---|---|
e028d0d1 JMF |
1 | #!/usr/bin/env python |
2 | ||
89087418 PH |
3 | from __future__ import unicode_literals |
4 | ||
5d254f77 PH |
5 | # Allow direct execution |
6 | import os | |
e028d0d1 JMF |
7 | import sys |
8 | import unittest | |
5d254f77 | 9 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
e028d0d1 | 10 | |
0217c783 PH |
11 | import copy |
12 | ||
c57f7757 | 13 | from test.helper import FakeYDL, assertRegexpMatches |
26e63931 | 14 | from youtube_dl import YoutubeDL |
3d4a70b8 | 15 | from youtube_dl.extractor import YoutubeIE |
e028d0d1 | 16 | |
e028d0d1 JMF |
17 | |
18 | class YDL(FakeYDL): | |
f4d96df0 PH |
19 | def __init__(self, *args, **kwargs): |
20 | super(YDL, self).__init__(*args, **kwargs) | |
e028d0d1 | 21 | self.downloaded_info_dicts = [] |
f4d96df0 | 22 | self.msgs = [] |
5d254f77 | 23 | |
e028d0d1 JMF |
24 | def process_info(self, info_dict): |
25 | self.downloaded_info_dicts.append(info_dict) | |
26 | ||
f4d96df0 PH |
27 | def to_screen(self, msg): |
28 | self.msgs.append(msg) | |
29 | ||
5d254f77 | 30 | |
3537b93d PH |
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 | ||
e028d0d1 JMF |
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 | |
5d254f77 | 47 | formats = [ |
3537b93d PH |
48 | {'ext': 'webm', 'height': 460, 'url': 'x'}, |
49 | {'ext': 'mp4', 'height': 460, 'url': 'y'}, | |
5d254f77 | 50 | ] |
3537b93d | 51 | info_dict = _make_result(formats) |
3d4a70b8 PH |
52 | yie = YoutubeIE(ydl) |
53 | yie._sort_formats(info_dict['formats']) | |
e028d0d1 JMF |
54 | ydl.process_ie_result(info_dict) |
55 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 56 | self.assertEqual(downloaded['ext'], 'webm') |
e028d0d1 JMF |
57 | |
58 | # Different resolution => download best quality (mp4) | |
59 | ydl = YDL() | |
60 | ydl.params['prefer_free_formats'] = True | |
5d254f77 | 61 | formats = [ |
3537b93d PH |
62 | {'ext': 'webm', 'height': 720, 'url': 'a'}, |
63 | {'ext': 'mp4', 'height': 1080, 'url': 'b'}, | |
5d254f77 | 64 | ] |
89087418 | 65 | info_dict['formats'] = formats |
3d4a70b8 PH |
66 | yie = YoutubeIE(ydl) |
67 | yie._sort_formats(info_dict['formats']) | |
e028d0d1 JMF |
68 | ydl.process_ie_result(info_dict) |
69 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 70 | self.assertEqual(downloaded['ext'], 'mp4') |
e028d0d1 | 71 | |
1c783bca | 72 | # No prefer_free_formats => prefer mp4 and flv for greater compatibility |
e028d0d1 JMF |
73 | ydl = YDL() |
74 | ydl.params['prefer_free_formats'] = False | |
5d254f77 | 75 | formats = [ |
3537b93d PH |
76 | {'ext': 'webm', 'height': 720, 'url': '_'}, |
77 | {'ext': 'mp4', 'height': 720, 'url': '_'}, | |
78 | {'ext': 'flv', 'height': 720, 'url': '_'}, | |
5d254f77 | 79 | ] |
89087418 | 80 | info_dict['formats'] = formats |
3d4a70b8 PH |
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] | |
89087418 | 85 | self.assertEqual(downloaded['ext'], 'mp4') |
3d4a70b8 PH |
86 | |
87 | ydl = YDL() | |
88 | ydl.params['prefer_free_formats'] = False | |
89 | formats = [ | |
3537b93d PH |
90 | {'ext': 'flv', 'height': 720, 'url': '_'}, |
91 | {'ext': 'webm', 'height': 720, 'url': '_'}, | |
3d4a70b8 | 92 | ] |
89087418 | 93 | info_dict['formats'] = formats |
3d4a70b8 PH |
94 | yie = YoutubeIE(ydl) |
95 | yie._sort_formats(info_dict['formats']) | |
e028d0d1 JMF |
96 | ydl.process_ie_result(info_dict) |
97 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 98 | self.assertEqual(downloaded['ext'], 'flv') |
e028d0d1 | 99 | |
f4d96df0 PH |
100 | def test_format_limit(self): |
101 | formats = [ | |
89087418 PH |
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}, | |
f4d96df0 | 106 | ] |
3537b93d | 107 | info_dict = _make_result(formats) |
f4d96df0 PH |
108 | |
109 | ydl = YDL() | |
110 | ydl.process_ie_result(info_dict) | |
111 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 112 | self.assertEqual(downloaded['format_id'], 'excellent') |
f4d96df0 PH |
113 | |
114 | ydl = YDL({'format_limit': 'good'}) | |
115 | assert ydl.params['format_limit'] == 'good' | |
8e3e0322 | 116 | ydl.process_ie_result(info_dict.copy()) |
f4d96df0 | 117 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 118 | self.assertEqual(downloaded['format_id'], 'good') |
f4d96df0 PH |
119 | |
120 | ydl = YDL({'format_limit': 'great', 'format': 'all'}) | |
8e3e0322 | 121 | ydl.process_ie_result(info_dict.copy()) |
89087418 PH |
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') | |
f4d96df0 PH |
125 | self.assertTrue('3' in ydl.msgs[0]) |
126 | ||
127 | ydl = YDL() | |
128 | ydl.params['format_limit'] = 'excellent' | |
8e3e0322 | 129 | ydl.process_ie_result(info_dict.copy()) |
f4d96df0 | 130 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 131 | self.assertEqual(downloaded['format_id'], 'excellent') |
f4d96df0 | 132 | |
a9c58ad9 JMF |
133 | def test_format_selection(self): |
134 | formats = [ | |
3537b93d PH |
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': '_'}, | |
a9c58ad9 | 139 | ] |
3537b93d | 140 | info_dict = _make_result(formats) |
a9c58ad9 | 141 | |
89087418 | 142 | ydl = YDL({'format': '20/47'}) |
8e3e0322 | 143 | ydl.process_ie_result(info_dict.copy()) |
a9c58ad9 | 144 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 145 | self.assertEqual(downloaded['format_id'], '47') |
a9c58ad9 | 146 | |
89087418 | 147 | ydl = YDL({'format': '20/71/worst'}) |
8e3e0322 | 148 | ydl.process_ie_result(info_dict.copy()) |
a9c58ad9 | 149 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 150 | self.assertEqual(downloaded['format_id'], '35') |
a9c58ad9 JMF |
151 | |
152 | ydl = YDL() | |
8e3e0322 | 153 | ydl.process_ie_result(info_dict.copy()) |
a9c58ad9 | 154 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 155 | self.assertEqual(downloaded['format_id'], '2') |
a9c58ad9 | 156 | |
89087418 | 157 | ydl = YDL({'format': 'webm/mp4'}) |
8e3e0322 | 158 | ydl.process_ie_result(info_dict.copy()) |
49e86983 | 159 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 160 | self.assertEqual(downloaded['format_id'], '47') |
49e86983 | 161 | |
89087418 | 162 | ydl = YDL({'format': '3gp/40/mp4'}) |
8e3e0322 | 163 | ydl.process_ie_result(info_dict.copy()) |
49e86983 | 164 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 165 | self.assertEqual(downloaded['format_id'], '35') |
49e86983 | 166 | |
ba7678f9 PH |
167 | def test_format_selection_audio(self): |
168 | formats = [ | |
3537b93d PH |
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': '_'}, | |
ba7678f9 | 173 | ] |
3537b93d | 174 | info_dict = _make_result(formats) |
ba7678f9 | 175 | |
89087418 | 176 | ydl = YDL({'format': 'bestaudio'}) |
ba7678f9 PH |
177 | ydl.process_ie_result(info_dict.copy()) |
178 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 179 | self.assertEqual(downloaded['format_id'], 'audio-high') |
ba7678f9 | 180 | |
89087418 | 181 | ydl = YDL({'format': 'worstaudio'}) |
ba7678f9 PH |
182 | ydl.process_ie_result(info_dict.copy()) |
183 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 184 | self.assertEqual(downloaded['format_id'], 'audio-low') |
ba7678f9 PH |
185 | |
186 | formats = [ | |
3537b93d PH |
187 | {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': '_'}, |
188 | {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': '_'}, | |
ba7678f9 | 189 | ] |
3537b93d | 190 | info_dict = _make_result(formats) |
ba7678f9 | 191 | |
89087418 | 192 | ydl = YDL({'format': 'bestaudio/worstaudio/best'}) |
ba7678f9 PH |
193 | ydl.process_ie_result(info_dict.copy()) |
194 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 195 | self.assertEqual(downloaded['format_id'], 'vid-high') |
ba7678f9 | 196 | |
0217c783 PH |
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 | ||
bc6d5978 JMF |
228 | def test_format_selection_video(self): |
229 | formats = [ | |
3537b93d PH |
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': '_'}, | |
bc6d5978 | 233 | ] |
3537b93d | 234 | info_dict = _make_result(formats) |
bc6d5978 JMF |
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 | ||
3d4a70b8 PH |
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 | |
c11125f9 | 254 | '137', '248', '136', '247', '135', '246', |
3d4a70b8 PH |
255 | '245', '244', '134', '243', '133', '242', '160', |
256 | # Dash audio | |
a053c349 | 257 | '141', '172', '140', '171', '139', |
3d4a70b8 PH |
258 | ] |
259 | ||
260 | for f1id, f2id in zip(order, order[1:]): | |
261 | f1 = YoutubeIE._formats[f1id].copy() | |
262 | f1['format_id'] = f1id | |
3537b93d | 263 | f1['url'] = 'url:' + f1id |
3d4a70b8 PH |
264 | f2 = YoutubeIE._formats[f2id].copy() |
265 | f2['format_id'] = f2id | |
3537b93d | 266 | f2['url'] = 'url:' + f2id |
3d4a70b8 | 267 | |
3537b93d | 268 | info_dict = _make_result([f1, f2], extractor='youtube') |
3d4a70b8 PH |
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 | ||
3537b93d | 276 | info_dict = _make_result([f2, f1], extractor='youtube') |
3d4a70b8 PH |
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 | ||
b6c45014 JMF |
284 | def test_add_extra_info(self): |
285 | test_dict = { | |
286 | 'extractor': 'Foo', | |
287 | } | |
288 | extra_info = { | |
289 | 'extractor': 'Bar', | |
290 | 'playlist': 'funny videos', | |
291 | } | |
292 | YDL.add_extra_info(test_dict, extra_info) | |
293 | self.assertEqual(test_dict['extractor'], 'Foo') | |
294 | self.assertEqual(test_dict['playlist'], 'funny videos') | |
295 | ||
26e63931 JMF |
296 | def test_prepare_filename(self): |
297 | info = { | |
89087418 PH |
298 | 'id': '1234', |
299 | 'ext': 'mp4', | |
300 | 'width': None, | |
26e63931 | 301 | } |
5f6a1245 | 302 | |
26e63931 JMF |
303 | def fname(templ): |
304 | ydl = YoutubeDL({'outtmpl': templ}) | |
305 | return ydl.prepare_filename(info) | |
89087418 PH |
306 | self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4') |
307 | self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4') | |
26e63931 | 308 | # Replace missing fields with 'NA' |
89087418 | 309 | self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4') |
26e63931 | 310 | |
c57f7757 PH |
311 | def test_format_note(self): |
312 | ydl = YoutubeDL() | |
313 | self.assertEqual(ydl._format_note({}), '') | |
314 | assertRegexpMatches(self, ydl._format_note({ | |
315 | 'vbr': 10, | |
1c783bca | 316 | }), '^\s*10k$') |
f4d96df0 | 317 | |
e028d0d1 JMF |
318 | if __name__ == '__main__': |
319 | unittest.main() |