]>
Raphaƫl G. Git Repositories - youtubedl/blob - test/test_YoutubeDL.py
85d87f2c31e803aff668f1d71a6bbdfba33cdcd8
   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__
)))) 
  13 from test
.helper 
import FakeYDL
, assertRegexpMatches
 
  14 from youtube_dl 
import YoutubeDL
 
  15 from youtube_dl
.extractor 
import YoutubeIE
 
  19     def __init__(self
, *args
, **kwargs
): 
  20         super(YDL
, self
).__init
__(*args
, **kwargs
) 
  21         self
.downloaded_info_dicts 
= [] 
  24     def process_info(self
, info_dict
): 
  25         self
.downloaded_info_dicts
.append(info_dict
) 
  27     def to_screen(self
, msg
): 
  31 def _make_result(formats
, **kwargs
): 
  35         'title': 'testttitle', 
  36         'extractor': 'testex', 
  42 class TestFormatSelection(unittest
.TestCase
): 
  43     def test_prefer_free_formats(self
): 
  44         # Same resolution => download webm 
  46         ydl
.params
['prefer_free_formats'] = True 
  48             {'ext': 'webm', 'height': 460, 'url': 'x'}, 
  49             {'ext': 'mp4', 'height': 460, 'url': 'y'}, 
  51         info_dict 
= _make_result(formats
) 
  53         yie
._sort
_formats
(info_dict
['formats']) 
  54         ydl
.process_ie_result(info_dict
) 
  55         downloaded 
= ydl
.downloaded_info_dicts
[0] 
  56         self
.assertEqual(downloaded
['ext'], 'webm') 
  58         # Different resolution => download best quality (mp4) 
  60         ydl
.params
['prefer_free_formats'] = True 
  62             {'ext': 'webm', 'height': 720, 'url': 'a'}, 
  63             {'ext': 'mp4', 'height': 1080, 'url': 'b'}, 
  65         info_dict
['formats'] = formats
 
  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
['ext'], 'mp4') 
  72         # No prefer_free_formats => prefer mp4 and flv for greater compatibility 
  74         ydl
.params
['prefer_free_formats'] = False 
  76             {'ext': 'webm', 'height': 720, 'url': '_'}, 
  77             {'ext': 'mp4', 'height': 720, 'url': '_'}, 
  78             {'ext': 'flv', 'height': 720, 'url': '_'}, 
  80         info_dict
['formats'] = formats
 
  82         yie
._sort
_formats
(info_dict
['formats']) 
  83         ydl
.process_ie_result(info_dict
) 
  84         downloaded 
= ydl
.downloaded_info_dicts
[0] 
  85         self
.assertEqual(downloaded
['ext'], 'mp4') 
  88         ydl
.params
['prefer_free_formats'] = False 
  90             {'ext': 'flv', 'height': 720, 'url': '_'}, 
  91             {'ext': 'webm', 'height': 720, 'url': '_'}, 
  93         info_dict
['formats'] = formats
 
  95         yie
._sort
_formats
(info_dict
['formats']) 
  96         ydl
.process_ie_result(info_dict
) 
  97         downloaded 
= ydl
.downloaded_info_dicts
[0] 
  98         self
.assertEqual(downloaded
['ext'], 'flv') 
 100     def test_format_limit(self
): 
 102             {'format_id': 'meh', 'url': 'http://example.com/meh', 'preference': 1}, 
 103             {'format_id': 'good', 'url': 'http://example.com/good', 'preference': 2}, 
 104             {'format_id': 'great', 'url': 'http://example.com/great', 'preference': 3}, 
 105             {'format_id': 'excellent', 'url': 'http://example.com/exc', 'preference': 4}, 
 107         info_dict 
= _make_result(formats
) 
 110         ydl
.process_ie_result(info_dict
) 
 111         downloaded 
= ydl
.downloaded_info_dicts
[0] 
 112         self
.assertEqual(downloaded
['format_id'], 'excellent') 
 114         ydl 
= YDL({'format_limit': 'good'}) 
 115         assert ydl
.params
['format_limit'] == 'good' 
 116         ydl
.process_ie_result(info_dict
.copy()) 
 117         downloaded 
= ydl
.downloaded_info_dicts
[0] 
 118         self
.assertEqual(downloaded
['format_id'], 'good') 
 120         ydl 
= YDL({'format_limit': 'great', 'format': 'all'}) 
 121         ydl
.process_ie_result(info_dict
.copy()) 
 122         self
