Skip to content

Instantly share code, notes, and snippets.

@bingluen
Last active February 7, 2017 01:47
Show Gist options
  • Save bingluen/2f08dfd02da3175475690681da879ba0 to your computer and use it in GitHub Desktop.
Save bingluen/2f08dfd02da3175475690681da879ba0 to your computer and use it in GitHub Desktop.

Usage

python convert2srt.py source_filename > output_filename
import sys
import re
def err(msg):
sys.stderr.write(msg)
sys.stderr.flush()
def out(msg):
sys.stdout.write(msg + '\n')
sys.stdout.flush()
def convert(filename):
# load source txt file
lines = []
with open(filename) as f:
lines = [txt.strip('\n') for txt in f.readlines()]
subs = []
for line in lines:
# split time & subtitle
sub = re.split('\t', line, 1)
if len(sub) > 1:
# convert time format
sub[0] = re.findall('^([0-9]{2}:[0-9]{2}:[0-9]{2})', sub[0])[0] + ',' + str(int(re.findall(':([0-9]{2})$', sub[0])[0]) * 1000 / 60)
# replace useless escape character
sub[1] = re.sub('\t', ' ', sub[1])
sub[1] = re.sub('[\r\n]', '', sub[1])
subs.append(sub)
# write as srt format to stdout
for subIndex in xrange(0, len(subs) - 1):
# wirte index
out(str(subIndex))
# write time
out(subs[subIndex][0] + ' --> ' + subs[subIndex + 1][0])
# write subtitle content
out(subs[subIndex][1])
out('')
if __name__ == '__main__':
if len(sys.argv) < 2:
err("You must give source filename")
else:
for filename in sys.argv[1:]:
convert(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment