From 3a3b57cc2389c5fed6fe1a7694bbcc1bce6c0cb9 Mon Sep 17 00:00:00 2001 From: Alex Zepeda Date: Sat, 23 May 2020 19:58:25 -0700 Subject: [PATCH 1/2] First pass at smallestvideo/smallestaudio filters --- README.md | 2 ++ youtube_dl/YoutubeDL.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/README.md b/README.md index 45326c69e..73bad7f6d 100644 --- a/README.md +++ b/README.md @@ -647,8 +647,10 @@ You can also use special names to select particular edge case formats: - `worst`: Select the worst quality format represented by a single file with video and audio. - `bestvideo`: Select the best quality video-only format (e.g. DASH video). May not be available. - `worstvideo`: Select the worst quality video-only format. May not be available. + - `smallestvideo`: Select the smallest video-only format. May not be available. - `bestaudio`: Select the best quality audio only-format. May not be available. - `worstaudio`: Select the worst quality audio only-format. May not be available. + - `smallestaudio`: Select the smallest audio-only format. May not be available. For example, to download the worst quality video-only format you can use `-f worstvideo`. diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 19370f62b..a965a23be 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -1283,6 +1283,13 @@ class YoutubeDL(object): if f.get('vcodec') == 'none'] if audio_formats: yield audio_formats[0] + elif format_spec == 'smallestaudio': + video_formats = [ + f for f in formats + if f.get('vcodec') == 'none'] + audio_formats = sorted(audio_formats, key=lambda format: format['filesize']) + if audio_formats: + yield audio_formats[0] elif format_spec == 'bestvideo': video_formats = [ f for f in formats @@ -1295,6 +1302,13 @@ class YoutubeDL(object): if f.get('acodec') == 'none'] if video_formats: yield video_formats[0] + elif format_spec == 'smallestvideo': + video_formats = [ + f for f in formats + if f.get('acodec') == 'none'] + video_formats = sorted(video_formats, key=lambda format: format['filesize']) + if video_formats: + yield video_formats[0] else: extensions = ['mp4', 'flv', 'webm', '3gp', 'm4a', 'mp3', 'ogg', 'aac', 'wav'] if format_spec in extensions: From 864314351215d4739c86c9edf3e641bab59437d0 Mon Sep 17 00:00:00 2001 From: Alex Zepeda Date: Sat, 23 May 2020 20:50:16 -0700 Subject: [PATCH 2/2] Add rudimentary smallest test --- test/test_YoutubeDL.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/test_YoutubeDL.py b/test/test_YoutubeDL.py index 1e204e551..bd422da1c 100644 --- a/test/test_YoutubeDL.py +++ b/test/test_YoutubeDL.py @@ -145,6 +145,22 @@ class TestFormatSelection(unittest.TestCase): downloaded = ydl.downloaded_info_dicts[0] self.assertEqual(downloaded['format_id'], 'example-with-dashes') + def test_format_selection_smallest_video(self): + # 399 mp4 1920x1080 1080p 2720k , av01.0.08M.08, 30fps, video only, 420.27MiB + # 137 mp4 1920x1080 1080p 4343k , avc1.640028, 30fps, video only, 757.36MiB + # 271 webm 2560x1440 1440p 9007k , vp9, 30fps, video only, 1.57GiB + formats = [ + {'format_id': 'av1', ext: 'mp4', 'acodec': 'none', 'filesize': 30, 'url': TEST_URL}, + {'format_id': 'h264', ext: 'mp4', 'acodec': 'none', 'filesize': 10, 'url': TEST_URL}, + {'format_id': 'vp9', ext: 'webm', 'acodec': 'none', 'filesize': 20, 'url': TEST_URL}, + ] + info_dict = _make_result(formats) + + ydl = YDL({'format': 'smallestvideo'}) + ydl.process_ie_result(info_dict.copy()) + downloaded = ydl.downloaded_info_dicts[0] + self.assertEqual(downloaded['format_id'], 'h264') + def test_format_selection_audio(self): formats = [ {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL},