+ self.assertEqual(downloaded['format_id'], 'audio-low')
+
+ formats = [
+ {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1},
+ {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2},
+ ]
+ info_dict = {'formats': formats, 'extractor': 'test'}
+
+ ydl = YDL({'format': 'bestaudio/worstaudio/best'})
+ ydl.process_ie_result(info_dict.copy())
+ downloaded = ydl.downloaded_info_dicts[0]
+ self.assertEqual(downloaded['format_id'], 'vid-high')
+
+ def test_youtube_format_selection(self):
+ order = [
+ '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '36', '17', '13',
+ # Apple HTTP Live Streaming
+ '96', '95', '94', '93', '92', '132', '151',
+ # 3D
+ '85', '84', '102', '83', '101', '82', '100',
+ # Dash video
+ '138', '137', '248', '136', '247', '135', '246',
+ '245', '244', '134', '243', '133', '242', '160',
+ # Dash audio
+ '141', '172', '140', '139', '171',
+ ]
+
+ for f1id, f2id in zip(order, order[1:]):
+ f1 = YoutubeIE._formats[f1id].copy()
+ f1['format_id'] = f1id
+ f2 = YoutubeIE._formats[f2id].copy()
+ f2['format_id'] = f2id
+
+ info_dict = {'formats': [f1, f2], 'extractor': 'youtube'}
+ ydl = YDL()
+ yie = YoutubeIE(ydl)
+ yie._sort_formats(info_dict['formats'])
+ ydl.process_ie_result(info_dict)
+ downloaded = ydl.downloaded_info_dicts[0]
+ self.assertEqual(downloaded['format_id'], f1id)
+
+ info_dict = {'formats': [f2, f1], 'extractor': 'youtube'}
+ ydl = YDL()
+ yie = YoutubeIE(ydl)
+ yie._sort_formats(info_dict['formats'])
+ ydl.process_ie_result(info_dict)
+ downloaded = ydl.downloaded_info_dicts[0]
+ self.assertEqual(downloaded['format_id'], f1id)
+
+ def test_add_extra_info(self):
+ test_dict = {
+ 'extractor': 'Foo',
+ }
+ extra_info = {
+ 'extractor': 'Bar',
+ 'playlist': 'funny videos',
+ }
+ YDL.add_extra_info(test_dict, extra_info)
+ self.assertEqual(test_dict['extractor'], 'Foo')
+ self.assertEqual(test_dict['playlist'], 'funny videos')
+
+ def test_prepare_filename(self):
+ info = {
+ 'id': '1234',
+ 'ext': 'mp4',
+ 'width': None,
+ }
+ def fname(templ):
+ ydl = YoutubeDL({'outtmpl': templ})
+ return ydl.prepare_filename(info)
+ self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4')
+ self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4')
+ # Replace missing fields with 'NA'
+ self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4')