Rename ffaudios.py -> ffstreams.py, generalize for subs too.

master
voussoir 2020-01-22 15:33:48 -08:00
parent dff9152898
commit a5db669139
1 changed files with 38 additions and 10 deletions

View File

@ -7,19 +7,26 @@ from voussoirkit import pathclass
from voussoirkit import winglob from voussoirkit import winglob
from voussoirkit import winwhich from voussoirkit import winwhich
EXTENSIONS = { AUDIO_EXTENSIONS = {
'aac': 'm4a', 'aac': 'm4a',
'mp3': 'mp3', 'mp3': 'mp3',
'opus': 'opus', 'opus': 'opus',
'*': 'mka',
} }
ffmpeg = winwhich.which('ffmpeg') SUBTITLE_EXTENSIONS = {
'ass': 'ass',
'subrip': 'srt',
'*': 'mkv',
}
def ffaudios(input_filename, moveto=None): FFMPEG = winwhich.which('ffmpeg')
def ffextractor(input_filename, prefix, search_pattern, extension_map, moveto=None):
input_filename = pathclass.Path(input_filename) input_filename = pathclass.Path(input_filename)
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_filename.absolute_path]
try: try:
output = subprocess.check_output(command, stderr=subprocess.STDOUT) output = subprocess.check_output(command, stderr=subprocess.STDOUT)
@ -29,32 +36,53 @@ def ffaudios(input_filename, moveto=None):
output = output.decode('utf-8') output = output.decode('utf-8')
maps = [] maps = []
for line in output.splitlines(): for line in output.splitlines():
match = re.search(r'Stream #0:(\d+)[^\s]*: Audio: (\w+)', line) match = re.search(search_pattern, line)
if match is None: if match is None:
continue continue
(stream_index, codec) = (match.group(1), match.group(2)) (stream_index, codec) = (match.group(1), match.group(2))
extension = EXTENSIONS.get(codec, 'mka') extension = extension_map.get(codec, extension_map['*'])
output_filename = input_filename.replace_extension('').add_extension(f'{stream_index}.{extension}') output_filename = input_filename.replace_extension('').add_extension(f'{prefix}{stream_index}.{extension}')
if moveto: if moveto:
output_filename = moveto.with_child(output_filename.basename) output_filename = moveto.with_child(output_filename.basename)
maps.extend(['-map', f'0:{stream_index}', '-c', 'copy', output_filename.absolute_path]) maps.extend(['-map', f'0:{stream_index}', '-c', 'copy', output_filename.absolute_path])
# print(output_filename)
command = [ffmpeg, '-i', input_filename.absolute_path, *maps] command = [FFMPEG, '-i', input_filename.absolute_path, *maps]
print(command) print(command)
subprocess.run(command, stderr=subprocess.STDOUT) subprocess.run(command, stderr=subprocess.STDOUT)
def ffaudios(input_filename, moveto=None):
ffextractor(
input_filename,
prefix='a',
search_pattern=r'Stream #0:(\d+)[^\s]*: Audio: (\w+)',
extension_map=AUDIO_EXTENSIONS,
moveto=moveto,
)
def ffsubtitles(input_filename, moveto=None):
ffextractor(
input_filename,
prefix='s',
search_pattern=r'Stream #0:(\d+)[^\s]*: Subtitle: (\w+)',
extension_map=SUBTITLE_EXTENSIONS,
moveto=moveto,
)
def ffaudios_argparse(args): def ffaudios_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):
ffaudios(input_filename, moveto=args.moveto) if args.audios:
ffaudios(input_filename, moveto=args.moveto)
if args.subtitles:
ffsubtitles(input_filename, moveto=args.moveto)
def main(argv): def main(argv):
parser = argparse.ArgumentParser(description=__doc__) parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('input_filename', nargs='+') parser.add_argument('input_filename', nargs='+')
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('--subtitles', dest='subtitles', action='store_true')
parser.set_defaults(func=ffaudios_argparse) parser.set_defaults(func=ffaudios_argparse)
args = parser.parse_args(argv) args = parser.parse_args(argv)