[ExtractAudio] Rescale --audio-quality correctly

Authored by: CrypticSignal, pukkandan
This commit is contained in:
pukkandan 2021-11-04 00:05:53 +05:30
parent bd93fd5d45
commit 31c49255bf
No known key found for this signature in database
GPG key ID: 0F00D95A001F4698
4 changed files with 28 additions and 17 deletions

View file

@ -29,6 +29,8 @@
error_to_compat_str,
ExistingVideoReached,
expand_path,
float_or_none,
int_or_none,
match_filter_func,
MaxDownloadsReached,
parse_duration,
@ -230,7 +232,7 @@ def parse_retries(retries, name=''):
parser.error('invalid audio format specified')
if opts.audioquality:
opts.audioquality = opts.audioquality.strip('k').strip('K')
if not opts.audioquality.isdigit():
if int_or_none(float_or_none(opts.audioquality)) is None: # int_or_none prevents inf, nan
parser.error('invalid audio quality specified')
if opts.recodevideo is not None:
opts.recodevideo = opts.recodevideo.replace(' ', '')

View file

@ -1215,7 +1215,7 @@ def _dict_from_options_callback(
postproc.add_option(
'--audio-quality', metavar='QUALITY',
dest='audioquality', default='5',
help='Specify ffmpeg audio quality, insert a value between 0 (better) and 9 (worse) for VBR or a specific bitrate like 128K (default %default)')
help='Specify ffmpeg audio quality, insert a value between 0 (best) and 10 (worst) for VBR or a specific bitrate like 128K (default %default)')
postproc.add_option(
'--remux-video',
metavar='FORMAT', dest='remuxvideo', default=None,

View file

@ -371,9 +371,29 @@ class FFmpegExtractAudioPP(FFmpegPostProcessor):
def __init__(self, downloader=None, preferredcodec=None, preferredquality=None, nopostoverwrites=False):
FFmpegPostProcessor.__init__(self, downloader)
self._preferredcodec = preferredcodec or 'best'
self._preferredquality = preferredquality
self._preferredquality = float_or_none(preferredquality)
self._nopostoverwrites = nopostoverwrites
def _quality_args(self, codec):
if self._preferredquality is None:
return []
elif self._preferredquality > 10:
return ['-b:a', f'{self._preferredquality}k']
limits = {
'libmp3lame': (10, 0),
'aac': (0.1, 11),
'vorbis': (0, 10),
'opus': None, # doesn't support -q:a
'wav': None,
'flac': None,
}[codec]
if not limits:
return []
q = limits[1] + (limits[0] - limits[1]) * (self._preferredquality / 10)
return ['-q:a', f'{q}']
def run_ffmpeg(self, path, out_path, codec, more_opts):
if codec is None:
acodec_opts = []
@ -417,23 +437,12 @@ def run(self, information):
# MP3 otherwise.
acodec = 'libmp3lame'
extension = 'mp3'
more_opts = []
if self._preferredquality is not None:
if int(self._preferredquality) < 10:
more_opts += ['-q:a', self._preferredquality]
else:
more_opts += ['-b:a', self._preferredquality + 'k']
more_opts = self._quality_args(acodec)
else:
# We convert the audio (lossy if codec is lossy)
acodec = ACODECS[self._preferredcodec]
extension = self._preferredcodec
more_opts = []
if self._preferredquality is not None:
# The opus codec doesn't support the -aq option
if int(self._preferredquality) < 10 and extension != 'opus':
more_opts += ['-q:a', self._preferredquality]
else:
more_opts += ['-b:a', self._preferredquality + 'k']
more_opts = self._quality_args(acodec)
if self._preferredcodec == 'aac':
more_opts += ['-f', 'adts']
if self._preferredcodec == 'm4a':

View file

@ -3871,7 +3871,7 @@ def int_or_none(v, scale=1, default=None, get_attr=None, invscale=1):
return default
try:
return int(v) * invscale // scale
except (ValueError, TypeError):
except (ValueError, TypeError, OverflowError):
return default