-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffstreams.py
155 lines (126 loc) · 4.19 KB
/
ffstreams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import argparse
import re
import subprocess
import sys
from voussoirkit import pathclass
from voussoirkit import winwhich
from voussoirkit import vlogging
log = vlogging.getLogger(__name__, 'ffstreams')
AUDIO_EXTENSIONS = {
'aac': 'm4a',
'ac3': 'ac3',
'flac': 'flac',
'mp3': 'mp3',
'opus': 'opus',
'vorbis': 'ogg',
'*': 'mka',
}
SUBTITLE_EXTENSIONS = {
'ass': 'ass',
'subrip': 'srt',
'*': 'mks',
}
FFMPEG = winwhich.which('ffmpeg')
def make_maps(input_file, prefix, search_pattern, extension_map, moveto=None):
input_file = pathclass.Path(input_file)
if moveto is not None:
moveto = pathclass.Path(moveto)
command = [FFMPEG, '-i', input_file.absolute_path]
try:
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
output = exc.output
output = output.decode('utf-8')
maps = []
for line in output.splitlines():
match = re.search(search_pattern, line)
if match is None:
continue
(stream_index, language, codec) = match.groups()
if language is None:
language = ''
else:
language = language.strip('()')
language = '.' + language
extension = extension_map.get(codec, extension_map['*'])
output_filename = input_file.replace_extension('')
output_filename = output_filename.add_extension(f'{prefix}{stream_index}{language}.{extension}')
log.debug(f'{stream_index}, codec={codec}, ext=.{extension}')
if moveto:
output_filename = moveto.with_child(output_filename.basename)
args = ['-map', f'0:{stream_index}', '-c', 'copy']
if extension == 'mks':
args.extend(['-f', 'matroska'])
args.append(output_filename.absolute_path)
maps.extend(args)
return maps
def video_maps(input_file, moveto=None):
return make_maps(
input_file,
prefix='v',
search_pattern=r'Stream #0:(\d+)(\(\w+\))?[^\s]*: Video: (\w+)',
extension_map=AUDIO_EXTENSIONS,
moveto=moveto,
)
def audio_maps(input_file, moveto=None):
return make_maps(
input_file,
prefix='a',
search_pattern=r'Stream #0:(\d+)(\(\w+\))?[^\s]*: Audio: (\w+)',
extension_map=AUDIO_EXTENSIONS,
moveto=moveto,
)
def subtitle_maps(input_file, moveto=None):
return make_maps(
input_file,
prefix='s',
search_pattern=r'Stream #0:(\d+)(\(\w+\))?[^\s]*: Subtitle: (\w+)',
extension_map=SUBTITLE_EXTENSIONS,
moveto=moveto,
)
def ffstreams(
input_file,
do_videos=False,
do_audios=False,
do_subtitles=False,
dry=False,
moveto=None,
):
maps = []
if do_videos:
maps.extend(video_maps(input_file, moveto=moveto))
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]
log.info(command)
if not dry:
subprocess.run(command, stderr=subprocess.STDOUT)
def ffstreams_argparse(args):
for input_file in pathclass.glob_many(args.input_filename):
ffstreams(
input_file,
do_videos=args.videos,
do_audios=args.audios,
do_subtitles=args.subtitles,
dry=args.dry,
moveto=args.moveto,
)
return 0
@vlogging.main_decorator
def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('input_filename', nargs='+')
parser.add_argument('--moveto', default=None)
parser.add_argument('--video', '--videos', dest='videos', 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('--dry', dest='dry', action='store_true')
parser.set_defaults(func=ffstreams_argparse)
args = parser.parse_args(argv)
return args.func(args)
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))