[cleanup] Refactor fixup

This commit is contained in:
pukkandan 2021-06-20 03:49:23 +05:30
parent 4e6767b5f2
commit fd7cfb6444
No known key found for this signature in database
GPG Key ID: 0F00D95A001F4698
3 changed files with 62 additions and 99 deletions

View File

@ -570,14 +570,9 @@ class YoutubeDL(object):
self.add_default_info_extractors() self.add_default_info_extractors()
for pp_def_raw in self.params.get('postprocessors', []): for pp_def_raw in self.params.get('postprocessors', []):
pp_class = get_postprocessor(pp_def_raw['key'])
pp_def = dict(pp_def_raw) pp_def = dict(pp_def_raw)
del pp_def['key'] when = pp_def.pop('when', 'post_process')
if 'when' in pp_def: pp_class = get_postprocessor(pp_def.pop('key'))
when = pp_def['when']
del pp_def['when']
else:
when = 'post_process'
pp = pp_class(self, **compat_kwargs(pp_def)) pp = pp_class(self, **compat_kwargs(pp_def))
self.add_post_processor(pp, when=when) self.add_post_processor(pp, when=when)
@ -2685,65 +2680,48 @@ class YoutubeDL(object):
return return
if success and full_filename != '-': if success and full_filename != '-':
# Fixup content
fixup_policy = self.params.get('fixup')
if fixup_policy is None:
fixup_policy = 'detect_or_warn'
INSTALL_FFMPEG_MESSAGE = 'Install ffmpeg to fix this automatically.' def fixup():
do_fixup = True
fixup_policy = self.params.get('fixup')
vid = info_dict['id']
stretched_ratio = info_dict.get('stretched_ratio') if fixup_policy in ('ignore', 'never'):
if stretched_ratio is not None and stretched_ratio != 1: return
if fixup_policy == 'warn': elif fixup_policy == 'warn':
self.report_warning('%s: Non-uniform pixel ratio (%s)' % ( do_fixup = False
info_dict['id'], stretched_ratio)) assert fixup_policy in ('detect_or_warn', None)
elif fixup_policy == 'detect_or_warn':
stretched_pp = FFmpegFixupStretchedPP(self) def ffmpeg_fixup(cndn, msg, cls):
if stretched_pp.available: if not cndn:
info_dict['__postprocessors'].append(stretched_pp) return
if not do_fixup:
self.report_warning(f'{vid}: {msg}')
return
pp = cls(self)
if pp.available:
info_dict['__postprocessors'].append(pp)
else: else:
self.report_warning( self.report_warning(f'{vid}: {msg}. Install ffmpeg to fix this automatically')
'%s: Non-uniform pixel ratio (%s). %s'
% (info_dict['id'], stretched_ratio, INSTALL_FFMPEG_MESSAGE))
else:
assert fixup_policy in ('ignore', 'never')
if (info_dict.get('requested_formats') is None stretched_ratio = info_dict.get('stretched_ratio')
and info_dict.get('container') == 'm4a_dash' ffmpeg_fixup(
and info_dict.get('ext') == 'm4a'): stretched_ratio not in (1, None),
if fixup_policy == 'warn': f'Non-uniform pixel ratio {stretched_ratio}',
self.report_warning( FFmpegFixupStretchedPP)
'%s: writing DASH m4a. '
'Only some players support this container.'
% info_dict['id'])
elif fixup_policy == 'detect_or_warn':
fixup_pp = FFmpegFixupM4aPP(self)
if fixup_pp.available:
info_dict['__postprocessors'].append(fixup_pp)
else:
self.report_warning(
'%s: writing DASH m4a. '
'Only some players support this container. %s'
% (info_dict['id'], INSTALL_FFMPEG_MESSAGE))
else:
assert fixup_policy in ('ignore', 'never')
if ('protocol' in info_dict ffmpeg_fixup(
and get_suitable_downloader(info_dict, self.params).__name__ == 'HlsFD'): (info_dict.get('requested_formats') is None
if fixup_policy == 'warn': and info_dict.get('container') == 'm4a_dash'
self.report_warning('%s: malformed AAC bitstream detected.' % ( and info_dict.get('ext') == 'm4a'),
info_dict['id'])) 'writing DASH m4a. Only some players support this container',
elif fixup_policy == 'detect_or_warn': FFmpegFixupM4aPP)
fixup_pp = FFmpegFixupM3u8PP(self)
if fixup_pp.available:
info_dict['__postprocessors'].append(fixup_pp)
else:
self.report_warning(
'%s: malformed AAC bitstream detected. %s'
% (info_dict['id'], INSTALL_FFMPEG_MESSAGE))
else:
assert fixup_policy in ('ignore', 'never')
downloader = (get_suitable_downloader(info_dict, self.params).__name__
if 'protocol' in info_dict else None)
ffmpeg_fixup(downloader == 'HlsFD', 'malformed AAC bitstream detected', FFmpegFixupM3u8PP)
fixup()
try: try:
info_dict = self.post_process(dl_filename, info_dict, files_to_move) info_dict = self.post_process(dl_filename, info_dict, files_to_move)
except PostProcessingError as err: except PostProcessingError as err:

