From e0ab98541cec9515d94ada8885a0bb999277f0c0 Mon Sep 17 00:00:00 2001 From: pukkandan Date: Mon, 6 Jun 2022 21:18:44 +0530 Subject: [PATCH] [ExtractAudio] Allow conditional conversion Closes #1715 --- README.md | 4 +++- yt_dlp/__init__.py | 2 +- yt_dlp/options.py | 3 ++- yt_dlp/postprocessor/ffmpeg.py | 9 ++++++--- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5347f2789..c14f4b365 100644 --- a/README.md +++ b/README.md @@ -873,7 +873,9 @@ You can also fork the project on github and run your fork's [build workflow](.gi (requires ffmpeg and ffprobe) --audio-format FORMAT Format to convert the audio to when -x is used. (currently supported: best (default), - mp3, aac, m4a, opus, vorbis, flac, alac, wav) + mp3, aac, m4a, opus, vorbis, flac, alac, + wav). You can specify multiple rules using + similar syntax as --remux-video --audio-quality QUALITY Specify ffmpeg audio quality to use when converting the audio with -x. Insert a value between 0 (best) and 10 (worst) for VBR or a diff --git a/yt_dlp/__init__.py b/yt_dlp/__init__.py index 10b31028b..d42a3f0d3 100644 --- a/yt_dlp/__init__.py +++ b/yt_dlp/__init__.py @@ -213,7 +213,7 @@ def validate_options(opts): validate_regex('format sorting', f, InfoExtractor.FormatSort.regex) # Postprocessor formats - validate_in('audio format', opts.audioformat, ['best'] + list(FFmpegExtractAudioPP.SUPPORTED_EXTS)) + validate_regex('audio format', opts.audioformat, FFmpegExtractAudioPP.FORMAT_RE) validate_in('subtitle format', opts.convertsubtitles, FFmpegSubtitlesConvertorPP.SUPPORTED_EXTS) validate_regex('thumbnail format', opts.convertthumbnails, FFmpegThumbnailsConvertorPP.FORMAT_RE) validate_regex('recode video format', opts.recodevideo, FFmpegVideoConvertorPP.FORMAT_RE) diff --git a/yt_dlp/options.py b/yt_dlp/options.py index 97d8c61a9..a9a2ba45f 100644 --- a/yt_dlp/options.py +++ b/yt_dlp/options.py @@ -1424,7 +1424,8 @@ def create_parser(): '--audio-format', metavar='FORMAT', dest='audioformat', default='best', help=( 'Format to convert the audio to when -x is used. ' - f'(currently supported: best (default), {", ".join(FFmpegExtractAudioPP.SUPPORTED_EXTS)})')) + f'(currently supported: best (default), {", ".join(FFmpegExtractAudioPP.SUPPORTED_EXTS)}). ' + 'You can specify multiple rules using similar syntax as --remux-video')) postproc.add_option( '--audio-quality', metavar='QUALITY', dest='audioquality', default='5', diff --git a/yt_dlp/postprocessor/ffmpeg.py b/yt_dlp/postprocessor/ffmpeg.py index e38b493c2..a726a962f 100644 --- a/yt_dlp/postprocessor/ffmpeg.py +++ b/yt_dlp/postprocessor/ffmpeg.py @@ -426,10 +426,11 @@ class FFmpegPostProcessor(PostProcessor): class FFmpegExtractAudioPP(FFmpegPostProcessor): COMMON_AUDIO_EXTS = ('wav', 'flac', 'm4a', 'aiff', 'mp3', 'ogg', 'mka', 'opus', 'wma') SUPPORTED_EXTS = tuple(ACODECS.keys()) + FORMAT_RE = create_mapping_re(('best', *SUPPORTED_EXTS)) def __init__(self, downloader=None, preferredcodec=None, preferredquality=None, nopostoverwrites=False): FFmpegPostProcessor.__init__(self, downloader) - self._preferredcodec = preferredcodec or 'best' + self.mapping = preferredcodec or 'best' self._preferredquality = float_or_none(preferredquality) self._nopostoverwrites = nopostoverwrites @@ -469,9 +470,11 @@ class FFmpegExtractAudioPP(FFmpegPostProcessor): @PostProcessor._restrict_to(images=False) def run(self, information): orig_path = path = information['filepath'] - target_format = self._preferredcodec + target_format, _skip_msg = resolve_mapping(information['ext'], self.mapping) if target_format == 'best' and information['ext'] in self.COMMON_AUDIO_EXTS: - self.to_screen(f'Not converting audio {orig_path}; the file is already in a common audio format') + target_format, _skip_msg = None, 'the file is already in a common audio format' + if not target_format: + self.to_screen(f'Not converting audio {orig_path}; {_skip_msg}') return [], information filecodec = self.get_audio_codec(path)