-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmerge.py
More file actions
executable file
·161 lines (146 loc) · 4.09 KB
/
Copy pathsubmerge.py
File metadata and controls
executable file
·161 lines (146 loc) · 4.09 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 Tao Chen <pro711@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
'''
Merge two subtitles into a single file.
'''
import sys
import pysrt
############################################
# Parameters
############################################
# The max time difference of two lines
SLACK = 500
# Whether combine each language into a single line
JOIN_LINES = True
def srttime_to_ms(t):
'Convert srttime object to number of milliseconds.'
return 1000*(t.hours*3600+t.minutes*60+t.seconds)+t.milliseconds
def indicator(d):
if abs(d) < SLACK:
return 0
elif d > 0:
return 1
else:
return -1
def timediff_to_indicator(std, etd):
'''Indicator function for time difference'''
std = srttime_to_ms(std)
etd = srttime_to_ms(etd)
if abs(std) + abs(etd) < 2 * SLACK:
return (0, 0)
else:
return (indicator(std), indicator(etd))
def compare_srtitem(a, b):
'''
Compare two subtitle items to determine precedence.
Returns: a tuple indicating the relative precedence of
the start and end of two items.
'''
# perfect match
if a.start == b.start and a.end == b.end:
return (0, 0)
# fuzzy match
std = a.start-b.start
etd = a.end-b.end
return timediff_to_indicator(std, etd)
def merge_lines(*lines):
subs = []
for line in lines:
if JOIN_LINES:
line = line.replace('\n', ' ')
subs.append(line)
return '\n'.join(subs)
def merge(sub1, sub2):
try:
sub1 = pysrt.SubRipFile.open(sub1)
except UnicodeDecodeError:
sub1 = pysrt.SubRipFile.open(sub1, encoding='GBK')
try:
sub2 = pysrt.SubRipFile.open(sub2)
except UnicodeDecodeError:
sub2 = pysrt.SubRipFile.open(sub2, encoding='GBK')
n_sub1 = len(sub1)
n_sub2 = len(sub2)
# merged subtitle
subm = pysrt.SubRipFile()
i = 0
j = 0
k = 0
while (i < n_sub1 and j < n_sub2):
a = sub1[i]
b = sub2[j]
(si, ei) = compare_srtitem(a, b)
if (si == 0 and ei == 0):
item = pysrt.SubRipItem(start=a.start, end=a.end, text=merge_lines(a.text, b.text))
subm.append(item)
i = i + 1
j = j + 1
elif (si == 0 and ei == -1):
a_next = sub1[i+1]
if compare_srtitem(a_next, b)[1] == 0:
# combine two items of sub1
text1 = a.text + ' ' + a_next.text
text2 = b.text
item = pysrt.SubRipItem(start=b.start, end=b.end, text=merge_lines(text1, text2))
subm.append(item)
i = i + 2
j = j + 1
else:
item = pysrt.SubRipItem(start=a.start, end=a.end, text=a.text)
subm.append(item)
i = i + 1
elif (si == 0 and ei == 1):
b_next = sub2[j+1]
if compare_srtitem(a, b_next)[1] == 0:
# combine two items of sub1
text1 = a.text
text2 = b.text + ' ' + b_next.text
item = pysrt.SubRipItem(start=a.start, end=a.end, text=merge_lines(text1, text2))
subm.append(item)
i = i + 1
j = j + 2
else:
item = pysrt.SubRipItem(start=b.start, end=b.end, text=b.text)
subm.append(item)
j = j + 1
elif (si < 0):
item = pysrt.SubRipItem(start=a.start, end=a.end, text=a.text)
subm.append(item)
i = i + 1
else:
item = pysrt.SubRipItem(start=b.start, end=b.end, text=b.text)
subm.append(item)
j = j + 1
# add any lines left
if i < n_sub1:
subm.extend(sub1[i:])
if j < n_sub2:
subm.extend(sub2[j:])
# correct indexes
subm.clean_indexes()
# write output file
subm.save('merged.srt')
def main(sub1, sub2):
merge(sub1, sub2)
if __name__ == '__main__':
if len(sys.argv) == 3:
main(sys.argv[1], sys.argv[2])
else:
print 'Usage: '+sys.argv[0]+' sub1.srt sub2.srt'