-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubtitle.py
executable file
·160 lines (62 loc) · 2.41 KB
/
subtitle.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Some srt subtitile is slower or faster. This little script can adjust it.
#
# Usage: python adjust_srt_timeline.py your_movie.srt delay_second
# e.g. python adjust_srt_timeline.py Shortbus.chs.srt -8.5
#
# Then there will be a "Shortbus.chs.srt_new" file.
#
# Copyright (C) 2008, vvoody
# GPLv3 applies ;-)
import sys, re, datetime
def get_new_timeline(old_timeline, sec, microsec):
'''Add or subtract the timeline and return a new timeline string.
This function can receive the argument like:
00:12:59,123 or
00:12:59.123
Return the new one like:
00:13:01,583
You can substitute "." for "," manually after returning.
'''
hour, minute, second, millisecond = \
map(int, (re.split(r'[:,\.]', old_timeline) ))
# We must set the year, month, day. It cannont be left none.
new_timeline = datetime.datetime(2000, 1, 1, hour, minute, second, millisecond*1000) + \
datetime.timedelta(seconds=sec, microseconds=microsec)
return '%02d:%02d:%02d,%03d' % \
(new_timeline.hour, new_timeline.minute, new_timeline.second, new_timeline.microsecond/1000)
try:
old_srt_name = sys.argv[1]
old_srt = open(old_srt_name, "r")
except IOError:
print >> sys.stderr, "Failed to open your srt file!\n"
try:
new_srt_name = sys.argv[1] + "_new"
new_srt = open(new_srt_name, "w")
except IOError:
print >> sys.stderr, "Failed to create a new srt file!\n"
print "Start..."
# Support .000000 ~ .999999, but .0 ~ .9 is enough.
delay = (float)(sys.argv[2])
second = (int)(delay)
microsecond = (int)(1000000*(delay - second))
# A timeline is like -> "00:12:59,123 -> 00:13:00,291"
modetext = re.compile(r"(\d{2}:\d{2}:\d{2}[.,]\d{3}).*(\d{2}:\d{2}:\d{2}[.,]\d{3})")
for line in old_srt:
if re.match(modetext, line):
timeline_start, timeline_end = re.match(modetext, line).groups()
new_timeline_start = get_new_timeline( timeline_start, second, microsecond )
new_timeline_end = get_new_timeline( timeline_end, second, microsecond )
line = line.replace( timeline_start, new_timeline_start )
line = line.replace( timeline_end, new_timeline_end )
# Print a dot when one line adjusted.
print '.',
new_srt.write(line)
# End of FOR
print
print "Adjust successfully!"
old_srt.close()
new_srt.close()
# End of script.