forked from jishnu7/sub-to-srt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub_to_srt.py
71 lines (65 loc) · 2.32 KB
/
sub_to_srt.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
import sys, os, re
# Convert total seconds into hours, min, sec and milli sec
def sec_to_time(sec):
hours = int(sec / 3600)
minutes = int((sec % 3600) / 60)
seconds = int(sec % 3600 % 60)
milli = int(1000*round(sec - int(sec),3))
if sec<0:
return None,None,None,None
return hours,minutes,seconds,milli
# Convert hours, min, sec and milli sec to total sec
def time_to_sec(h,m,s,ms):
return float(h*3600+m*60+s+ms/60.0)
# Function to convert frame number to corresponding minutes and seconds
def frame_to_time(frame, frame_rate, span=0):
sec = int(frame)/float(frame_rate)
if span!=0:
temp = span
try:
neg = temp.split("-")[1]
except:
neg = None
h = int(span.split(":")[0])
m = int(span.split(":")[1])
s = int(span.split(":")[2])
ms = int(span.split(":")[3])
span = time_to_sec(abs(h),m,s,ms)
if neg!=None:
span = -span
hours,minutes,seconds,milli = sec_to_time(sec+span)
if hours != None:
return "%02d:%02d:%02d,%s" % (hours,minutes,seconds,milli)
else:
return None
# Function to convert sub format to srt
def sub_to_srt(sub,srt,frame_rate,span=0):
line_count = 0
out_file = open(srt, "w")
# Read sub file line by line
for line in open(sub):
line_count +=1
try:
start_frame, end_frame, text = re.findall("\{(\d+)\}\{(\d+)\}(.*)$", line)[0]
except:
continue
start_time = frame_to_time(start_frame, frame_rate,span)
end_time = frame_to_time(end_frame, frame_rate,span)
# Write to file
if start_time != None or end_time != None:
out_file.write("%d\n%s --> %s\n%s\n\n" %(line_count,start_time,end_time,text.strip().replace("|", "\n")))
out_file.close()
if __name__ == '__main__':
arg_len = len(sys.argv)
if arg_len<4 or arg_len>5:
print "-"*75
print "sub_to_srt <source file> <destination> <frame rate> <(optional) Time span in hour:min:sec:milli sec format>"
print "-"*75
sys.exit(0)
if not os.path.isfile(sys.argv[1]):
print "Source file does not exsist"
sys.exit(0)
if arg_len==5:
sub_to_srt(sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4])
else:
sub_to_srt(sys.argv[1],sys.argv[2],sys.argv[3])