3 # Allow direct execution
7 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9 from test.helper import FakeYDL
10 from youtube_dl import YoutubeDL
14 def __init__(self, *args, **kwargs):
15 super(YDL, self).__init__(*args, **kwargs)
16 self.downloaded_info_dicts = []
19 def process_info(self, info_dict):
20 self.downloaded_info_dicts.append(info_dict)
22 def to_screen(self, msg):
26 class TestFormatSelection(unittest.TestCase):
27 def test_prefer_free_formats(self):
28 # Same resolution => download webm
30 ydl.params['prefer_free_formats'] = True
32 {u'ext': u'webm', u'height': 460},
33 {u'ext': u'mp4', u'height': 460},
35 info_dict = {u'formats': formats, u'extractor': u'test'}
36 ydl.process_ie_result(info_dict)
37 downloaded = ydl.downloaded_info_dicts[0]
38 self.assertEqual(downloaded[u'ext'], u'webm')
40 # Different resolution => download best quality (mp4)
42 ydl.params['prefer_free_formats'] = True
44 {u'ext': u'webm', u'height': 720},
45 {u'ext': u'mp4', u'height': 1080},
47 info_dict[u'formats'] = formats
48 ydl.process_ie_result(info_dict)
49 downloaded = ydl.downloaded_info_dicts[0]
50 self.assertEqual(downloaded[u'ext'], u'mp4')
52 # No prefer_free_formats => keep original formats order
54 ydl.params['prefer_free_formats'] = False
56 {u'ext': u'webm', u'height': 720},
57 {u'ext': u'flv', u'height': 720},
59 info_dict[u'formats'] = formats
60 ydl.process_ie_result(info_dict)
61 downloaded = ydl.downloaded_info_dicts[0]
62 self.assertEqual(downloaded[u'ext'], u'flv')
64 def test_format_limit(self):
66 {u'format_id': u'meh', u'url': u'http://example.com/meh'},
67 {u'format_id': u'good', u'url': u'http://example.com/good'},
68 {u'format_id': u'great', u'url': u'http://example.com/great'},
69 {u'format_id': u'excellent', u'url': u'http://example.com/exc'},
72 u'formats': formats, u'extractor': u'test', 'id': 'testvid'}
75 ydl.process_ie_result(info_dict)
76 downloaded = ydl.downloaded_info_dicts[0]
77 self.assertEqual(downloaded[u'format_id'], u'excellent')
79 ydl = YDL({'format_limit': 'good'})
80 assert ydl.params['format_limit'] == 'good'
81 ydl.process_ie_result(info_dict)
82 downloaded = ydl.downloaded_info_dicts[0]
83 self.assertEqual(downloaded[u'format_id'], u'good')
85 ydl = YDL({'format_limit': 'great', 'format': 'all'})
86 ydl.process_ie_result(info_dict)
87 self.assertEqual(ydl.downloaded_info_dicts[0][u'format_id'], u'meh')
88 self.assertEqual(ydl.downloaded_info_dicts[1][u'format_id'], u'good')
89 self.assertEqual(ydl.downloaded_info_dicts[2][u'format_id'], u'great')
90 self.assertTrue('3' in ydl.msgs[0])
93 ydl.params['format_limit'] = 'excellent'
94 ydl.process_ie_result(info_dict)
95 downloaded = ydl.downloaded_info_dicts[0]
96 self.assertEqual(downloaded[u'format_id'], u'excellent')
98 def test_format_selection(self):
100 {u'format_id': u'35', u'ext': u'mp4'},
101 {u'format_id': u'45', u'ext': u'webm'},
102 {u'format_id': u'47', u'ext': u'webm'},
103 {u'format_id': u'2', u'ext': u'flv'},
105 info_dict = {u'formats': formats, u'extractor': u'test'}
107 ydl = YDL({'format': u'20/47'})
108 ydl.process_ie_result(info_dict)
109 downloaded = ydl.downloaded_info_dicts[0]
110 self.assertEqual(downloaded['format_id'], u'47')
112 ydl = YDL({'format': u'20/71/worst'})
113 ydl.process_ie_result(info_dict)
114 downloaded = ydl.downloaded_info_dicts[0]
115 self.assertEqual(downloaded['format_id'], u'35')
118 ydl.process_ie_result(info_dict)
119 downloaded = ydl.downloaded_info_dicts[0]
120 self.assertEqual(downloaded['format_id'], u'2')
122 ydl = YDL({'format': u'webm/mp4'})
123 ydl.process_ie_result(info_dict)
124 downloaded = ydl.downloaded_info_dicts[0]
125 self.assertEqual(downloaded['format_id'], u'47')
127 ydl = YDL({'format': u'3gp/40/mp4'})
128 ydl.process_ie_result(info_dict)
129 downloaded = ydl.downloaded_info_dicts[0]
130 self.assertEqual(downloaded['format_id'], u'35')
132 def test_add_extra_info(self):
138 'playlist': 'funny videos',
140 YDL.add_extra_info(test_dict, extra_info)
141 self.assertEqual(test_dict['extractor'], 'Foo')
142 self.assertEqual(test_dict['playlist'], 'funny videos')
144 def test_prepare_filename(self):
151 ydl = YoutubeDL({'outtmpl': templ})
152 return ydl.prepare_filename(info)
153 self.assertEqual(fname(u'%(id)s.%(ext)s'), u'1234.mp4')
154 self.assertEqual(fname(u'%(id)s-%(width)s.%(ext)s'), u'1234-NA.mp4')
155 # Replace missing fields with 'NA'
156 self.assertEqual(fname(u'%(uploader_date)s-%(id)s.%(ext)s'), u'NA-1234.mp4')
159 if __name__ == '__main__':