]> jfr.im git - yt-dlp.git/commitdiff
[ExtractAudio] Allow conditional conversion
authorpukkandan <redacted>
Mon, 6 Jun 2022 15:48:44 +0000 (21:18 +0530)
committerpukkandan <redacted>
Mon, 6 Jun 2022 16:21:28 +0000 (21:51 +0530)
Closes #1715

README.md
yt_dlp/__init__.py
yt_dlp/options.py
yt_dlp/postprocessor/ffmpeg.py

index 5347f2789356f40698e0886c8ecaa01fd9e1e9d1..c14f4b365f7d52c51ee3f792269935fadc924341 100644 (file)
--- a/README.md
+++ b/README.md
@@ -873,7 +873,9 @@ ## Post-Processing Options:
                                     (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
index 10b31028b9182838a78c0c5c62c4dc81ca8d9704..d42a3f0d33cda5b45aa6e25c44f35dbcde6fa50e 100644 (file)
@@ -213,7 +213,7 @@ def validate_minmax(min_val, max_val, min_name, max_name=None):
         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)
index 97d8c61a929b2178baaa8f08e0c0889fcbd6034b..a9a2ba45f460d6c84bbd0ecf43622e6acb0fbec4 100644 (file)
@@ -1424,7 +1424,8 @@ def _alias_callback(option, opt_str, value, parser, opts, nargs):
         '--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',
index e38b493c23c1058a569d97d8548c81cf59bcc24c..a726a962fba0d66eb18552d696b97854bf7651a4 100644 (file)
@@ -426,10 +426,11 @@ def _concat_spec(cls, in_files, concat_opts=None):
 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 @@ def run_ffmpeg(self, path, out_path, codec, more_opts):
     @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)