-
Notifications
You must be signed in to change notification settings - Fork 1
/
shiftsubs.py
109 lines (90 loc) · 3.07 KB
/
shiftsubs.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
import datetime, time, sys, re
def change_time(hour, minute, second, time_diff):
""" Changes a time by the amount of seconds specified as time_diff
Parameters:
hour: hour value of the time
minute: minute value
second: second value
time_diff: the distance (in seconds) by which to change the time
(this can be positive or negative)
"""
current_time = datetime.datetime(1, 1, 1, hour, minute, second)
difference = datetime.timedelta(seconds=time_diff)
try:
return current_time + difference
except OverflowError:
print 'ERROR: Date value out of range.'
sys.exit()
except:
print 'ERROR: Error changing time.'
return None
def process_time_string(s, time_diff):
""" Processes a .SRT subtitle time string. Ignores milliseconds.
Parameters:
s: the time string. Format: HH:MM:SS,MMS (milliseconds)
time_diff: the time change to apply (in seconds)
"""
# Ignore milliseconds
s = s.split(',')
# Convert to time object
dt = time.strptime(s[0], '%H:%M:%S')
# Apply time difference
dt = change_time(dt.tm_hour, dt.tm_min, dt.tm_sec, time_diff)
# Fix formatting for the minutes
minutes = str(dt.minute)
seconds = str(dt.second)
if len(minutes) == 1:
minutes = '0%s' % (minutes,)
if len(seconds) == 1:
seconds = '0%s' % (seconds,)
new_time_string = '0%s:%s:%s,%s' % (str(dt.hour), minutes, seconds, s[1])
return new_time_string
def is_time_string(s):
""" Determines if a string 's' is an SRT file time string
Format: HH:MM:SS,MMS --> HH:MM:SS,MMS
"""
if re.match(r'^\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}$', s):
return True
return False
def shift_subtitles(rfile, wfile, time_diff):
""" Takes subtitles from rfile, alters them by time_diff, and writes to wfile.
Parameters:
rfile: The subtitle file to read
wfile: The subtitle file to write
time_diff: The time change to apply (in seconds)
Important to note is that the subtitle times are indicated in SRT files by:
01:12:05,328 --> 01:12:09,100
The first time is when the subtitle appears, the second time is when it disappears
"""
# Open files for reading and writing
try:
source_file = file(rfile, 'r')
except IOError:
print 'ERROR: No such file or directory:', rfile
sys.exit()
print 'Reading from file:', rfile
try:
new_file = file(wfile, 'w')
except IOError:
print 'ERROR: No such file or directory:', wfile
sys.exit()
print 'Writing to file:', wfile
new_lines = []
for line in source_file.readlines():
line = line[:-2] # removes '\r\n' from line
print 'Read line:', line
if is_time_string(line):
print 'Identified time string:', line
times = line.split(' --> ') # split up the two times
new_times = []
for t in times:
new_times.append(process_time_string(t, time_diff))
line = new_times[0] + ' --> ' + new_times[1]
print 'Changed times:', line
new_lines.append(line + '\r\n') # adds back in '\r\n'
print 'Line added:', new_lines[-1]
for line in new_lines:
new_file.write(line)
print 'Wrote line:', line
# the script runs from here
shift_subtitles(sys.argv[1], sys.argv[2], int(sys.argv[3]))