.assertEqual(ydl
.downloaded_info_dicts
[0]['format_id'], 'meh') 
 123         self
.assertEqual(ydl
.downloaded_info_dicts
[1]['format_id'], 'good') 
 124         self
.assertEqual(ydl
.downloaded_info_dicts
[2]['format_id'], 'great') 
 125         self
.assertTrue('3' in ydl
.msgs
[0]) 
 128         ydl
.params
['format_limit'] = 'excellent' 
 129         ydl
.process_ie_result(info_dict
.copy()) 
 130         downloaded 
= ydl
.downloaded_info_dicts
[0] 
 131         self
.assertEqual(downloaded
['format_id'], 'excellent') 
 133     def test_format_selection(self
): 
 135             {'format_id': '35', 'ext': 'mp4', 'preference': 1, 'url': '_'}, 
 136             {'format_id': '45', 'ext': 'webm', 'preference': 2, 'url': '_'}, 
 137             {'format_id': '47', 'ext': 'webm', 'preference': 3, 'url': '_'}, 
 138             {'format_id': '2', 'ext': 'flv', 'preference': 4, 'url': '_'}, 
 140         info_dict 
= _make_result(formats
) 
 142         ydl 
= YDL({'format': '20/47'}) 
 143         ydl
.process_ie_result(info_dict
.copy()) 
 144         downloaded 
= ydl
.downloaded_info_dicts
[0] 
 145         self
.assertEqual(downloaded
['format_id'], '47') 
 147         ydl 
= YDL({'format': '20/71/worst'}) 
 148         ydl
.process_ie_result(info_dict
.copy()) 
 149         downloaded 
= ydl
.downloaded_info_dicts
[0] 
 150         self
.assertEqual(downloaded
['format_id'], '35') 
 153         ydl
.process_ie_result(info_dict
.copy()) 
 154         downloaded 
= ydl
.downloaded_info_dicts
[0] 
 155         self
.assertEqual(downloaded
['format_id'], '2') 
 157         ydl 
= YDL({'format': 'webm/mp4'}) 
 158         ydl
.process_ie_result(info_dict
.copy()) 
 159         downloaded 
= ydl
.downloaded_info_dicts
[0] 
 160         self
.assertEqual(downloaded
['format_id'], '47') 
 162         ydl 
= YDL({'format': '3gp/40/mp4'}) 
 163         ydl
.process_ie_result(info_dict
.copy()) 
 164         downloaded 
= ydl
.downloaded_info_dicts
[0] 
 165         self
