]> jfr.im git - yt-dlp.git/blame - test/test_YoutubeDL.py
release 2014.04.04.1
[yt-dlp.git] / test / test_YoutubeDL.py
CommitLineData
e028d0d1
JMF
1#!/usr/bin/env python
2
89087418
PH
3from __future__ import unicode_literals
4
5d254f77
PH
5# Allow direct execution
6import os
e028d0d1
JMF
7import sys
8import unittest
5d254f77 9sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
e028d0d1 10
5d254f77 11from test.helper import FakeYDL
26e63931 12from youtube_dl import YoutubeDL
3d4a70b8 13from youtube_dl.extractor import YoutubeIE
e028d0d1 14
e028d0d1
JMF
15
16class 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
e028d0d1
JMF
29class TestFormatSelection(unittest.TestCase):
30 def test_prefer_free_formats(self):
31 # Same resolution => download webm
32 ydl = YDL()
33 ydl.params['prefer_free_formats'] = True
5d254f77 34 formats = [
89087418
PH
35 {'ext': 'webm', 'height': 460},
36 {'ext': 'mp4', 'height': 460},
5d254f77 37 ]
89087418 38 info_dict = {'formats': formats, 'extractor': 'test'}
3d4a70b8
PH
39 yie = YoutubeIE(ydl)
40 yie._sort_formats(info_dict['formats'])
e028d0d1
JMF
41 ydl.process_ie_result(info_dict)
42 downloaded = ydl.downloaded_info_dicts[0]
89087418 43 self.assertEqual(downloaded['ext'], 'webm')
e028d0d1
JMF
44
45 # Different resolution => download best quality (mp4)
46 ydl = YDL()
47 ydl.params['prefer_free_formats'] = True
5d254f77 48 formats = [
89087418
PH
49 {'ext': 'webm', 'height': 720},
50 {'ext': 'mp4', 'height': 1080},
5d254f77 51 ]
89087418 52 info_dict['formats'] = formats
3d4a70b8
PH
53 yie = YoutubeIE(ydl)
54 yie._sort_formats(info_dict['formats'])
e028d0d1
JMF
55 ydl.process_ie_result(info_dict)
56 downloaded = ydl.downloaded_info_dicts[0]
89087418 57 self.assertEqual(downloaded['ext'], 'mp4')
e028d0d1 58
3d4a70b8 59 # No prefer_free_formats => prefer mp4 and flv for greater compatibilty
e028d0d1
JMF
60 ydl = YDL()
61 ydl.params['prefer_free_formats'] = False
5d254f77 62 formats = [
89087418
PH
63 {'ext': 'webm', 'height': 720},
64 {'ext': 'mp4', 'height': 720},
65 {'ext': 'flv', 'height': 720},
5d254f77 66 ]
89087418 67 info_dict['formats'] = formats
3d4a70b8
PH
68 yie = YoutubeIE(ydl)
69 yie._sort_formats(info_dict['formats'])
70 ydl.process_ie_result(info_dict)
71 downloaded = ydl.downloaded_info_dicts[0]
89087418 72 self.assertEqual(downloaded['ext'], 'mp4')
3d4a70b8
PH
73
74 ydl = YDL()
75 ydl.params['prefer_free_formats'] = False
76 formats = [
89087418
PH
77 {'ext': 'flv', 'height': 720},
78 {'ext': 'webm', 'height': 720},
3d4a70b8 79 ]
89087418 80 info_dict['formats'] = formats
3d4a70b8
PH
81 yie = YoutubeIE(ydl)
82 yie._sort_formats(info_dict['formats'])
e028d0d1
JMF
83 ydl.process_ie_result(info_dict)
84 downloaded = ydl.downloaded_info_dicts[0]
89087418 85 self.assertEqual(downloaded['ext'], 'flv')
e028d0d1 86
f4d96df0
PH
87 def test_format_limit(self):
88 formats = [
89087418
PH
89 {'format_id': 'meh', 'url': 'http://example.com/meh', 'preference': 1},
90 {'format_id': 'good', 'url': 'http://example.com/good', 'preference': 2},
91 {'format_id': 'great', 'url': 'http://example.com/great', 'preference': 3},
92 {'format_id': 'excellent', 'url': 'http://example.com/exc', 'preference': 4},
f4d96df0
PH
93 ]
94 info_dict = {
89087418 95 'formats': formats, 'extractor': 'test', 'id': 'testvid'}
f4d96df0
PH
96
97 ydl = YDL()
98 ydl.process_ie_result(info_dict)
99 downloaded = ydl.downloaded_info_dicts[0]
89087418 100 self.assertEqual(downloaded['format_id'], 'excellent')
f4d96df0
PH
101
102 ydl = YDL({'format_limit': 'good'})
103 assert ydl.params['format_limit'] == 'good'
8e3e0322 104 ydl.process_ie_result(info_dict.copy())
f4d96df0 105 downloaded = ydl.downloaded_info_dicts[0]
89087418 106 self.assertEqual(downloaded['format_id'], 'good')
f4d96df0
PH
107
108 ydl = YDL({'format_limit': 'great', 'format': 'all'})
8e3e0322 109 ydl.process_ie_result(info_dict.copy())
89087418
PH
110 self.assertEqual(ydl.downloaded_info_dicts[0]['format_id'], 'meh')
111 self.assertEqual(ydl.downloaded_info_dicts[1]['format_id'], 'good')
112 self.assertEqual(ydl.downloaded_info_dicts[2]['format_id'], 'great')
f4d96df0
PH
113 self.assertTrue('3' in ydl.msgs[0])
114
115 ydl = YDL()
116 ydl.params['format_limit'] = 'excellent'
8e3e0322 117 ydl.process_ie_result(info_dict.copy())
f4d96df0 118 downloaded = ydl.downloaded_info_dicts[0]
89087418 119 self.assertEqual(downloaded['format_id'], 'excellent')
f4d96df0 120
a9c58ad9
JMF
121 def test_format_selection(self):
122 formats = [
89087418
PH
123 {'format_id': '35', 'ext': 'mp4', 'preference': 1},
124 {'format_id': '45', 'ext': 'webm', 'preference': 2},
125 {'format_id': '47', 'ext': 'webm', 'preference': 3},
126 {'format_id': '2', 'ext': 'flv', 'preference': 4},
a9c58ad9 127 ]
89087418 128 info_dict = {'formats': formats, 'extractor': 'test'}
a9c58ad9 129
89087418 130 ydl = YDL({'format': '20/47'})
8e3e0322 131 ydl.process_ie_result(info_dict.copy())
a9c58ad9 132 downloaded = ydl.downloaded_info_dicts[0]
89087418 133 self.assertEqual(downloaded['format_id'], '47')
a9c58ad9 134
89087418 135 ydl = YDL({'format': '20/71/worst'})
8e3e0322 136 ydl.process_ie_result(info_dict.copy())
a9c58ad9 137 downloaded = ydl.downloaded_info_dicts[0]
89087418 138 self.assertEqual(downloaded['format_id'], '35')
a9c58ad9
JMF
139
140 ydl = YDL()
8e3e0322 141 ydl.process_ie_result(info_dict.copy())
a9c58ad9 142 downloaded = ydl.downloaded_info_dicts[0]
89087418 143 self.assertEqual(downloaded['format_id'], '2')
a9c58ad9 144
89087418 145 ydl = YDL({'format': 'webm/mp4'})
8e3e0322 146 ydl.process_ie_result(info_dict.copy())
49e86983 147 downloaded = ydl.downloaded_info_dicts[0]
89087418 148 self.assertEqual(downloaded['format_id'], '47')
49e86983 149
89087418 150 ydl = YDL({'format': '3gp/40/mp4'})
8e3e0322 151 ydl.process_ie_result(info_dict.copy())
49e86983 152 downloaded = ydl.downloaded_info_dicts[0]
89087418 153 self.assertEqual(downloaded['format_id'], '35')
49e86983 154
ba7678f9
PH
155 def test_format_selection_audio(self):
156 formats = [
89087418
PH
157 {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none'},
158 {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none'},
159 {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none'},
160 {'format_id': 'vid', 'ext': 'mp4', 'preference': 4},
ba7678f9 161 ]
89087418 162 info_dict = {'formats': formats, 'extractor': 'test'}
ba7678f9 163
89087418 164 ydl = YDL({'format': 'bestaudio'})
ba7678f9
PH
165 ydl.process_ie_result(info_dict.copy())
166 downloaded = ydl.downloaded_info_dicts[0]
89087418 167 self.assertEqual(downloaded['format_id'], 'audio-high')
ba7678f9 168
89087418 169 ydl = YDL({'format': 'worstaudio'})
ba7678f9
PH
170 ydl.process_ie_result(info_dict.copy())
171 downloaded = ydl.downloaded_info_dicts[0]
89087418 172 self.assertEqual(downloaded['format_id'], 'audio-low')
ba7678f9
PH
173
174 formats = [
89087418
PH
175 {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1},
176 {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2},
ba7678f9 177 ]
89087418 178 info_dict = {'formats': formats, 'extractor': 'test'}
ba7678f9 179
89087418 180 ydl = YDL({'format': 'bestaudio/worstaudio/best'})
ba7678f9
PH
181 ydl.process_ie_result(info_dict.copy())
182 downloaded = ydl.downloaded_info_dicts[0]
89087418 183 self.assertEqual(downloaded['format_id'], 'vid-high')
ba7678f9 184
bc6d5978
JMF
185 def test_format_selection_video(self):
186 formats = [
187 {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none'},
188 {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none'},
189 {'format_id': 'vid', 'ext': 'mp4', 'preference': 3},
190 ]
191 info_dict = {'formats': formats, 'extractor': 'test'}
192
193 ydl = YDL({'format': 'bestvideo'})
194 ydl.process_ie_result(info_dict.copy())
195 downloaded = ydl.downloaded_info_dicts[0]
196 self.assertEqual(downloaded['format_id'], 'dash-video-high')
197
198 ydl = YDL({'format': 'worstvideo'})
199 ydl.process_ie_result(info_dict.copy())
200 downloaded = ydl.downloaded_info_dicts[0]
201 self.assertEqual(downloaded['format_id'], 'dash-video-low')
202
3d4a70b8
PH
203 def test_youtube_format_selection(self):
204 order = [
205 '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '36', '17', '13',
206 # Apple HTTP Live Streaming
207 '96', '95', '94', '93', '92', '132', '151',
208 # 3D
209 '85', '84', '102', '83', '101', '82', '100',
210 # Dash video
211 '138', '137', '248', '136', '247', '135', '246',
212 '245', '244', '134', '243', '133', '242', '160',
213 # Dash audio
214 '141', '172', '140', '139', '171',
215 ]
216
217 for f1id, f2id in zip(order, order[1:]):
218 f1 = YoutubeIE._formats[f1id].copy()
219 f1['format_id'] = f1id
220 f2 = YoutubeIE._formats[f2id].copy()
221 f2['format_id'] = f2id
222
223 info_dict = {'formats': [f1, f2], 'extractor': 'youtube'}
224 ydl = YDL()
225 yie = YoutubeIE(ydl)
226 yie._sort_formats(info_dict['formats'])
227 ydl.process_ie_result(info_dict)
228 downloaded = ydl.downloaded_info_dicts[0]
229 self.assertEqual(downloaded['format_id'], f1id)
230
231 info_dict = {'formats': [f2, f1], 'extractor': 'youtube'}
232 ydl = YDL()
233 yie = YoutubeIE(ydl)
234 yie._sort_formats(info_dict['formats'])
235 ydl.process_ie_result(info_dict)
236 downloaded = ydl.downloaded_info_dicts[0]
237 self.assertEqual(downloaded['format_id'], f1id)
238
b6c45014
JMF
239 def test_add_extra_info(self):
240 test_dict = {
241 'extractor': 'Foo',
242 }
243 extra_info = {
244 'extractor': 'Bar',
245 'playlist': 'funny videos',
246 }
247 YDL.add_extra_info(test_dict, extra_info)
248 self.assertEqual(test_dict['extractor'], 'Foo')
249 self.assertEqual(test_dict['playlist'], 'funny videos')
250
26e63931
JMF
251 def test_prepare_filename(self):
252 info = {
89087418
PH
253 'id': '1234',
254 'ext': 'mp4',
255 'width': None,
26e63931
JMF
256 }
257 def fname(templ):
258 ydl = YoutubeDL({'outtmpl': templ})
259 return ydl.prepare_filename(info)
89087418
PH
260 self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4')
261 self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4')
26e63931 262 # Replace missing fields with 'NA'
89087418 263 self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4')
26e63931 264
f4d96df0 265
e028d0d1
JMF
266if __name__ == '__main__':
267 unittest.main()