View File

@ -1230,6 +1230,7 @@ def parseOpts(overrideArguments=None):
postproc.add_option( postproc.add_option(
'--fixup', '--fixup',
metavar='POLICY', dest='fixup', default=None, metavar='POLICY', dest='fixup', default=None,
choices=('never', 'ignore', 'warn', 'detect_or_warn'),
help=( help=(
'Automatically correct known faults of the file. ' 'Automatically correct known faults of the file. '
'One of never (do nothing), warn (only emit a warning), ' 'One of never (do nothing), warn (only emit a warning), '

View File

@ -661,58 +661,42 @@ class FFmpegMergerPP(FFmpegPostProcessor):
return True return True
class FFmpegFixupStretchedPP(FFmpegPostProcessor): class FFmpegFixupPostProcessor(FFmpegPostProcessor):
def _fixup(self, msg, filename, options):
temp_filename = prepend_extension(filename, 'temp')
self.to_screen('{msg} of "{filename}"')
self.run_ffmpeg(filename, temp_filename, options)
os.remove(encodeFilename(filename))
os.rename(encodeFilename(temp_filename), encodeFilename(filename))
class FFmpegFixupStretchedPP(FFmpegFixupPostProcessor):
@PostProcessor._restrict_to(images=False, audio=False) @PostProcessor._restrict_to(images=False, audio=False)
def run(self, info): def run(self, info):
stretched_ratio = info.get('stretched_ratio') stretched_ratio = info.get('stretched_ratio')
if stretched_ratio is None or stretched_ratio == 1: if stretched_ratio not in (None, 1):
return [], info self._fixup('Fixing aspect ratio', info['filepath'], [
'-c', 'copy', '-map', '0', '-dn', '-aspect', '%f' % stretched_ratio])
filename = info['filepath']
temp_filename = prepend_extension(filename, 'temp')
options = ['-c', 'copy', '-map', '0', '-dn', '-aspect', '%f' % stretched_ratio]
self.to_screen('Fixing aspect ratio in "%s"' % filename)
self.run_ffmpeg(filename, temp_filename, options)
os.remove(encodeFilename(filename))
os.rename(encodeFilename(temp_filename), encodeFilename(filename))
return [], info return [], info
class FFmpegFixupM4aPP(FFmpegPostProcessor): class FFmpegFixupM4aPP(FFmpegFixupPostProcessor):
@PostProcessor._restrict_to(images=False, video=False) @PostProcessor._restrict_to(images=False, video=False)
def run(self, info): def run(self, info):
if info.get('container') != 'm4a_dash': if info.get('container') == 'm4a_dash':
return [], info self._fixup('Correcting container', info['filepath'], [
'-c', 'copy', '-map', '0', '-dn', '-f', 'mp4'])
filename = info['filepath']
temp_filename = prepend_extension(filename, 'temp')
options = ['-c', 'copy', '-map', '0', '-dn', '-f', 'mp4']
self.to_screen('Correcting container in "%s"' % filename)
self.run_ffmpeg(filename, temp_filename, options)
os.remove(encodeFilename(filename))
os.rename(encodeFilename(temp_filename), encodeFilename(filename))
return [], info return [], info
class FFmpegFixupM3u8PP(FFmpegPostProcessor): class FFmpegFixupM3u8PP(FFmpegFixupPostProcessor):
@PostProcessor._restrict_to(images=False) @PostProcessor._restrict_to(images=False)
def run(self, info): def run(self, info):
filename = info['filepath'] if self.get_audio_codec(info['filepath']) == 'aac':
if self.get_audio_codec(filename) == 'aac': self._fixup('Fixing malformed AAC bitstream', info['filepath'], [
temp_filename = prepend_extension(filename, 'temp') '-c', 'copy', '-map', '0', '-dn', '-f', 'mp4', '-bsf:a', 'aac_adtstoasc'])
options = ['-c', 'copy', '-map', '0', '-dn', '-f', 'mp4', '-bsf:a', 'aac_adtstoasc']
self.to_screen('Fixing malformed AAC bitstream in "%s"' % filename)
self.run_ffmpeg(filename, temp_filename, options)
os.remove(encodeFilename(filename))
os.rename(encodeFilename(temp_filename), encodeFilename(filename))
return [], info return [], info