.assertEqual(downloaded
['format_id'], '35') 
 167     def test_format_selection_audio(self
): 
 169             {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none', 'url': '_'}, 
 170             {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none', 'url': '_'}, 
 171             {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none', 'url': '_'}, 
 172             {'format_id': 'vid', 'ext': 'mp4', 'preference': 4, 'url': '_'}, 
 174         info_dict 
= _make_result(formats
) 
 176         ydl 
= YDL({'format': 'bestaudio'}) 
 177         ydl
.process_ie_result(info_dict
.copy()) 
 178         downloaded 
= ydl
.downloaded_info_dicts
[0] 
 179         self
.assertEqual(downloaded
['format_id'], 'audio-high') 
 181         ydl 
= YDL({'format': 'worstaudio'}) 
 182         ydl
.process_ie_result(info_dict
.copy()) 
 183         downloaded 
= ydl
.downloaded_info_dicts
[0] 
 184         self
.assertEqual(downloaded
['format_id'], 'audio-low') 
 187             {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': '_'}, 
 188             {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': '_'}, 
 190         info_dict 
= _make_result(formats
) 
 192         ydl 
= YDL({'format': 'bestaudio/worstaudio/best'}) 
 193         ydl
.process_ie_result(info_dict
.copy()) 
 194         downloaded 
= ydl
.downloaded_info_dicts
[0] 
 195         self
.assertEqual(downloaded
['format_id'], 'vid-high') 
 197     def test_format_selection_audio_exts(self
): 
 199             {'format_id': 'mp3-64', 'ext': 'mp3', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, 
 200             {'format_id': 'ogg-64', 'ext': 'ogg', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, 
 201             {'format_id': 'aac-64', 'ext': 'aac', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, 
 202             {'format_id': 'mp3-32', 'ext': 'mp3', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'}, 
 203             {'format_id': 'aac-32', 'ext': 'aac', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'}, 
 206         info_dict 
= _make_result(formats
) 
 207         ydl 
= YDL({'format': 'best'}) 
 209         ie
._sort
_formats
(info_dict
['formats']) 
 210         ydl
.process_ie_result(copy
.deepcopy(info_dict
)) 
 211         downloaded 
= ydl
.downloaded_info_dicts
[0] 
 212         self
.assertEqual(downloaded
['format_id'], 'aac-64') 
 214         ydl 
= YDL({'format': 'mp3'}) 
 216         ie
._sort
_formats
(info_dict
['formats']) 
 217         ydl
.process_ie_result(copy
.deepcopy(info_dict
)) 
 218         downloaded 
= ydl
.downloaded_info_dicts
[0] 
 219         self
.assertEqual(downloaded
['format_id'], 'mp3-64') 
 221         ydl 
= YDL({'prefer_free_formats': True}) 
 223         ie
._sort
_formats
(info_dict
['formats']) 
 224         ydl
.process_ie_result(copy
.deepcopy(info_dict
)) 
 225         downloaded 
= ydl
.downloaded_info_dicts
[0] 
 226         self
.assertEqual(downloaded
['format_id'], 'ogg-64') 
 228     def test_format_selection_video(self
): 
 230             {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none', 'url': '_'}, 
 231             {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none', 'url': '_'}, 
 232             {'format_id': 'vid', 'ext': 'mp4', 'preference': 3, 'url': '_'}, 
 234         info_dict 
= _make_result(formats
) 
 236         ydl 
= YDL({'format': 'bestvideo'}) 
 237         ydl
.process_ie_result(info_dict
.copy()) 
 238         downloaded 
= ydl
.downloaded_info_dicts
[0] 
 239         self
.assertEqual(downloaded
['format_id'], 'dash-video-high') 
 241         ydl 
= YDL({'format': 'worstvideo'}) 
 242         ydl
.process_ie_result(info_dict
.copy()) 
 243         downloaded 
= ydl
.downloaded_info_dicts
[0] 
 244         self
.assertEqual(downloaded
['format_id'], 'dash-video-low') 
 246     def test_youtube_format_selection(self
): 
 248             '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '36', '17', '13', 
 249             # Apple HTTP Live Streaming 
 250             '96', '95', '94', '93', '92', '132', '151', 
 252             '85', '84', '102', '83', '101', '82', '100', 
 254             '137', '248', '136', '247', '135', '246', 
 255             '245', '244', '134', '243', '133', '242', '160', 
 257             '141', '172', '140', '171', '139', 
 260         for f1id
, f2id 
in zip(order
, order
[1:]): 
 261             f1 
= YoutubeIE
._formats
[f1id
].copy() 
 262             f1
['format_id'] = f1id
 
 263             f1
['url'] = 'url:' + f1id
 
 264             f2 
= YoutubeIE
._formats
[f2id
].copy() 
 265             f2
['format_id'] = f2id
 
 266             f2
['url'] = 'url:' + f2id
 
 268             info_dict 
= _make_result([f1
, f2
], extractor
='youtube') 
 271             yie
._sort
_formats
(info_dict
['formats']) 
 272             ydl
.process_ie_result(info_dict
) 
 273             downloaded 
= ydl
.downloaded_info_dicts
[0] 
 274             self
.assertEqual(downloaded
['format_id'], f1id
) 
 276             info_dict 
= _make_result([f2
, f1
], extractor
='youtube') 
 279             yie
._sort
_formats
(info_dict
['formats']) 
 280             ydl
.process_ie_result(info_dict
) 
 281             downloaded 
= ydl
.downloaded_info_dicts
[0] 
 282             self
.assertEqual(downloaded
['format_id'], f1id
) 
 284     def test_add_extra_info(self
): 
 290             'playlist': 'funny videos', 
 292         YDL
.add_extra_info(test_dict
, extra_info
) 
 293         self
.assertEqual(test_dict
['extractor'], 'Foo') 
 294         self
.assertEqual(test_dict
['playlist'], 'funny videos') 
 296     def test_prepare_filename(self
): 
 304             ydl 
= YoutubeDL({'outtmpl': templ
}) 
 305             return ydl
.prepare_filename(info
) 
 306         self
.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4') 
 307         self
.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4') 
 308         # Replace missing fields with 'NA' 
 309         self
.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4') 
 311     def test_format_note(self
): 
 313         self
.assertEqual(ydl
._format
_note
({}), '') 
 314         assertRegexpMatches(self
, ydl
._format
_note
({ 
 318 if __name__ 
== '__main__':