2020-01-16 05:26:16 +00:00
|
|
|
import argparse
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from voussoirkit import pathclass
|
2020-01-16 06:33:44 +00:00
|
|
|
from voussoirkit import winglob
|
2020-01-16 05:26:16 +00:00
|
|
|
from voussoirkit import winwhich
|
|
|
|
|
2020-01-22 23:33:48 +00:00
|
|
|
AUDIO_EXTENSIONS = {
|
2020-01-16 05:26:16 +00:00
|
|
|
'aac': 'm4a',
|
|
|
|
'mp3': 'mp3',
|
|
|
|
'opus': 'opus',
|
2020-01-22 23:33:48 +00:00
|
|
|
'*': 'mka',
|
2020-01-16 05:26:16 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 23:33:48 +00:00
|
|
|
SUBTITLE_EXTENSIONS = {
|
|
|
|
'ass': 'ass',
|
|
|
|
'subrip': 'srt',
|
2020-01-22 23:48:50 +00:00
|
|
|
'*': 'mks',
|
2020-01-22 23:33:48 +00:00
|
|
|
}
|
2020-01-16 05:26:16 +00:00
|
|
|
|
2020-01-22 23:33:48 +00:00
|
|
|
FFMPEG = winwhich.which('ffmpeg')
|
|
|
|
|
|
|
|
def ffextractor(input_filename, prefix, search_pattern, extension_map, moveto=None):
|
2020-01-16 05:26:16 +00:00
|
|
|
input_filename = pathclass.Path(input_filename)
|
2020-01-22 23:49:06 +00:00
|
|
|
|
2020-01-16 05:26:16 +00:00
|
|
|
if moveto is not None:
|
|
|
|
moveto = pathclass.Path(moveto)
|
2020-01-22 23:49:06 +00:00
|
|
|
|
2020-01-22 23:33:48 +00:00
|
|
|
command = [FFMPEG, '-i', input_filename.absolute_path]
|
2020-01-16 05:26:16 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
|
|
|
|
except subprocess.CalledProcessError as exc:
|
|
|
|
output = exc.output
|
|
|
|
output = output.decode('utf-8')
|
2020-01-22 23:49:06 +00:00
|
|
|
|
2020-01-16 05:26:16 +00:00
|
|
|
maps = []
|
|
|
|
for line in output.splitlines():
|
2020-01-22 23:33:48 +00:00
|
|
|
match = re.search(search_pattern, line)
|
2020-01-16 05:26:16 +00:00
|
|
|
if match is None:
|
|
|
|
continue
|
2020-01-22 23:49:06 +00:00
|
|
|
|
2020-01-16 05:26:16 +00:00
|
|
|
(stream_index, codec) = (match.group(1), match.group(2))
|
2020-01-22 23:33:48 +00:00
|
|
|
extension = extension_map.get(codec, extension_map['*'])
|
2020-01-22 23:49:06 +00:00
|
|
|
output_filename = input_filename.replace_extension('')
|
|
|
|
output_filename = output_filename.add_extension(f'{prefix}{stream_index}.{extension}')
|
|
|
|
|
2020-01-16 05:26:16 +00:00
|
|
|
if moveto:
|
|
|
|
output_filename = moveto.with_child(output_filename.basename)
|
2020-01-22 23:48:50 +00:00
|
|
|
|
|
|
|
args = ['-map', f'0:{stream_index}', '-c', 'copy']
|
|
|
|
|
|
|
|
if extension == 'mks':
|
|
|
|
args.extend(['-f', 'matroska'])
|
|
|
|
|
|
|
|
args.append(output_filename.absolute_path)
|
|
|
|
maps.extend(args)
|
2020-01-16 05:26:16 +00:00
|
|
|
|
2020-01-22 23:33:48 +00:00
|
|
|
command = [FFMPEG, '-i', input_filename.absolute_path, *maps]
|
2020-01-16 05:26:16 +00:00
|
|
|
print(command)
|
|
|
|
subprocess.run(command, stderr=subprocess.STDOUT)
|
|
|
|
|
2020-01-22 23:33:48 +00:00
|
|
|
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,
|
|
|
|
)
|
2020-01-16 05:26:16 +00:00
|
|
|
|
2020-01-22 23:49:06 +00:00
|
|
|
def ffstreams_argparse(args):
|
2020-01-16 05:26:16 +00:00
|
|
|
for pattern in args.input_filename:
|
2020-01-16 06:33:44 +00:00
|
|
|
for input_filename in winglob.glob(pattern):
|
2020-01-22 23:33:48 +00:00
|
|
|
if args.audios:
|
|
|
|
ffaudios(input_filename, moveto=args.moveto)
|
|
|
|
if args.subtitles:
|
|
|
|
ffsubtitles(input_filename, moveto=args.moveto)
|
2020-01-16 05:26:16 +00:00
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
|
|
|
|
parser.add_argument('input_filename', nargs='+')
|
|
|
|
parser.add_argument('--moveto', dest='moveto', default=None)
|
2020-01-22 23:33:48 +00:00
|
|
|
parser.add_argument('--audio', '--audios', dest='audios', action='store_true')
|
2020-01-27 00:24:55 +00:00
|
|
|
parser.add_argument('--subtitles', '--subs', dest='subtitles', action='store_true')
|
2020-01-22 23:49:06 +00:00
|
|
|
parser.set_defaults(func=ffstreams_argparse)
|
2020-01-16 05:26:16 +00:00
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
return args.func(args)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|