Refactor to perform both audios and subs in a single command.

This commit is contained in:
voussoir 2020-02-19 15:19:30 -08:00
parent 651861b8b4
commit 57fd5955e1

View file

@ -24,13 +24,13 @@ SUBTITLE_EXTENSIONS = {
FFMPEG = winwhich.which('ffmpeg') FFMPEG = winwhich.which('ffmpeg')
def ffextractor(input_filename, prefix, search_pattern, extension_map, moveto=None): def make_maps(input_file, prefix, search_pattern, extension_map, moveto=None):
input_filename = pathclass.Path(input_filename) input_file = pathclass.Path(input_file)
if moveto is not None: if moveto is not None:
moveto = pathclass.Path(moveto) moveto = pathclass.Path(moveto)
command = [FFMPEG, '-i', input_filename.absolute_path] command = [FFMPEG, '-i', input_file.absolute_path]
try: try:
output = subprocess.check_output(command, stderr=subprocess.STDOUT) output = subprocess.check_output(command, stderr=subprocess.STDOUT)
@ -46,8 +46,9 @@ def ffextractor(input_filename, prefix, search_pattern, extension_map, moveto=No
(stream_index, codec) = (match.group(1), match.group(2)) (stream_index, codec) = (match.group(1), match.group(2))
extension = extension_map.get(codec, extension_map['*']) extension = extension_map.get(codec, extension_map['*'])
output_filename = input_filename.replace_extension('') output_filename = input_file.replace_extension('')
output_filename = output_filename.add_extension(f'{prefix}{stream_index}.{extension}') output_filename = output_filename.add_extension(f'{prefix}{stream_index}.{extension}')
print(f'{stream_index}, codec={codec}, ext=.{extension}')
if moveto: if moveto:
output_filename = moveto.with_child(output_filename.basename) output_filename = moveto.with_child(output_filename.basename)
@ -59,36 +60,53 @@ def ffextractor(input_filename, prefix, search_pattern, extension_map, moveto=No
args.append(output_filename.absolute_path) args.append(output_filename.absolute_path)
maps.extend(args) maps.extend(args)
return maps
command = [FFMPEG, '-i', input_filename.absolute_path, *maps] def audio_maps(input_file, moveto=None):
print(command) return make_maps(
subprocess.run(command, stderr=subprocess.STDOUT) input_file,
def ffaudios(input_filename, moveto=None):
ffextractor(
input_filename,
prefix='a', prefix='a',
search_pattern=r'Stream #0:(\d+)[^\s]*: Audio: (\w+)', search_pattern=r'Stream #0:(\d+)[^\s]*: Audio: (\w+)',
extension_map=AUDIO_EXTENSIONS, extension_map=AUDIO_EXTENSIONS,
moveto=moveto, moveto=moveto,
) )
def ffsubtitles(input_filename, moveto=None): def subtitle_maps(input_file, moveto=None):
ffextractor( return make_maps(
input_filename, input_file,
prefix='s', prefix='s',
search_pattern=r'Stream #0:(\d+)[^\s]*: Subtitle: (\w+)', search_pattern=r'Stream #0:(\d+)[^\s]*: Subtitle: (\w+)',
extension_map=SUBTITLE_EXTENSIONS, extension_map=SUBTITLE_EXTENSIONS,
moveto=moveto, moveto=moveto,
) )
def ffstreams(input_file, do_audios=False, do_subtitles=False, dry=False, moveto=None):
maps = []
if do_audios:
maps.extend(audio_maps(input_file, moveto=moveto))
if do_subtitles:
maps.extend(subtitle_maps(input_file, moveto=moveto))
if not maps:
return
command = [FFMPEG, '-i', input_file.absolute_path, *maps]
print(command)
if not dry:
subprocess.run(command, stderr=subprocess.STDOUT)
def ffstreams_argparse(args): def ffstreams_argparse(args):
for pattern in args.input_filename: for pattern in args.input_filename:
for input_filename in winglob.glob(pattern): for input_filename in winglob.glob(pattern):
if args.audios: input_file = pathclass.Path(input_filename)
ffaudios(input_filename, moveto=args.moveto) ffstreams(
if args.subtitles: input_file,
ffsubtitles(input_filename, moveto=args.moveto) do_audios=args.audios,
do_subtitles=args.subtitles,
dry=args.dry,
moveto=args.moveto,
)
def main(argv): def main(argv):
parser = argparse.ArgumentParser(description=__doc__) parser = argparse.ArgumentParser(description=__doc__)
@ -97,6 +115,7 @@ def main(argv):
parser.add_argument('--moveto', dest='moveto', default=None) parser.add_argument('--moveto', dest='moveto', default=None)
parser.add_argument('--audio', '--audios', dest='audios', action='store_true') parser.add_argument('--audio', '--audios', dest='audios', action='store_true')
parser.add_argument('--subtitles', '--subs', dest='subtitles', action='store_true') parser.add_argument('--subtitles', '--subs', dest='subtitles', action='store_true')
parser.add_argument('--dry', dest='dry', action='store_true')
parser.set_defaults(func=ffstreams_argparse) parser.set_defaults(func=ffstreams_argparse)
args = parser.parse_args(argv) args = parser.parse_args(argv)