[ExtractAudio] Allow conditional conversion

Closes #1715
This commit is contained in:
pukkandan 2022-06-06 21:18:44 +05:30
parent 35faefee5d
commit e0ab98541c
No known key found for this signature in database
GPG key ID: 7EEE9E1E817D0A39
4 changed files with 12 additions and 6 deletions

View file

@ -873,7 +873,9 @@ ## Post-Processing Options:
(requires ffmpeg and ffprobe) (requires ffmpeg and ffprobe)
--audio-format FORMAT Format to convert the audio to when -x is --audio-format FORMAT Format to convert the audio to when -x is
used. (currently supported: best (default), 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 --audio-quality QUALITY Specify ffmpeg audio quality to use when
converting the audio with -x. Insert a value converting the audio with -x. Insert a value
between 0 (best) and 10 (worst) for VBR or a between 0 (best) and 10 (worst) for VBR or a

View 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) validate_regex('format sorting', f, InfoExtractor.FormatSort.regex)
# Postprocessor formats # 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_in('subtitle format', opts.convertsubtitles, FFmpegSubtitlesConvertorPP.SUPPORTED_EXTS)
validate_regex('thumbnail format', opts.convertthumbnails, FFmpegThumbnailsConvertorPP.FORMAT_RE) validate_regex('thumbnail format', opts.convertthumbnails, FFmpegThumbnailsConvertorPP.FORMAT_RE)
validate_regex('recode video format', opts.recodevideo, FFmpegVideoConvertorPP.FORMAT_RE) validate_regex('recode video format', opts.recodevideo, FFmpegVideoConvertorPP.FORMAT_RE)

View file

@ -1424,7 +1424,8 @@ def _alias_callback(option, opt_str, value, parser, opts, nargs):
'--audio-format', metavar='FORMAT', dest='audioformat', default='best', '--audio-format', metavar='FORMAT', dest='audioformat', default='best',
help=( help=(
'Format to convert the audio to when -x is used. ' '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( postproc.add_option(
'--audio-quality', metavar='QUALITY', '--audio-quality', metavar='QUALITY',
dest='audioquality', default='5', dest='audioquality', default='5',

View file

@ -426,10 +426,11 @@ def _concat_spec(cls, in_files, concat_opts=None):
class FFmpegExtractAudioPP(FFmpegPostProcessor): class FFmpegExtractAudioPP(FFmpegPostProcessor):
COMMON_AUDIO_EXTS = ('wav', 'flac', 'm4a', 'aiff', 'mp3', 'ogg', 'mka', 'opus', 'wma') COMMON_AUDIO_EXTS = ('wav', 'flac', 'm4a', 'aiff', 'mp3', 'ogg', 'mka', 'opus', 'wma')
SUPPORTED_EXTS = tuple(ACODECS.keys()) SUPPORTED_EXTS = tuple(ACODECS.keys())
FORMAT_RE = create_mapping_re(('best', *SUPPORTED_EXTS))
def __init__(self, downloader=None, preferredcodec=None, preferredquality=None, nopostoverwrites=False): def __init__(self, downloader=None, preferredcodec=None, preferredquality=None, nopostoverwrites=False):
FFmpegPostProcessor.__init__(self, downloader) FFmpegPostProcessor.__init__(self, downloader)
self._preferredcodec = preferredcodec or 'best' self.mapping = preferredcodec or 'best'
self._preferredquality = float_or_none(preferredquality) self._preferredquality = float_or_none(preferredquality)
self._nopostoverwrites = nopostoverwrites self._nopostoverwrites = nopostoverwrites
@ -469,9 +470,11 @@ def run_ffmpeg(self, path, out_path, codec, more_opts):
@PostProcessor._restrict_to(images=False) @PostProcessor._restrict_to(images=False)
def run(self, information): def run(self, information):
orig_path = path = information['filepath'] 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: 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 return [], information
filecodec = self.get_audio_codec(path) filecodec = self.get_audio_codec(path)