[utils, cleanup] Refactor parse_codecs

This commit is contained in:
pukkandan 2022-07-10 16:50:54 +05:30
parent 4019bf0525
commit d816f61fbf
No known key found for this signature in database
GPG Key ID: 7EEE9E1E817D0A39
2 changed files with 32 additions and 24 deletions

View File

@ -3523,6 +3523,19 @@ class YoutubeDL:
] for f in formats if f.get('preference') is None or f['preference'] >= -1000] ] for f in formats if f.get('preference') is None or f['preference'] >= -1000]
return render_table(['format code', 'extension', 'resolution', 'note'], table, extra_gap=1) return render_table(['format code', 'extension', 'resolution', 'note'], table, extra_gap=1)
def simplified_codec(f, field):
assert field in ('acodec', 'vcodec')
codec = f.get(field, 'unknown')
if codec != 'none':
return '.'.join(codec.split('.')[:4])
if field == 'vcodec' and f.get('acodec') == 'none':
return 'images'
elif field == 'acodec' and f.get('vcodec') == 'none':
return ''
return self._format_out('audio only' if field == 'vcodec' else 'video only',
self.Styles.SUPPRESS)
delim = self._format_out('\u2502', self.Styles.DELIM, '|', test_encoding=True) delim = self._format_out('\u2502', self.Styles.DELIM, '|', test_encoding=True)
table = [ table = [
[ [
@ -3536,13 +3549,9 @@ class YoutubeDL:
format_field(f, 'tbr', '\t%dk'), format_field(f, 'tbr', '\t%dk'),
shorten_protocol_name(f.get('protocol', '')), shorten_protocol_name(f.get('protocol', '')),
delim, delim,
format_field(f, 'vcodec', default='unknown').replace( simplified_codec(f, 'vcodec'),
'none', 'images' if f.get('acodec') == 'none'
else self._format_out('audio only', self.Styles.SUPPRESS)),
format_field(f, 'vbr', '\t%dk'), format_field(f, 'vbr', '\t%dk'),
format_field(f, 'acodec', default='unknown').replace( simplified_codec(f, 'acodec'),
'none', '' if f.get('vcodec') == 'none'
else self._format_out('video only', self.Styles.SUPPRESS)),
format_field(f, 'abr', '\t%dk'), format_field(f, 'abr', '\t%dk'),
format_field(f, 'asr', '\t%s', func=format_decimal_suffix), format_field(f, 'asr', '\t%s', func=format_decimal_suffix),
join_nonempty( join_nonempty(

View File

@ -3419,24 +3419,23 @@ def parse_codecs(codecs_str):
str.strip, codecs_str.strip().strip(',').split(',')))) str.strip, codecs_str.strip().strip(',').split(','))))
vcodec, acodec, scodec, hdr = None, None, None, None vcodec, acodec, scodec, hdr = None, None, None, None
for full_codec in split_codecs: for full_codec in split_codecs:
parts = full_codec.split('.') parts = re.sub(r'0+(?=\d)', '', full_codec).split('.')
codec = parts[0].replace('0', '') if parts[0] in ('avc1', 'avc2', 'avc3', 'avc4', 'vp9', 'vp8', 'hev1', 'hev2',
if codec in ('avc1', 'avc2', 'avc3', 'avc4', 'vp9', 'vp8', 'hev1', 'hev2', 'h263', 'h264', 'mp4v', 'hvc1', 'av1', 'theora', 'dvh1', 'dvhe'):
'h263', 'h264', 'mp4v', 'hvc1', 'av1', 'theora', 'dvh1', 'dvhe'): if vcodec:
if not vcodec: continue
vcodec = '.'.join(parts[:4]) if codec in ('vp9', 'av1', 'hvc1') else full_codec vcodec = full_codec
if codec in ('dvh1', 'dvhe'): if parts[0] in ('dvh1', 'dvhe'):
hdr = 'DV' hdr = 'DV'
elif codec == 'av1' and len(parts) > 3 and parts[3] == '10': elif parts[0] == 'av1' and traverse_obj(parts, 3) == '10':
hdr = 'HDR10' hdr = 'HDR10'
elif full_codec.replace('0', '').startswith('vp9.2'): elif parts[:2] == ['vp9', '2']:
hdr = 'HDR10' hdr = 'HDR10'
elif codec in ('flac', 'mp4a', 'opus', 'vorbis', 'mp3', 'aac', 'ac-3', 'ec-3', 'eac3', 'dtsc', 'dtse', 'dtsh', 'dtsl'): elif parts[0] in ('flac', 'mp4a', 'opus', 'vorbis', 'mp3', 'aac',
if not acodec: 'ac-3', 'ec-3', 'eac3', 'dtsc', 'dtse', 'dtsh', 'dtsl'):
acodec = full_codec acodec = acodec or full_codec
elif codec in ('stpp', 'wvtt',): elif parts[0] in ('stpp', 'wvtt'):
if not scodec: scodec = scodec or full_codec
scodec = full_codec
else: else:
write_string(f'WARNING: Unknown codec {full_codec}\n') write_string(f'WARNING: Unknown codec {full_codec}\n')
if vcodec or acodec or scodec: if vcodec or acodec or scodec: