]> Raphaƫl G. Git Repositories - youtubedl/blob - test/test_YoutubeDL.py
Imported Upstream version 2014.01.17.2
[youtubedl] / test / test_YoutubeDL.py
1 #!/usr/bin/env python
2
3 # Allow direct execution
4 import os
5 import sys
6 import unittest
7 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8
9 from test.helper import FakeYDL
10 from youtube_dl import YoutubeDL
11 from youtube_dl.extractor import YoutubeIE
12
13
14 class YDL(FakeYDL):
15 def __init__(self, *args, **kwargs):
16 super(YDL, self).__init__(*args, **kwargs)
17 self.downloaded_info_dicts = []
18 self.msgs = []
19
20 def process_info(self, info_dict):
21 self.downloaded_info_dicts.append(info_dict)
22
23 def to_screen(self, msg):
24 self.msgs.append(msg)
25
26
27 class TestFormatSelection(unittest.TestCase):
28 def test_prefer_free_formats(self):
29 # Same resolution => download webm
30 ydl = YDL()
31 ydl.params['prefer_free_formats'] = True
32 formats = [
33 {u'ext': u'webm', u'height': 460},
34 {u'ext': u'mp4', u'height': 460},
35 ]
36 info_dict = {u'formats': formats, u'extractor': u'test'}
37 yie = YoutubeIE(ydl)
38 yie._sort_formats(info_dict['formats'])
39 ydl.process_ie_result(info_dict)
40 downloaded = ydl.downloaded_info_dicts[0]
41 self.assertEqual(downloaded[u'ext'], u'webm')
42
43 # Different resolution => download best quality (mp4)
44 ydl = YDL()
45 ydl.params['prefer_free_formats'] = True
46 formats = [
47 {u'ext': u'webm', u'height': 720},
48 {u'ext': u'mp4', u'height': 1080},
49 ]
50 info_dict[u'formats'] = formats
51 yie = YoutubeIE(ydl)
52 yie._sort_formats(info_dict['formats'])
53 ydl.process_ie_result(info_dict)
54 downloaded = ydl.downloaded_info_dicts[0]
55 self.assertEqual(downloaded[u'ext'], u'mp4')
56
57 # No prefer_free_formats => prefer mp4 and flv for greater compatibilty
58 ydl = YDL()
59 ydl.params['prefer_free_formats'] = False
60 formats = [
61 {u'ext': u'webm', u'height': 720},
62 {u'ext': u'mp4', u'height': 720},
63 {u'ext': u'flv', u'height': 720},
64 ]
65 info_dict[u'formats'] = formats
66 yie = YoutubeIE(ydl)
67 yie._sort_formats(info_dict['formats'])
68 ydl.process_ie_result(info_dict)
69 downloaded = ydl.downloaded_info_dicts[0]
70 self.assertEqual(downloaded[u'ext'], u'mp4')
71
72 ydl = YDL()
73 ydl.params['prefer_free_formats'] = False
74 formats = [
75 {u'ext': u'flv', u'height': 720},
76 {u'ext': u'webm', u'height': 720},
77 ]
78 info_dict[u'formats'] = formats
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]
83 self.assertEqual(downloaded[u'ext'], u'flv')
84
85 def test_format_limit(self):
86 formats = [
87 {u'format_id': u'meh', u'url': u'http://example.com/meh', 'preference': 1},
88 {u'format_id': u'good', u'url': u'http://example.com/good', 'preference': 2},
89 {u'format_id': u'great', u'url': u'http://example.com/great', 'preference': 3},
90 {u'format_id': u'excellent', u'url': u'http://example.com/exc', 'preference': 4},
91 ]
92 info_dict = {
93 u'formats': formats, u'extractor': u'test', 'id': 'testvid'}
94
95 ydl = YDL()
96 ydl.process_ie_result(info_dict)
97 downloaded = ydl.downloaded_info_dicts[0]
98 self.assertEqual(downloaded[u'format_id'], u'excellent')
99
100 ydl = YDL({'format_limit': 'good'})
101 assert ydl.params['format_limit'] == 'good'
102 ydl.process_ie_result(info_dict.copy())
103 downloaded = ydl.downloaded_info_dicts[0]
104 self.assertEqual(downloaded[u'format_id'], u'good')
105
106 ydl = YDL({'format_limit': 'great', 'format': 'all'})
107 ydl.process_ie_result(info_dict.copy())
108 self.assertEqual(ydl.downloaded_info_dicts[0][u'format_id'], u'meh')
109 self.assertEqual(ydl.downloaded_info_dicts[1][u'format_id'], u'good')
110 self.assertEqual(ydl.downloaded_info_dicts[2][u'format_id'], u'great')
111 self.assertTrue('3' in ydl.msgs[0])
112
113 ydl = YDL()
114 ydl.params['format_limit'] = 'excellent'
115 ydl.process_ie_result(info_dict.copy())
116 downloaded = ydl.downloaded_info_dicts[0]
117 self.assertEqual(downloaded[u'format_id'], u'excellent')
118
119 def test_format_selection(self):
120 formats = [
121 {u'format_id': u'35', u'ext': u'mp4', 'preference': 1},
122 {u'format_id': u'45', u'ext': u'webm', 'preference': 2},
123 {u'format_id': u'47', u'ext': u'webm', 'preference': 3},
124 {u'format_id': u'2', u'ext': u'flv', 'preference': 4},
125 ]
126 info_dict = {u'formats': formats, u'extractor': u'test'}
127
128 ydl = YDL({'format': u'20/47'})
129 ydl.process_ie_result(info_dict.copy())
130 downloaded = ydl.downloaded_info_dicts[0]
131 self.assertEqual(downloaded['format_id'], u'47')
132
133 ydl = YDL({'format': u'20/71/worst'})
134 ydl.process_ie_result(info_dict.copy())
135 downloaded = ydl.downloaded_info_dicts[0]
136 self.assertEqual(downloaded['format_id'], u'35')
137
138 ydl = YDL()
139 ydl.process_ie_result(info_dict.copy())
140 downloaded = ydl.downloaded_info_dicts[0]
141 self.assertEqual(downloaded['format_id'], u'2')
142
143 ydl = YDL({'format': u'webm/mp4'})
144 ydl.process_ie_result(info_dict.copy())
145 downloaded = ydl.downloaded_info_dicts[0]
146 self.assertEqual(downloaded['format_id'], u'47')
147
148 ydl = YDL({'format': u'3gp/40/mp4'})
149 ydl.process_ie_result(info_dict.copy())
150 downloaded = ydl.downloaded_info_dicts[0]
151 self.assertEqual(downloaded['format_id'], u'35')
152
153 def test_youtube_format_selection(self):
154 order = [
155 '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '36', '17', '13',
156 # Apple HTTP Live Streaming
157 '96', '95', '94', '93', '92', '132', '151',
158 # 3D
159 '85', '84', '102', '83', '101', '82', '100',
160 # Dash video
161 '138', '137', '248', '136', '247', '135', '246',
162 '245', '244', '134', '243', '133', '242', '160',
163 # Dash audio
164 '141', '172', '140', '139', '171',
165 ]
166
167 for f1id, f2id in zip(order, order[1:]):
168 f1 = YoutubeIE._formats[f1id].copy()
169 f1['format_id'] = f1id
170 f2 = YoutubeIE._formats[f2id].copy()
171 f2['format_id'] = f2id
172
173 info_dict = {'formats': [f1, f2], 'extractor': 'youtube'}
174 ydl = YDL()
175 yie = YoutubeIE(ydl)
176 yie._sort_formats(info_dict['formats'])
177 ydl.process_ie_result(info_dict)
178 downloaded = ydl.downloaded_info_dicts[0]
179 self.assertEqual(downloaded['format_id'], f1id)
180
181 info_dict = {'formats': [f2, f1], 'extractor': 'youtube'}
182 ydl = YDL()
183 yie = YoutubeIE(ydl)
184 yie._sort_formats(info_dict['formats'])
185 ydl.process_ie_result(info_dict)
186 downloaded = ydl.downloaded_info_dicts[0]
187 self.assertEqual(downloaded['format_id'], f1id)
188
189 def test_add_extra_info(self):
190 test_dict = {
191 'extractor': 'Foo',
192 }
193 extra_info = {
194 'extractor': 'Bar',
195 'playlist': 'funny videos',
196 }
197 YDL.add_extra_info(test_dict, extra_info)
198 self.assertEqual(test_dict['extractor'], 'Foo')
199 self.assertEqual(test_dict['playlist'], 'funny videos')
200
201 def test_prepare_filename(self):
202 info = {
203 u'id': u'1234',
204 u'ext': u'mp4',
205 u'width': None,
206 }
207 def fname(templ):
208 ydl = YoutubeDL({'outtmpl': templ})
209 return ydl.prepare_filename(info)
210 self.assertEqual(fname(u'%(id)s.%(ext)s'), u'1234.mp4')
211 self.assertEqual(fname(u'%(id)s-%(width)s.%(ext)s'), u'1234-NA.mp4')
212 # Replace missing fields with 'NA'
213 self.assertEqual(fname(u'%(uploader_date)s-%(id)s.%(ext)s'), u'NA-1234.mp4')
214
215
216 if __name__ == '__main__':
217 unittest.main()