summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJaime Marquínez Ferrándiz <[email protected]>2013-10-21 13:19:58 +0200
committerJaime Marquínez Ferrándiz <[email protected]>2013-10-21 13:19:58 +0200
commita9c58ad945e88e8eadbbff9c165c19b46805063b (patch)
treecb89e6ca2e09bcfca95d53511df8811b68e5366e
parentf8b45beacccbc5c237b0a04a568b51d602b1c1d3 (diff)
downloadyoutube-dl-a9c58ad945e88e8eadbbff9c165c19b46805063b.tar.gz
youtube-dl-a9c58ad945e88e8eadbbff9c165c19b46805063b.zip
Accept requested formats to be in the format 35/best (closes #1552)
The format selection code is now an independent function.
-rw-r--r--test/test_YoutubeDL.py23
-rw-r--r--youtube_dl/YoutubeDL.py27
2 files changed, 41 insertions, 9 deletions
diff --git a/test/test_YoutubeDL.py b/test/test_YoutubeDL.py
index ba6dc05bc..2073bc4df 100644
--- a/test/test_YoutubeDL.py
+++ b/test/test_YoutubeDL.py
@@ -94,6 +94,29 @@ class TestFormatSelection(unittest.TestCase):
downloaded = ydl.downloaded_info_dicts[0]
self.assertEqual(downloaded[u'format_id'], u'excellent')
+ def test_format_selection(self):
+ formats = [
+ {u'format_id': u'35'},
+ {u'format_id': u'47'},
+ {u'format_id': u'2'},
+ ]
+ info_dict = {u'formats': formats, u'extractor': u'test'}
+
+ ydl = YDL({'format': u'20/47'})
+ ydl.process_ie_result(info_dict)
+ downloaded = ydl.downloaded_info_dicts[0]
+ self.assertEqual(downloaded['format_id'], u'47')
+
+ ydl = YDL({'format': u'20/71/worst'})
+ ydl.process_ie_result(info_dict)
+ downloaded = ydl.downloaded_info_dicts[0]
+ self.assertEqual(downloaded['format_id'], u'35')
+
+ ydl = YDL()
+ ydl.process_ie_result(info_dict)
+ downloaded = ydl.downloaded_info_dicts[0]
+ self.assertEqual(downloaded['format_id'], u'2')
+
if __name__ == '__main__':
unittest.main()
diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py
index 296c0f992..bc69214e7 100644
--- a/youtube_dl/YoutubeDL.py
+++ b/youtube_dl/YoutubeDL.py
@@ -448,6 +448,17 @@ class YoutubeDL(object):
else:
raise Exception('Invalid result type: %s' % result_type)
+ def select_format(self, format_spec, available_formats):
+ if format_spec == 'best' or format_spec is None:
+ return available_formats[-1]
+ elif format_spec == 'worst':
+ return available_formats[0]
+ else:
+ matches = list(filter(lambda f:f['format_id'] == format_spec ,available_formats))
+ if matches:
+ return matches[-1]
+ return None
+
def process_video_result(self, info_dict, download=True):
assert info_dict.get('_type', 'video') == 'video'
@@ -502,22 +513,20 @@ class YoutubeDL(object):
formats = sorted(formats, key=_free_formats_key)
req_format = self.params.get('format', 'best')
+ if req_format is None:
+ req_format = 'best'
formats_to_download = []
- if req_format == 'best' or req_format is None:
- formats_to_download = [formats[-1]]
- elif req_format == 'worst':
- formats_to_download = [formats[0]]
# The -1 is for supporting YoutubeIE
- elif req_format in ('-1', 'all'):
+ if req_format in ('-1', 'all'):
formats_to_download = formats
else:
- # We can accept formats requestd in the format: 34/10/5, we pick
+ # We can accept formats requestd in the format: 34/5/best, we pick
# the first that is available, starting from left
req_formats = req_format.split('/')
for rf in req_formats:
- matches = filter(lambda f:f['format_id'] == rf ,formats)
- if matches:
- formats_to_download = [matches[0]]
+ selected_format = self.select_format(rf, formats)
+ if selected_format is not None:
+ formats_to_download = [selected_format]
break
if not formats_to_download:
raise ExtractorError(u'requested format not available')