Add -f bestaudio (Fixes #2163)
authorPhilipp Hagemeister <phihag@phihag.de>
Wed, 22 Jan 2014 13:47:29 +0000 (14:47 +0100)
committerPhilipp Hagemeister <phihag@phihag.de>
Wed, 22 Jan 2014 13:47:29 +0000 (14:47 +0100)
test/test_YoutubeDL.py
youtube_dl/YoutubeDL.py
youtube_dl/__init__.py

index 01de10e311865df805979e1f362f61ab25592a10..3adb9f344b0a78ee5c9e31aafec5ee9ad1d166aa 100644 (file)
@@ -150,6 +150,36 @@ class TestFormatSelection(unittest.TestCase):
         downloaded = ydl.downloaded_info_dicts[0]
         self.assertEqual(downloaded['format_id'], u'35')
 
+    def test_format_selection_audio(self):
+        formats = [
+            {u'format_id': u'audio-low', u'ext': u'webm', 'preference': 1, 'vcodec': 'none'},
+            {u'format_id': u'audio-mid', u'ext': u'webm', 'preference': 2, 'vcodec': 'none'},
+            {u'format_id': u'audio-high', u'ext': u'flv', 'preference': 3, 'vcodec': 'none'},
+            {u'format_id': u'vid', u'ext': u'mp4', 'preference': 4},
+        ]
+        info_dict = {u'formats': formats, u'extractor': u'test'}
+
+        ydl = YDL({'format': u'bestaudio'})
+        ydl.process_ie_result(info_dict.copy())
+        downloaded = ydl.downloaded_info_dicts[0]
+        self.assertEqual(downloaded['format_id'], u'audio-high')
+
+        ydl = YDL({'format': u'worstaudio'})
+        ydl.process_ie_result(info_dict.copy())
+        downloaded = ydl.downloaded_info_dicts[0]
+        self.assertEqual(downloaded['format_id'], u'audio-low')
+
+        formats = [
+            {u'format_id': u'vid-low', u'ext': u'mp4', 'preference': 1},
+            {u'format_id': u'vid-high', u'ext': u'mp4', 'preference': 2},
+        ]
+        info_dict = {u'formats': formats, u'extractor': u'test'}
+
+        ydl = YDL({'format': u'bestaudio/worstaudio/best'})
+        ydl.process_ie_result(info_dict.copy())
+        downloaded = ydl.downloaded_info_dicts[0]
+        self.assertEqual(downloaded['format_id'], u'vid-high')
+
     def test_youtube_format_selection(self):
         order = [
             '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '36', '17', '13',
index c6430d3670d56ff0e60c1bc47958dc8b0348c08d..9f15616fa1a8c2cd7a2679795fdc8ccce76c6c00 100644 (file)
@@ -637,6 +637,18 @@ class YoutubeDL(object):
             return available_formats[-1]
         elif format_spec == 'worst':
             return available_formats[0]
+        elif format_spec == 'bestaudio':
+            audio_formats = [
+                f for f in available_formats
+                if f.get('vcodec') == 'none']
+            if audio_formats:
+                return audio_formats[-1]
+        elif format_spec == 'worstaudio':
+            audio_formats = [
+                f for f in available_formats
+                if f.get('vcodec') == 'none']
+            if audio_formats:
+                return audio_formats[0]
         else:
             extensions = ['mp4', 'flv', 'webm', '3gp']
             if format_spec in extensions:
index 44047888d04f55b7645e20503463d2aa5f7c6ac9..4db97ad3c20e2f17ce5f9376fe9b7dd1487edc97 100644 (file)
@@ -257,7 +257,7 @@ def parseOpts(overrideArguments=None):
 
     video_format.add_option('-f', '--format',
             action='store', dest='format', metavar='FORMAT', default='best',
-            help='video format code, specify the order of preference using slashes: "-f 22/17/18". "-f mp4" and "-f flv" are also supported')
+            help='video format code, specify the order of preference using slashes: "-f 22/17/18". "-f mp4" and "-f flv" are also supported. You can also use the special names "best", "bestaudio", and "worst"')
     video_format.add_option('--all-formats',
             action='store_const', dest='format', help='download all available video formats', const='all')
     video_format.add_option('--prefer-free-formats',