-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrarfix.py
executable file
·145 lines (116 loc) · 3.25 KB
/
rarfix.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
#!/usr/bin/env python3
import sys
import os
import re
import subprocess
import collections
import json
from typing import List
SUBFILE_REGEX = re.compile(r'^(\d+)_(\w+)\.[sS][rR][tT]$')
LANGS = {
'english': 'eng',
'french': 'fre',
'bulgarian': 'bul',
'chinese': 'chi',
'arabic': 'ara',
'czech': 'cze',
'danish': 'dan',
'dutch': 'dut',
'finnish': 'fin',
'greek': 'gre',
'spanish': 'spa',
'hungarian': 'hun',
'swedish': 'swe',
'slovenian': 'slv',
'polish': 'pol',
'vietnamese': 'vie',
'romanian': 'rum',
'bokmal': 'nob',
'portuguese': 'por',
'korean': 'kor',
'german': 'ger',
'russian': 'rus',
'italian': 'ita',
'turkish': 'tur',
'estonian': 'est',
'indonesian': 'ind',
'hebrew': 'heb',
'japanese': 'jpn',
}
DESTDIR = "/media/Collection1/Movies"
subinfo = collections.namedtuple('subinfo', ['num', 'langcode', 'file'])
if len(sys.argv) != 2:
print(f'Usage: {sys.argv[0]} <movie-path>')
exit(2)
srcdir = sys.argv[1]
subdir = os.path.join(srcdir, 'Subs')
def _find_subs(subdir: str, mbase: str) -> List[subinfo]:
subs = []
for root, dirs, files in os.walk(subdir):
for s in files:
##
# Handle dvdsub, just assume English.
##
if s == f'{mbase}.idx':
subs.append(subinfo(num=0, langcode='eng', file=os.path.join(root, s)))
continue
m = SUBFILE_REGEX.fullmatch(s)
if not m:
print(f'WARNING: non-matching subtitle file {root}/{s}')
continue
num = int(m[1])
lang = m[2].lower()
path = os.path.join(root, s)
if len(lang) != 3:
langcode = LANGS.get(lang, 'und')
else:
langcode = lang
if langcode == 'und' and lang != 'und':
print(f'WARNING: unknown language {lang}, marking as unknown')
subs.append(subinfo(num=num, langcode=langcode, file=path))
break
subs.sort(key=lambda x: x.num)
return subs
mbase = os.path.basename(srcdir)
mfile = os.path.join(srcdir, f'{mbase}.mp4')
streaminfo = subprocess.run(
['ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_streams', mfile],
check=True,
capture_output=True
)
nsub = 0
for st in json.loads(streaminfo.stdout)['streams']:
if st['codec_type'] == 'subtitle':
nsub += 1
if nsub != 0:
print('File already has subtitles...')
exit(1)
subs = _find_subs(subdir, mbase)
# for s in subs:
# print(s)
# exit(0)
outdir = os.path.join(DESTDIR, mbase)
os.makedirs(outdir, 0o755, exist_ok=True)
ffargs = ['ffmpeg', '-y', '-i', mfile]
mapargs = ['-map', '0:v']
metaargs = []
# subtitle start index
suboffset = 1
i = 0
for s in subs:
ffargs += ['-i', s.file]
mapargs += ['-map', f'{i + suboffset}:s']
metaargs.append(f'-metadata:s:s:{i}')
metaargs.append(f'language={s.langcode}')
i += 1
mapargs += ['-map', '0:a']
ffargs += mapargs
ffargs += ['-c:v', 'copy']
if len(subs) > 0:
ffargs += ['-c:s', 'copy']
ffargs += ['-c:a', 'copy']
ffargs += metaargs
ffargs += ['-max_interleave_delta', '0']
ffargs.append(os.path.join(outdir, f'{mbase}.mkv'))
#print(ffargs)
subprocess.run(ffargs, check=True)