]>
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 | |
c57f7757 | 11 | from test.helper import FakeYDL, assertRegexpMatches |
26e63931 | 12 | from youtube_dl import YoutubeDL |
3d4a70b8 | 13 | from youtube_dl.extractor import YoutubeIE |
e028d0d1 | 14 | |
e028d0d1 JMF |
15 | |
16 | class YDL(FakeYDL): | |
f4d96df0 PH |
17 | def __init__(self, *args, **kwargs): |
18 | super(YDL, self).__init__(*args, **kwargs) | |
e028d0d1 | 19 | self.downloaded_info_dicts = [] |
f4d96df0 | 20 | self.msgs = [] |
5d254f77 | 21 | |
e028d0d1 JMF |
22 | def process_info(self, info_dict): |
23 | self.downloaded_info_dicts.append(info_dict) | |
24 | ||
f4d96df0 PH |
25 | def to_screen(self, msg): |
26 | self.msgs.append(msg) | |
27 | ||
5d254f77 | 28 | |
3537b93d PH |
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 | ||
e028d0d1 JMF |
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 | |
5d254f77 | 45 | formats = [ |
3537b93d PH |
46 | {'ext': 'webm', 'height': 460, 'url': 'x'}, |
47 | {'ext': 'mp4', 'height': 460, 'url': 'y'}, | |
5d254f77 | 48 | ] |
3537b93d | 49 | info_dict = _make_result(formats) |
3d4a70b8 PH |
50 | yie = YoutubeIE(ydl) |
51 | yie._sort_formats(info_dict['formats']) | |
e028d0d1 JMF |
52 | ydl.process_ie_result(info_dict) |
53 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 54 | self.assertEqual(downloaded['ext'], 'webm') |
e028d0d1 JMF |
55 | |
56 | # Different resolution => download best quality (mp4) | |
57 | ydl = YDL() | |
58 | ydl.params['prefer_free_formats'] = True | |
5d254f77 | 59 | formats = [ |
3537b93d PH |
60 | {'ext': 'webm', 'height': 720, 'url': 'a'}, |
61 | {'ext': 'mp4', 'height': 1080, 'url': 'b'}, | |
5d254f77 | 62 | ] |
89087418 | 63 | info_dict['formats'] = formats |
3d4a70b8 PH |
64 | yie = YoutubeIE(ydl) |
65 | yie._sort_formats(info_dict['formats']) | |
e028d0d1 JMF |
66 | ydl.process_ie_result(info_dict) |
67 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 68 | self.assertEqual(downloaded['ext'], 'mp4') |
e028d0d1 | 69 | |
1c783bca | 70 | # No prefer_free_formats => prefer mp4 and flv for greater compatibility |
e028d0d1 JMF |
71 | ydl = YDL() |
72 | ydl.params['prefer_free_formats'] = False | |
5d254f77 | 73 | formats = [ |
3537b93d PH |
74 | {'ext': 'webm', 'height': 720, 'url': '_'}, |
75 | {'ext': 'mp4', 'height': 720, 'url': '_'}, | |
76 | {'ext': 'flv', 'height': 720, 'url': '_'}, | |
5d254f77 | 77 | ] |
89087418 | 78 | info_dict['formats'] = formats |
3d4a70b8 PH |
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] | |
89087418 | 83 | self.assertEqual(downloaded['ext'], 'mp4') |
3d4a70b8 PH |
84 | |
85 | ydl = YDL() | |
86 | ydl.params['prefer_free_formats'] = False | |
87 | formats = [ | |
3537b93d PH |
88 | {'ext': 'flv', 'height': 720, 'url': '_'}, |
89 | {'ext': 'webm', 'height': 720, 'url': '_'}, | |
3d4a70b8 | 90 | ] |
89087418 | 91 | info_dict['formats'] = formats |
3d4a70b8 PH |
92 | yie = YoutubeIE(ydl) |
93 | yie._sort_formats(info_dict['formats']) | |
e028d0d1 JMF |
94 | ydl.process_ie_result(info_dict) |
95 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 96 | self.assertEqual(downloaded['ext'], 'flv') |
e028d0d1 | 97 | |
f4d96df0 PH |
98 | def test_format_limit(self): |
99 | formats = [ | |
89087418 PH |
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}, | |
f4d96df0 | 104 | ] |
3537b93d | 105 | info_dict = _make_result(formats) |
f4d96df0 PH |
106 | |
107 | ydl = YDL() | |
108 | ydl.process_ie_result(info_dict) | |
109 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 110 | self.assertEqual(downloaded['format_id'], 'excellent') |
f4d96df0 PH |
111 | |
112 | ydl = YDL({'format_limit': 'good'}) | |
113 | assert ydl.params['format_limit'] == 'good' | |
8e3e0322 | 114 | ydl.process_ie_result(info_dict.copy()) |
f4d96df0 | 115 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 116 | self.assertEqual(downloaded['format_id'], 'good') |
f4d96df0 PH |
117 | |
118 | ydl = YDL({'format_limit': 'great', 'format': 'all'}) | |
8e3e0322 | 119 | ydl.process_ie_result(info_dict.copy()) |
89087418 PH |
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') | |
f4d96df0 PH |
123 | self.assertTrue('3' in ydl.msgs[0]) |
124 | ||
125 | ydl = YDL() | |
126 | ydl.params['format_limit'] = 'excellent' | |
8e3e0322 | 127 | ydl.process_ie_result(info_dict.copy()) |
f4d96df0 | 128 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 129 | self.assertEqual(downloaded['format_id'], 'excellent') |
f4d96df0 | 130 | |
a9c58ad9 JMF |
131 | def test_format_selection(self): |
132 | formats = [ | |
3537b93d PH |
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': '_'}, | |
a9c58ad9 | 137 | ] |
3537b93d | 138 | info_dict = _make_result(formats) |
a9c58ad9 | 139 | |
89087418 | 140 | ydl = YDL({'format': '20/47'}) |
8e3e0322 | 141 | ydl.process_ie_result(info_dict.copy()) |
a9c58ad9 | 142 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 143 | self.assertEqual(downloaded['format_id'], '47') |
a9c58ad9 | 144 | |
89087418 | 145 | ydl = YDL({'format': '20/71/worst'}) |
8e3e0322 | 146 | ydl.process_ie_result(info_dict.copy()) |
a9c58ad9 | 147 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 148 | self.assertEqual(downloaded['format_id'], '35') |
a9c58ad9 JMF |
149 | |
150 | ydl = YDL() | |
8e3e0322 | 151 | ydl.process_ie_result(info_dict.copy()) |
a9c58ad9 | 152 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 153 | self.assertEqual(downloaded['format_id'], '2') |
a9c58ad9 | 154 | |
89087418 | 155 | ydl = YDL({'format': 'webm/mp4'}) |
8e3e0322 | 156 | ydl.process_ie_result(info_dict.copy()) |
49e86983 | 157 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 158 | self.assertEqual(downloaded['format_id'], '47') |
49e86983 | 159 | |
89087418 | 160 | ydl = YDL({'format': '3gp/40/mp4'}) |
8e3e0322 | 161 | ydl.process_ie_result(info_dict.copy()) |
49e86983 | 162 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 163 | self.assertEqual(downloaded['format_id'], '35') |
49e86983 | 164 | |
ba7678f9 PH |
165 | def test_format_selection_audio(self): |
166 | formats = [ | |
3537b93d PH |
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': '_'}, | |
ba7678f9 | 171 | ] |
3537b93d | 172 | info_dict = _make_result(formats) |
ba7678f9 | 173 | |
89087418 | 174 | ydl = YDL({'format': 'bestaudio'}) |
ba7678f9 PH |
175 | ydl.process_ie_result(info_dict.copy()) |
176 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 177 | self.assertEqual(downloaded['format_id'], 'audio-high') |
ba7678f9 | 178 | |
89087418 | 179 | ydl = YDL({'format': 'worstaudio'}) |
ba7678f9 PH |
180 | ydl.process_ie_result(info_dict.copy()) |
181 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 182 | self.assertEqual(downloaded['format_id'], 'audio-low') |
ba7678f9 PH |
183 | |
184 | formats = [ | |
3537b93d PH |
185 | {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': '_'}, |
186 | {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': '_'}, | |
ba7678f9 | 187 | ] |
3537b93d | 188 | info_dict = _make_result(formats) |
ba7678f9 | 189 | |
89087418 | 190 | ydl = YDL({'format': 'bestaudio/worstaudio/best'}) |
ba7678f9 PH |
191 | ydl.process_ie_result(info_dict.copy()) |
192 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 193 | self.assertEqual(downloaded['format_id'], 'vid-high') |
ba7678f9 | 194 | |
bc6d5978 JMF |
195 | def test_format_selection_video(self): |
196 | formats = [ | |
3537b93d PH |
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': '_'}, | |
bc6d5978 | 200 | ] |
3537b93d | 201 | info_dict = _make_result(formats) |
bc6d5978 JMF |
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 | ||
3d4a70b8 PH |
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', '139', '171', | |
225 | ] | |
226 | ||
227 | for f1id, f2id in zip(order, order[1:]): | |
228 | f1 = YoutubeIE._formats[f1id].copy() | |
229 | f1['format_id'] = f1id | |
3537b93d | 230 | f1['url'] = 'url:' + f1id |
3d4a70b8 PH |
231 | f2 = YoutubeIE._formats[f2id].copy() |
232 | f2['format_id'] = f2id | |
3537b93d | 233 | f2['url'] = 'url:' + f2id |
3d4a70b8 | 234 | |
3537b93d | 235 | info_dict = _make_result([f1, f2], extractor='youtube') |
3d4a70b8 PH |
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 | ||
3537b93d | 243 | info_dict = _make_result([f2, f1], extractor='youtube') |
3d4a70b8 PH |
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 | ||
b6c45014 JMF |
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 | ||
26e63931 JMF |
263 | def test_prepare_filename(self): |
264 | info = { | |
89087418 PH |
265 | 'id': '1234', |
266 | 'ext': 'mp4', | |
267 | 'width': None, | |
26e63931 JMF |
268 | } |
269 | def fname(templ): | |
270 | ydl = YoutubeDL({'outtmpl': templ}) | |
271 | return ydl.prepare_filename(info) | |
89087418 PH |
272 | self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4') |
273 | self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4') | |
26e63931 | 274 | # Replace missing fields with 'NA' |
89087418 | 275 | self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4') |
26e63931 | 276 | |
c57f7757 PH |
277 | def test_format_note(self): |
278 | ydl = YoutubeDL() | |
279 | self.assertEqual(ydl._format_note({}), '') | |
280 | assertRegexpMatches(self, ydl._format_note({ | |
281 | 'vbr': 10, | |
1c783bca | 282 | }), '^\s*10k$') |
f4d96df0 | 283 | |
e028d0d1 JMF |
284 | if __name__ == '__main__': |
285 | unittest.main() |