From 6425e15ea14a91d09deb0ec774659d8db328e5a1 Mon Sep 17 00:00:00 2001 From: Andrei Lebedev Date: Tue, 9 Aug 2022 23:13:47 +0200 Subject: [PATCH] [YoutubeDL] Prevent failure on non-audio-video formats --- test/test_YoutubeDL.py | 12 ++++++++++++ youtube_dl/YoutubeDL.py | 8 ++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/test/test_YoutubeDL.py b/test/test_YoutubeDL.py index f8c8e619c..04a98115c 100644 --- a/test/test_YoutubeDL.py +++ b/test/test_YoutubeDL.py @@ -454,6 +454,18 @@ class TestFormatSelection(unittest.TestCase): ydl.process_ie_result(info_dict.copy()) self.assertEqual(ydl.downloaded_info_dicts[0]['format_id'], 'video+audio') + def test_format_selection_non_audio_video(self): + formats = [ + {'format_id': 'audio', 'url': TEST_URL, 'ext': 'm4a', 'vcodec': 'none'}, + {'format_id': 'chapter', 'ext': 'mhtml', 'acodec': 'none', 'vcodec': 'none', 'url': 'about:invalid'}, + {'format_id': 'video', 'url': TEST_URL, 'ext': 'mp4', 'acodec': 'none'}, + ] + info_dict = _make_result(formats) + + ydl = YDL({'format': 'bestvideo+bestaudio'}) + ydl.process_ie_result(info_dict.copy()) + self.assertEqual(ydl.downloaded_info_dicts[0]['format_id'], 'video+audio') + def test_invalid_format_specs(self): def assert_syntax_error(format_spec): ydl = YDL({'format': format_spec}) diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 3895b408f..49c3d2d29 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -1319,25 +1319,25 @@ class YoutubeDL(object): elif format_spec == 'bestaudio': audio_formats = [ f for f in formats - if f.get('vcodec') == 'none'] + if f.get('vcodec') == 'none' and f.get('acodec') != 'none'] if audio_formats: yield audio_formats[-1] elif format_spec == 'worstaudio': audio_formats = [ f for f in formats - if f.get('vcodec') == 'none'] + if f.get('vcodec') == 'none' and f.get('acodec') != 'none'] if audio_formats: yield audio_formats[0] elif format_spec == 'bestvideo': video_formats = [ f for f in formats - if f.get('acodec') == 'none'] + if f.get('acodec') == 'none' and f.get('vcodec') != 'none'] if video_formats: yield video_formats[-1] elif format_spec == 'worstvideo': video_formats = [ f for f in formats - if f.get('acodec') == 'none'] + if f.get('acodec') == 'none' and f.get('vcodec') != 'none'] if video_formats: yield video_formats[0] else: