-------------------------------------------------------------------------------
# Name: sub2srt-python-command-line
# Purpose:
#
# Author: Bojan
#
# Created: 24/06/2012
# Licence: bsd
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import os
import sys
def conversion(a):
#do the text
start = a.split('}{',1) [0][1:]
tem = a.split('}{',1) [1]
end = tem.split('}',1)[0]
text = a.split('}',2)[2]
#replace | with newline
text = text.replace('|','\n')
start = timec(start)
end = timec(end)
#print ( "{0} --> {1}\n{2}".format(start, end, text) )
return "{0} --> {1}\n{2}".format(start, end, text)
def timec(ti):
#conversion time
sec = int(ti)/frames
h = str( int((int(sec/60)-int(sec/60)%60)/60) )
m = str( int(sec/60)%60 )
s = str( int(sec%60) )
c = str( round(sec%100,3) )
c = ( c.split('.',1) [1] )
if c == "":
#c must be string, not c = 0
c = '0'
if (len(h)==1):
h = '0'+h
if (len(m)==1):
m = '0'+m
if (len(s)==1):
s = '0'+s
#after decimal 3=300, not 003
if (len(c)==1):
c = c+'00'
if (len(c)==2):
c = c+'0'
#print ( "{0}:{1}:{2},{3}".format(h, m,s,c) )
return "{0}:{1}:{2},{3}".format(h, m,s,c)
def main():
#0 = prog, ...
if len(sys.argv) < 4:
print ('sub2srt-python-command-line input.sub output.srt fps')
sys.exit()
infile = sys.argv[1]
fo = open(infile,'r')
global frames
frames = float(sys.argv[3])
outf = sys.argv[2]
if os.path.isfile(outf):
os.remove(outf)
of = open(outf,'a')
for indx,line in enumerate(fo):
of.write( str(indx+1) + '\n' + conversion(line)+ '\n' )
fo.close()
of.close()
if __name__ == '__main__':
main()