3 from __future__ import unicode_literals
5 # Allow direct execution
9 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11 from test.helper import FakeYDL, assertRegexpMatches
12 from youtube_dl import YoutubeDL
13 from youtube_dl.extractor import YoutubeIE
17 def __init__(self, *args, **kwargs):
18 super(YDL, self).__init__(*args, **kwargs)
19 self.downloaded_info_dicts = []
22 def process_info(self, info_dict):
23 self.downloaded_info_dicts.append(info_dict)
25 def to_screen(self, msg):
29 def _make_result(formats, **kwargs):
33 'title': 'testttitle',
34 'extractor': 'testex',
40 class TestFormatSelection(unittest.TestCase):
41 def test_prefer_free_formats(self):
42 # Same resolution => download webm
44 ydl.params['prefer_free_formats'] = True
46 {'ext': 'webm', 'height': 460, 'url': 'x'},
47 {'ext': 'mp4', 'height': 460, 'url': 'y'},
49 info_dict = _make_result(formats)
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')
56 # Different resolution => download best quality (mp4)
58 ydl.params['prefer_free_formats'] = True
60 {'ext': 'webm', 'height': 720, 'url': 'a'},
61 {'ext': 'mp4', 'height': 1080, 'url': 'b'},
63 info_dict['formats'] = formats
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')
70 # No prefer_free_formats => prefer mp4 and flv for greater compatibility
72 ydl.params['prefer_free_formats'] = False
74 {'ext': 'webm', 'height': 720, 'url': '_'},
75 {'ext': 'mp4', 'height': 720, 'url': '_'},
76 {'ext': 'flv', 'height': 720, 'url': '_'},
78 info_dict['formats'] = formats
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')
86 ydl.params['prefer_free_formats'] = False
88 {'ext': 'flv', 'height': 720, 'url': '_'},
89 {'ext': 'webm', 'height': 720, 'url': '_'},
91 info_dict['formats'] = formats
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')
98 def test_format_limit(self):
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},
105 info_dict = _make_result(formats)
108 ydl.process_ie_result(info_dict)
109 downloaded = ydl.downloaded_info_dicts[0]
110 self.assertEqual(downloaded['format_id'], 'excellent')
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')
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])
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')
131 def test_format_selection(self):
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': '_'},
138 info_dict = _make_result(formats)
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')
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')
151 ydl.process_ie_result(info_dict.copy())
152 downloaded = ydl.downloaded_info_dicts[0]
153 self.assertEqual(downloaded['format_id'], '2')
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')
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')
165 def test_format_selection_audio(self):
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': '_'},
172 info_dict = _make_result(formats)
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')
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')
185 {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': '_'},
186 {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': '_'},
188 info_dict = _make_result(formats)
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')
195 def test_format_selection_video(self):
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': '_'},
201 info_dict = _make_result(formats)
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')
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')
213 def test_youtube_format_selection(self):
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',
219 '85', '84', '102', '83', '101', '82', '100',
221 '138', '137', '248', '136', '247', '135', '246',
222 '245', '244', '134', '243', '133', '242', '160',
224 '141', '172', '140', '171', '139',
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
235 info_dict = _make_result([f1, f2], extractor='youtube')
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)
243 info_dict = _make_result([f2, f1], extractor='youtube')
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)
251 def test_add_extra_info(self):
257 'playlist': 'funny videos',
259 YDL.add_extra_info(test_dict, extra_info)
260 self.assertEqual(test_dict['extractor'], 'Foo')
261 self.assertEqual(test_dict['playlist'], 'funny videos')
263 def test_prepare_filename(self):
271 ydl = YoutubeDL({'outtmpl': templ})
272 return ydl.prepare_filename(info)
273 self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4')
274 self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4')
275 # Replace missing fields with 'NA'
276 self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4')
278 def test_format_note(self):
280 self.assertEqual(ydl._format_note({}), '')
281 assertRegexpMatches(self, ydl._format_note({
285 if __name__ == '__main__':