[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 @@ from .utils import (
error_to_compat_str, error_to_compat_str,
ExistingVideoReached, ExistingVideoReached,
expand_path, expand_path,
float_or_none,
int_or_none,
match_filter_func, match_filter_func,
MaxDownloadsReached, MaxDownloadsReached,
parse_duration, parse_duration,
@ -230,7 +232,7 @@ def _real_main(argv=None):
parser.error('invalid audio format specified') parser.error('invalid audio format specified')
if opts.audioquality: if opts.audioquality:
opts.audioquality = opts.audioquality.strip('k').strip('K') 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') parser.error('invalid audio quality specified')
if opts.recodevideo is not None: if opts.recodevideo is not None:
opts.recodevideo = opts.recodevideo.replace(' ', '') opts.recodevideo = opts.recodevideo.replace(' ', '')

View file

@ -1215,7 +1215,7 @@ def parseOpts(overrideArguments=None):
postproc.add_option( postproc.add_option(
'--audio-quality', metavar='QUALITY', '--audio-quality', metavar='QUALITY',
dest='audioquality', default='5', 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( postproc.add_option(
'--remux-video', '--remux-video',
metavar='FORMAT', dest='remuxvideo', default=None, 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): 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._preferredcodec = preferredcodec or 'best'
self._preferredquality = preferredquality self._preferredquality = float_or_none(preferredquality)
self._nopostoverwrites = nopostoverwrites 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): def run_ffmpeg(self, path, out_path, codec, more_opts):
if codec is None: if codec is None:
acodec_opts = [] acodec_opts = []
@ -417,23 +437,12 @@ class FFmpegExtractAudioPP(FFmpegPostProcessor):
# MP3 otherwise. # MP3 otherwise.
acodec = 'libmp3lame' acodec = 'libmp3lame'
extension = 'mp3' extension = 'mp3'
more_opts = [] more_opts = self._quality_args(acodec)
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']
else: else:
# We convert the audio (lossy if codec is lossy) # We convert the audio (lossy if codec is lossy)
acodec = ACODECS[self._preferredcodec] acodec = ACODECS[self._preferredcodec]
extension = self._preferredcodec extension = self._preferredcodec
more_opts = [] more_opts = self._quality_args(acodec)
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']
if self._preferredcodec == 'aac': if self._preferredcodec == 'aac':
more_opts += ['-f', 'adts'] more_opts += ['-f', 'adts']
if self._preferredcodec == 'm4a': 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 return default
try: try:
return int(v) * invscale // scale return int(v) * invscale // scale
except (ValueError, TypeError): except (ValueError, TypeError, OverflowError):
return default return default