-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubadj.py
More file actions
56 lines (48 loc) · 1.98 KB
/
subadj.py
File metadata and controls
56 lines (48 loc) · 1.98 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
import os
import datetime as dt
import argparse
def get_hours_minutes_seconds(td):
days = td.days
hours, remainder = divmod(td.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
# If you want to take into account fractions of a second
seconds += td.microseconds / 1e6
seconds, milliseconds = divmod(seconds, 1)
return int(hours), int(minutes), int(seconds), int(milliseconds*1e3)
def get_time_shift(l,tss):
tstr=''
for i,x in enumerate( l.split('-->') ):
ts=x.split(':')
secs=ts[2].split(',')
temp=dt.timedelta(hours=float(ts[0]), minutes=float(ts[1]), seconds=float(secs[0]), milliseconds=float(secs[1]))
temp+=tss
if temp < dt.timedelta():
temp=dt.timedelta()
h,m,s,ms=get_hours_minutes_seconds(temp)
tstr+='{:02d}:{:02d}:{:02d},{:03d}'.format(h,m,s,ms)
if i==0: tstr+=' --> '
return tstr+'\n'
if __name__ == '__main__':
parser = argparse.ArgumentParser(prog='subtitles adjust timing',
description=''' This programs helps you rewrite a subtitle file
of type srt by shifting the timestamps by a chosen amount
thus resynchronizing the subs to the audio track.
You will have to determine the shift yourself ''')
parser.add_argument('infile', type=str, help='subtitle file, usually ending in srt')
parser.add_argument('shift', type=float, help='time shift in seconds')
args = parser.parse_args()
filename, ext = os.path.splitext(args.infile)
if ext != '.srt':
print('Are you sure?')
fname=filename+'_resync_0xAC'+ext
resync=open(fname,'w+')
time_shift=dt.timedelta(seconds=args.shift)
with open(args.infile,'r') as fin:
print(f'{args.infile} is being processed...')
for line in fin:
if '-->' in line:
resync.write( get_time_shift(line, time_shift) )
else:
resync.write(line)
print(f'{fname} has been written')
resync.close()