-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
332 lines (295 loc) · 8.75 KB
/
test.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import subprocess
import sys
import json
import os
import re
import glob
import pysubs2
import argparse
from dotenv import load_dotenv
load_dotenv()
from delocalizer import Delocalizer
LANGUAGES = ["eng", "spa"]
WORDS = {}
ERRORS = []
parser = argparse.ArgumentParser(description='Convert vobsub subtitles into srt format.')
parser.add_argument('--shift', dest='shift', type=float, action="store", default=0.0,
help='Shift time')
parser.add_argument('--j', dest='jfile', type=str, action="store", default=False,
help='JSON file')
parser.add_argument('--l', dest='language', type=str, action="store", default=False,
help='Language')
parser.add_argument('--f', dest='folder', type=str, action="store", default=False,
help='Folder')
parser.add_argument('--y', dest='yes', type=int, action="store", default=False,
help='Edit the first subtitle track')
def load_json(jname):
try:
global WORDS
print("Loading ", jname)
f = open(jname)
data = json.load(f)
WORDS = data
return True
except Exception as e:
return False
def testsubs(subfile):
try:
active = False
p = 0
subs = pysubs2.load(subfile,encoding='utf-8')
with open("test.txt", "w", encoding="utf-8") as f:
for k,line in enumerate(subs):
if not active:
if bool(re.match(r"{\\(.*)(}r$)", line.text, re.IGNORECASE)):
active = True
p += 1
# f.write(str(k)+": "+line.text+"\n")
# f.write(subs[k+1].text+"\n")
else:
if p == 1: #u
if bool(re.match(r"{\\(.*)(}u$)", line.text, re.IGNORECASE)):
p += 1
continue
else:
active = False
p = 0
elif p == 2: #b
if bool(re.match(r"{\\(.*)(}b$)", line.text, re.IGNORECASE)):
p += 1
continue
else:
active = False
p = 0
elif p == 3: #b
if bool(re.match(r"{\\(.*)(}b$)", line.text, re.IGNORECASE)):
p += 1
continue
else:
active = False
p = 0
elif p == 4: #e
if bool(re.match(r"{\\(.*)(}e$)", line.text, re.IGNORECASE)):
p += 1
continue
else:
active = False
p = 0
elif p == 5: #r
if bool(re.match(r"{\\(.*)(}r$)", line.text, re.IGNORECASE)):
p += 1
subs[k-p].text = subs[k-p].text[:-1] + "G"
subs[k-(p-1)].text = subs[k-(p-1)].text[:-1] + "o"
subs[k-(p-2)].text = subs[k-(p-1)].text[:-1] + "m"
subs[k-(p-3)].text = subs[k-(p-1)].text[:-1] + "u"
subs[k-(p-4)].text = ""
subs[k-(p-5)].text = ""
f.write(str(k)+": "+subs[k-(p-0)].text+"\n")
f.write(str(k)+": "+subs[k-(p-1)].text+"\n")
f.write(str(k)+": "+subs[k-(p-2)].text+"\n")
f.write(str(k)+": "+subs[k-(p-3)].text+"\n")
f.write(str(k)+": "+subs[k-(p-4)].text+"\n")
f.write(str(k)+": "+subs[k-(p-5)].text+"\n")
active = False
p = 0
else:
active = False
p = 0
except Exception as e:
print(e)
def checkCase(k, subs):
try:
if bool(re.match(r"{\\(.*)(}u$)", subs[k+1].text, re.IGNORECASE)):
if bool(re.match(r"{\\(.*)(}b$)", subs[k+2].text, re.IGNORECASE)):
if bool(re.match(r"{\\(.*)(}b$)", subs[k+3].text, re.IGNORECASE)):
if bool(re.match(r"{\\(.*)(}e$)", subs[k+4].text, re.IGNORECASE)):
if bool(re.match(r"{\\(.*)(}r$)", subs[k+5].text, re.IGNORECASE)):
return 1 # rubber
elif bool(re.match(r"{\\(.*)(}rub$)", subs[k+3].text, re.IGNORECASE)):
return 2 # r u b rub
elif bool(re.match(r"{\\(.*)(}e$)", subs[k+1].text, re.IGNORECASE)):
if bool(re.match(r"{\\(.*)(}r$)", subs[k+2].text, re.IGNORECASE)):
if bool(re.match(r"{\\(.*)(}ber$)", subs[k+3].text, re.IGNORECASE)):
return 3 # b e r ber
return 0
except Exception as e:
raise e
def modifyByCase(k, subs, case):
try:
if case == 1:
subs[k].text = subs[k].text[:-1] + "G"
subs[k+1].text = subs[k+1].text[:-1] + "o"
subs[k+2].text = ""
subs[k+3].text = subs[k+3].text[:-1] + "m"
subs[k+4].text = subs[k+4].text[:-1] + "u"
subs[k+5].text = ""
return 5
elif case == 2:
subs[k].text = subs[k].text[:-1] + "G"
subs[k+1].text = subs[k+1].text[:-1] + "o"
subs[k+2].text = ""
return 2
elif case == 3:
subs[k].text = subs[k].text[:-1] + "m"
subs[k+1].text = subs[k+1].text[:-1] + "u"
subs[k+2].text = ""
return 2
else:
return 0
except Exception as e:
print(e)
def modifySubsWAnim(subfile):
try:
subs = pysubs2.load(subfile,encoding='utf-8')
nfilename = "[Delocalized]"+subfile
active = False
case = 0
for nl, line in enumerate(subs):
if not active:
if bool(re.match(r"{\\(.*)(}r$)", line.text, re.IGNORECASE)) or bool(re.match(r"{\\(.*)(}b$)", line.text, re.IGNORECASE)):
case = checkCase(nl, subs)
if case > 0:
active = True
skips = modifyByCase(nl, subs, case)
continue
for k,v in WORDS.items():
if bool(re.match(r"{\\(.*)(}rub$|}ber)", line.text, re.IGNORECASE)):
nline = line.text
nline = re.sub(r"rub", "Go", nline, flags=re.I)
nline = re.sub(r"ber", "mu", nline, flags=re.I)
line.text = nline
break
else:
line.text = re.sub(k, v, line.text, flags=re.I)
else:
skips -= 1
if skips > 0:
continue
else:
active = False
case = 0
continue
subs.save(nfilename)
return nfilename
except Exception as e:
print(e)
return False
def modifySubs(subfile):
try:
subs = pysubs2.load(subfile,encoding='utf-8')
nfilename = "[Delocalized]"+subfile
for nl, line in enumerate(subs):
for k,v in WORDS.items():
line.text = re.sub(k, v, line.text, flags=re.I)
subs.save(nfilename)
return nfilename
except Exception as e:
print(e)
return False
def extract_subs(name, index, language="eng"):
try:
filename = os.path.splitext(name)[0]
subtitle = filename+".ass"
args = ["ffmpeg", "-loglevel", "quiet", "-i", name, "-map", "0:{}".format(index), "-c", "copy", subtitle]
rc = subprocess.Popen(args, shell=False)
rc.communicate()
return subtitle
except Exception as e:
print(e)
return False
def shift_subs(fname, delta):
subs1 = pysubs2.load(fname, encoding="utf-8")
subs1.shift(s=delta)
msub = "m_" + fname
subs1.save(msub)
return msub
def mux(streams, f, subtitle):
try:
sfiles = get_number_subs(streams)
filename = os.path.splitext(f)[0]
if os.path.isfile(subtitle):
newfilename = ".\\Finished\\"+filename + '.mkv'
args = ["ffmpeg", "-loglevel", "quiet", "-y", "-i", f, "-i", subtitle, "-c", "copy", "-map", "0", "-map", "1", "-metadata:s:s:{}".format(sfiles), "language=eng", "-metadata:s:s:{}".format(sfiles), "handler_name=English", "-metadata:s:s:{}".format(sfiles), "title=Unlocalized", "-max_interleave_delta", "0", "-disposition:s:0", "0", "-disposition:s:{}".format(sfiles), "default", newfilename]
rc = subprocess.Popen(args, shell=False)
rc.communicate()
return True
except Exception as e:
return False
def get_streams(fname):
info = json.loads(subprocess.check_output(["ffprobe", "-v", "quiet", "-print_format", "json", "-show_streams", fname]))
return info
def get_language_index(streams, language):
try:
index = -1
f_sub = -1
for i in streams["streams"]:
if i["codec_type"] == "subtitle":
if f_sub == -1:
f_sub = i["index"]
if "language" in i["tags"].keys():
if i["tags"]["language"] == language:
print(i["tags"]["language"], "|", i["index"])
index = i["index"]
return index
if f_sub > -1:
return f_sub
else:
return -1
except Exception as e:
print(e)
return -1
def get_number_subs(streams):
try:
subs = 0
for i in streams["streams"]:
if i["codec_type"] == "subtitle":
subs += 1
return subs
except Exception as e:
print(e)
return 0
def demux(language):
global ERRORS
files = glob.glob('*.mkv')
index = -1
for f in files:
print("Extracting: ", f)
streams = get_streams(f)
index = get_language_index(streams, language)
if index > -1:
print("Subtitles found at", index)
subfile = extract_subs(f, index, language)
if subfile:
print("Delocalizing...")
unloc_sub = modifySubs(subfile)
if unloc_sub:
os.remove(subfile)
print("Muxxing with file:", unloc_sub)
r = mux(streams, f, unloc_sub)
if r:
os.remove(unloc_sub)
else:
print("Failed to mux sub!")
ERRORS.append(str(f))
else:
print("Failed to modify subs!")
ERRORS.append(str(f))
else:
print("Failed to extract Subtitle!")
ERRORS.append(str(f))
else:
print("Subtitles not found!")
ERRORS.append(str(f))
def main():
args = parser.parse_args()
delocalizer = Delocalizer()
s = delocalizer.prepare_data(args)
if s:
delocalizer.delocalize()
def test():
testsubs("One.Piece.E796-E798.720p.ass")
if __name__ == '__main__':
main()
#print(mkvmerge)
#f = '.\\Files\\Getsuyoubi no Tawawa 2 - 02.mkv'
#print(info['tracks'])