Skip to content

Commit 3d6f9bc

Browse files
committed
rewrite video and audio codec detection with regex lists
1 parent dddc73b commit 3d6f9bc

File tree

1 file changed

+58
-44
lines changed

1 file changed

+58
-44
lines changed

pythonbits/bb.py

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -457,62 +457,76 @@ def _render_container(self):
457457

458458
def _render_video_codec(self):
459459
video_track = self['tracks']['video']
460-
# norm_bitrate = (float(bit_rate) /
461-
# (video_track.width*video_track.height))
462-
if (video_track['codec_id'] in ('V_MPEG4/ISO/AVC', 'AVC', 'avc1') or
463-
video_track['format'] == 'AVC'):
464-
if 'x264' in video_track.get('writing_library', ''):
465-
return 'x264'
466-
else:
467-
return 'H.264'
468-
elif video_track['codec_id'] in ('V_MPEGH/ISO/HEVC', 'HEVC'):
469-
if 'x265' in video_track.get('writing_library', ''):
470-
return 'x265'
471-
else:
472-
return 'H.265'
473-
elif video_track['codec_id'] in ('V_MS/VFW/FOURCC / WVC1',):
474-
return 'VC-1'
475-
elif 'VP9' in video_track['codec_id']:
476-
return 'VP9'
477-
elif video_track['codec_id'] == 'XVID':
478-
return 'XVid'
479-
elif video_track['codec_id'] == 'MP42':
480-
return 'DivX'
481-
elif video_track['format'] == 'MPEG Video':
482-
return 'MPEG-2'
460+
codec_id = video_track['codec_id']
461+
462+
match_list = [('(V_MPEG4/ISO/)?AVC1?', 'H.264'),
463+
('(V_MPEGH/ISO/)?HEVC', 'H.265'),
464+
('(V_MS/VFW/FOURCC / )?WVC1', 'VC-1'),
465+
('VP9', 'VP9'),
466+
('XVID', 'XVid'),
467+
('(MP42|DX[45]0)', 'DivX'),
468+
('(V_)?MPEG2', 'MPEG-2')
469+
]
470+
471+
norm_codec_id = None
472+
for rx, rv in match_list:
473+
rx = re.compile(rx, flags=re.IGNORECASE)
474+
if rx.match(codec_id):
475+
norm_codec_id = rv
476+
break
483477
else:
484-
msg = "Unknown or unsupported video codec '{}' ({})".format(
485-
video_track.get('codec_id'),
486-
video_track.get('writing_library'))
487-
raise RuntimeError(msg)
478+
if video_track['format'] == 'MPEG Video':
479+
if video_track['format_version'] == 'Version 1':
480+
norm_codec_id = 'MPEG-1'
481+
elif video_track['format_version'] == 'Version 2':
482+
norm_codec_id = 'MPEG-2'
483+
elif video_track['format'] == 'AVC':
484+
norm_codec_id = 'H.264'
485+
486+
# x264/5 is not a codec, but the rules is the rules
487+
if (norm_codec_id == 'H.264' and
488+
'x264' in video_track.get('writing_library', '')):
489+
return 'x264'
490+
elif (norm_codec_id == 'H.265' and
491+
'x265' in video_track.get('writing_library', '')):
492+
return 'x265'
493+
elif norm_codec_id:
494+
return norm_codec_id
495+
496+
msg = "Unknown or unsupported video codec '{}' ({}, {})".format(
497+
video_track.get('codec_id'),
498+
video_track.get('format'),
499+
video_track.get('writing_library'))
500+
raise RuntimeError(msg)
488501

489502
def _render_audio_codec(self):
490503
audio_track = self['tracks']['audio'][0] # main audio track
491-
if (audio_track.get('codec_id_hint') == 'MP3' or
492-
audio_track['codec_id'] in ('MPA1L3', '55')):
504+
if audio_track.get('codec_id_hint') == 'MP3':
493505
return 'MP3'
494-
elif audio_track['codec_id'].lower().startswith('mp4a'):
495-
return 'AAC'
496506
elif 'Dolby Atmos' in audio_track['commercial_name']:
497507
return 'Dolby Atmos'
498508
elif 'DTS-HD' in audio_track['commercial_name']:
499509
if audio_track.get('other_format', '') == 'DTS XLL X':
500510
return 'DTS:X'
501511
return 'DTS-HD'
502512

503-
if audio_track['codec_id'].startswith('A_'):
504-
audio_track['codec_id'] = audio_track['codec_id'][2:]
505-
# Could be "ac-3"
506-
audio_track['codec_id'] = audio_track['codec_id'].upper()
507-
audio_codecs = ('AC3', 'AC-3', 'EAC3', 'DTS', 'FLAC', 'AAC', 'MP3',
508-
'TRUEHD', 'PCM', '2000')
509-
for c in audio_codecs:
510-
if audio_track['codec_id'].startswith(c):
511-
c = c.replace('EAC3', 'AC-3') \
512-
.replace('AC3', 'AC-3') \
513-
.replace('2000', 'AC-3') \
514-
.replace('TRUEHD', 'True-HD')
515-
return c
513+
codec_id = audio_track['codec_id']
514+
if codec_id.startswith('A_'):
515+
codec_id = codec_id[2:]
516+
517+
match_list = [('(E?AC-?3|2000)', 'AC-3'),
518+
('DTS', 'DTS'),
519+
('FLAC', 'FLAC'),
520+
('(AAC|MP4A)', 'AAC'),
521+
('(MP3|MPA1L3|55)', 'MP3'),
522+
('TRUEHD', 'True-HD'),
523+
('PCM', 'PCM'),
524+
]
525+
526+
for rx, rv in match_list:
527+
rx = re.compile(rx, flags=re.IGNORECASE)
528+
if rx.match(codec_id):
529+
return rv
516530

517531
raise ValueError("Unknown or unsupported audio codec '{}'".format(
518532
audio_track['codec_id']))

0 commit comments

Comments
 (0)