-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsub_merger.py
515 lines (438 loc) · 19.8 KB
/
sub_merger.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/usr/bin/env python2
# author: Christopher Slycord
from __future__ import print_function
import re
import sys
import os
from my_subtitle import MySubtitle
from smi2srt import SMI2SRT
try:
import cchardet
except ImportError:
print('''cchardet python package not found. Please install it using
macports or pip''')
sys.exit()
try:
import chardet
except ImportError:
print('''chardet python package not found. Please install it using
macports or pip''')
sys.exit()
TIME_PATTERN = (r'\d{1,2}:\d{1,2}:\d{1,2},\d{1,5} --> '
r'\d{1,2}:\d{1,2}:\d{1,2},\d{1,5}\r\n')
class Merger():
"""
SRT Merger allows you to merge subtitle files, no matter what language
are the subtitles encoded in. The result of this merge will be a new
subtitle file which will display subtitles from each merged file.
"""
def __init__(self,
output_file='subtitle_name.srt',
output_encoding='utf-8'):
self.lines = []
self.remove_rows = list()
dirpath = os.path.dirname(output_file)
self.output_path = dirpath if dirpath != '' else '.'
self.output_name = os.path.basename(output_file)
self.output_encoding = output_encoding
# self.subtitles will be a list of all subs
self.subtitles = list()
def get_milliseconds(self, timestr, File, SubNumber):
if len(timestr) != 12:
print(File + " has an invalid timecode for an SRT file.")
print("Have a look at subtitle number " + str(SubNumber))
print("Exiting!")
sys.exit(1)
HRS = int(timestr[0:2])
MINS = int(timestr[3:5])
SECS = int(timestr[6:8])
MIL_SECS = int(timestr[9:12])
HRS_MS = int(3600000)
MINS_MS = int(60000)
SECS_MS = int(1000)
return(HRS*HRS_MS+MINS*MINS_MS+SECS*SECS_MS+MIL_SECS)
def _findOverlapping(self, CheckSubtitle):
# returns the maximum position in the list of timestamps that are lower
# than the one we are checking. Avoids needless looping
first = 0
last = len(self.subtitles)-1
if (last < first):
return first
if CheckSubtitle <= self.subtitles[first]:
return first
if CheckSubtitle >= self.subtitles[last]:
return last
def do_search(Subtitle, low, high):
if Subtitle >= self.subtitles[high]:
return high
if Subtitle <= self.subtitles[low]:
return low
if low >= high:
return high
middle = (low + high) // 2
middle_sub = self.subtitles[middle]
if middle_sub == Subtitle:
return middle
if middle_sub > Subtitle:
return do_search(Subtitle, low, middle)
return do_search(Subtitle, middle+1, high)
position = do_search(CheckSubtitle, first, last)
# position now has the position in the list that has the next highest
# start time.
# But the new subtitle to be inserted might start before the previous
# subtitle ended
keep_looking = True
while position != first and keep_looking:
if CheckSubtitle.start() < self.subtitles[position-1].end():
position -= 1
else:
keep_looking = False
return position
def _add_lines(self, ListOfLines, position, remove=True):
end = len(self.subtitles)
if position > end:
self.subtitles += ListOfLines
else:
if not remove:
if position == end:
self.subtitles += ListOfLines
else:
self.subtitles = self.subtitles[0:position] + \
ListOfLines + self.subtitles[position:]
else:
self.subtitles = self.subtitles[0:position] + \
ListOfLines + self.subtitles[position+1:]
def _check_times(self,
CheckSubtitle,
SubNumber,
FirstFile=True):
# timestamp = self.ms2TS(start_time) + " --> " + self.ms2TS(end_time)
start_time = CheckSubtitle.start()
end_time = CheckSubtitle.end()
line_of_text = CheckSubtitle.data()
# set original variables
list_start = int(-1)
list_end = int(-1)
list_line = ""
finished_adding = False
while not finished_adding:
i = self._findOverlapping(CheckSubtitle)
# file = "First" if FirstFile == True else "Second"
# print(file + "- Line Number:" + str(SubNumber))
if len(self.subtitles) > 0:
CurrentSubtitle = self.subtitles[i]
list_start = CurrentSubtitle.start()
list_end = CurrentSubtitle.end()
list_line = CurrentSubtitle.data()
added_rows = list()
# check for overlaps
# starts before
if start_time < list_start:
# 1) starts & ends before (or at) Current ending position
# start_time end_time list_start list_end
# start_time end_time/list_start list_end
if end_time <= list_start:
new_row = MySubtitle(start_time,
end_time,
line_of_text)
added_rows.append(new_row)
self._add_lines(added_rows, i, remove=False)
finished_adding = True
break
else:
# all of these start before and end sometime after
# the current one starts
# FIRST Add the stuff that occurs early
new_row = MySubtitle(start_time,
list_start,
line_of_text)
added_rows.append(new_row)
# 2) starts before & ends between
# start_time list_start end_time list_end
if end_time < list_end:
new_row = MySubtitle(list_start,
end_time,
list_line+'\n'+line_of_text)
added_rows.append(new_row)
new_row = MySubtitle(end_time,
list_end,
list_line)
added_rows.append(new_row)
self._add_lines(added_rows, i, remove=True)
finished_adding = True
break
# 3) starts before & ends same
# start_time list_start list_end
# end_time
if end_time == list_end:
new_row = MySubtitle(list_start,
end_time,
list_line+'\n'+line_of_text)
added_rows.append(new_row)
self._add_lines(added_rows, i, remove=True)
finished_adding = True
break
# 4 starts before & ends after
# start_time list_start list_end end_time
if end_time > list_end:
new_row = MySubtitle(list_start,
list_end,
list_line+'\n'+line_of_text)
added_rows.append(new_row)
self._add_lines(added_rows, i, remove=True)
# update start time to be able to continue checking
start_time = list_end
finished_adding = False
else:
# either starts at same or after (start_time >= list_start)
# Starts at same
if start_time == list_start:
if end_time >= list_end:
new_row = MySubtitle(list_start,
list_end,
list_line+'\n'+line_of_text)
added_rows.append(new_row)
self._add_lines(added_rows, i, remove=True)
if end_time == list_end:
# starts same; ends same
# list_start list_end
# start_time end_time
finished_adding = True
break
else:
# starts same; ends after
# list_start list_end end_time
# start_time
start_time = list_end
finished_adding = False
else:
# starts same; ends before
# list_start end_time list_end
# start_time
new_row = MySubtitle(list_start,
end_time,
list_line+'\n'+line_of_text)
added_rows.append(new_row)
new_row = MySubtitle(end_time,
list_end,
list_line)
added_rows.append(new_row)
self._add_lines(added_rows, i, remove=True)
finished_adding = True
break
else:
# starts after this one or between
if start_time >= list_end:
# starts after; so continue looking
break
# starts between
else:
# needed for all that are between
new_row = MySubtitle(list_start,
start_time,
list_line)
added_rows.append(new_row)
if end_time < list_end:
# starts & ends between
# list_start start_time end_time list_end
new_row = MySubtitle(start_time,
end_time,
list_line+'\n'+line_of_text)
added_rows.append(new_row)
new_row = MySubtitle(end_time,
list_end,
list_line)
added_rows.append(new_row)
self._add_lines(added_rows, i, remove=True)
finished_adding = True
break
else:
# if end_time >= list_end
new_row = MySubtitle(start_time,
list_end,
list_line+'\n'+line_of_text)
added_rows.append(new_row)
self._add_lines(added_rows, i, remove=True)
if end_time == list_end:
# starts between ends same
# list_start start_time list_end
# end_time
# added above
finished_adding = True
break
if end_time > list_end:
# starts between ends after
# list_start start_time list_end end_time
# timestamps added above
start_time = list_end
finished_adding = False
if not finished_adding:
CheckSubtitle = MySubtitle(start_time,
end_time,
line_of_text)
if not finished_adding:
# Looks like this goes after all the other time stamps
new_row = MySubtitle(start_time,
end_time,
line_of_text)
added_rows.append(new_row)
self._add_lines(added_rows, len(self.subtitles), remove=False)
def _split_dialogs(self,
dialogs,
subtitle,
FirstFile=True):
for dialog in dialogs:
if dialog.startswith('\r\n'):
dialog = dialog.replace('\r\n', '', 1)
if dialog.startswith('\n'):
dialog = dialog[1:]
if (
dialog == '' or
dialog == '\n' or
dialog.rstrip().lstrip() == ''):
continue
try:
if dialog.startswith('\r\n'):
dialog = dialog[2:]
SubNumber = dialog.split('\n', 2)[0]
StartTime = dialog.split('\n', 2)[1].split('-->')[0].rstrip()
StartTime = int(self.get_milliseconds(StartTime,
subtitle['address'],
SubNumber))
EndTime = dialog.split('\n', 2)[1].split('-->')[1].lstrip()
EndTime = int(self.get_milliseconds(EndTime,
subtitle['address'],
SubNumber))
except Exception as e:
continue
texts = dialog.split('\n', 1)[1].split('\n')[1:]
Text = ""
for t in texts:
t = t.lstrip()
Text += t + '\n'
if Text == '' or Text == '\n':
continue
Text = Text.strip('\n')
sub = MySubtitle(StartTime, EndTime, Text)
self._check_times(sub, SubNumber, FirstFile)
def _encode(self, text):
codec = self.output_encoding
try:
return bytes(text, encoding=codec)
except Exception as e:
print('Problem in "%s" to encoing by %s. \nError: %s'
% (repr(text), codec, e))
return (b'An error has been occured in encoing by specifed '
b'`output_encoding`')
def add(self, top_subtitle_address, bottom_subtitle_address):
windows_line_ending = '\r\n'
linux_line_ending = '\n'
top_subtitle = {
'address': top_subtitle_address,
'dialogs': []
}
bottom_subtitle = {
'address': bottom_subtitle_address,
'dialogs': []
}
if top_subtitle_address.lower().endswith('.smi'):
SMI = SMI2SRT(smi=top_subtitle_address,
encoding=self.output_encoding)
converted_smi = SMI.convert_smi(outside=True)
for i in range(len(converted_smi)):
self._check_times(converted_smi[i].MySub, i+1, FirstFile=True)
# self._add_lines(SMI.convert_smi(outside=True),
# len(self.subtitles),
# remove=False)
# easy_srt = SMI.analysis_srt()
else:
with open(top_subtitle_address, 'rb') as file:
data = file.read()
cchdt = cchardet.detect(data)
codec = cchdt['encoding']
if cchdt['confidence'] < 0.99:
chdt = chardet.detect(data)
if chdt['confidence'] > cchdt['confidence']:
codec = chdt['encoding']
data = data.decode(codec).replace(windows_line_ending,
linux_line_ending)
dialogs = re.split('\r\n\r|\n\n', data)
top_subtitle['data'] = data
top_subtitle['raw_dialogs'] = dialogs
self._split_dialogs(dialogs, top_subtitle,
FirstFile=True)
if bottom_subtitle_address.lower().endswith('.smi'):
SMI = SMI2SRT(smi=bottom_subtitle_address,
encoding=self.output_encoding)
converted_smi = SMI.convert_smi(outside=True)
for i in range(len(converted_smi)):
self._check_times(converted_smi[i].MySub, i+1, FirstFile=False)
# self._add_lines(SMI.convert_smi(outside=True),
# len(self.subtitles),
# remove=False)
else:
with open(bottom_subtitle_address, 'rb') as file:
data = file.read()
cchdt = cchardet.detect(data)
codec = cchdt['encoding']
if cchdt['confidence'] < 0.99:
chdt = chardet.detect(data)
if chdt['confidence'] > cchdt['confidence']:
codec = chdt['encoding']
data = data.decode(codec).replace(windows_line_ending,
linux_line_ending)
bottom_dialogs = re.split('\r\n\r|\n\n', data)
bottom_subtitle['data'] = data
bottom_subtitle['raw_dialogs'] = bottom_dialogs
self._split_dialogs(bottom_dialogs, bottom_subtitle,
FirstFile=False)
def get_output_path(self):
if self.output_path.endswith('/'):
return self.output_path + self.output_name
return self.output_path + '/' + self.output_name
def ms2TS(self, timeMS):
HRS_MS = int(3600000)
MINS_MS = int(60000)
SECS_MS = int(1000)
ms = timeMS
hours = ms // HRS_MS
ms -= hours * HRS_MS
minutes = ms // MINS_MS
ms -= minutes * MINS_MS
seconds = ms // SECS_MS
ms -= seconds * SECS_MS
s = '%02d:%02d:%02d,%03d' % (hours, minutes, seconds, ms)
return s
def _write(self):
self.lines = []
for i in range(len(self.subtitles)):
current = self.subtitles[i]
line = str(i+1) + '\n' + current.timestamp() + '\n' + \
current.data() + '\n\n'
if i == len(self.subtitles)-1:
line = line[:-1]
line = line.encode(self.output_encoding)
self.lines.append(line)
if self.lines[-1].endswith(b'\x00\n\x00'):
self.lines[-1] = self.lines[-1][:-3] + b'\x00'
if self.lines[-1].endswith(b'\n'):
self.lines[-1] = self.lines[-1][:-1] + b''
import io
with io.open(self.get_output_path(), 'w',
encoding=self.output_encoding) as output:
output.buffer.writelines(self.lines)
print("'%s'" % (output.name), 'created successfully.')
if __name__ == '__main__':
if (len(sys.argv) < 4) or (len(sys.argv) > 5):
print("Usage:")
print("$python srtmerger top_sub.srt bottom_sub.srt output.srt \
[output_encoding]")
sys.exit(1)
else:
if len(sys.argv) == 5:
encoding = sys.argv[4]
else:
encoding = "utf-8"
m = Merger(output_file=sys.argv[3], output_encoding=encoding)
m.add(sys.argv[1], sys.argv[2])
m._write()