Skip to content

Instantly share code, notes, and snippets.

@mbrezu
Created January 1, 2011 21:41
Show Gist options
  • Save mbrezu/762030 to your computer and use it in GitHub Desktop.
Save mbrezu/762030 to your computer and use it in GitHub Desktop.
Combine timings from one srt with text from another.
import sys
import re
import math
import os.path
class SrtEntry(object):
def __init__(self, number, startTime, endTime, text):
self.number = number
self.startTime = startTime
self.endTime = endTime
self.text = text
def __repr__(self):
return "SrtEntry(%d, %lf, %lf, %s)" % (self.number,
self.startTime,
self.endTime,
repr(self.text))
def __str__(self):
def breakTime(t):
h = int(t / 3600)
m = int((t - h * 3600) / 60)
s = int(t - h * 3600 - m * 60)
ms = int((t - math.floor(t)) * 1000)
return [h,m,s,ms]
args = [self.number] + \
breakTime(self.startTime) + \
breakTime(self.endTime) + \
[self.text]
return "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n%s\n\n" % \
tuple(args)
def parseSrtFile(fileName):
def parseSubtitle(lines):
result = SrtEntry(int(lines[0]),
0,
0,
"\n".join([line.strip() for line in lines[2:]]))
pattern = \
'(\d\d):(\d\d):(\d\d),(\d\d\d) --> (\d\d):(\d\d):(\d\d),(\d\d\d)'
match = re.search(pattern, lines[1])
result.startTime = int(match.group(1)) * 3600 \
+ int(match.group(2)) * 60 \
+ int(match.group(3)) \
+ int(match.group(4)) / 1000.0
result.endTime = int(match.group(5)) * 3600 \
+ int(match.group(6)) * 60 \
+ int(match.group(7)) \
+ int(match.group(8)) / 1000.0
return result
f = file(fileName)
content = f.readlines()
f.close()
subtitles = []
currentSubtitle = []
for line in content:
if line.strip() == "":
if len(currentSubtitle) > 0:
subtitles.append(currentSubtitle)
currentSubtitle = []
else:
currentSubtitle.append(line)
if len(currentSubtitle) > 0:
subtitles.append(currentSubtitle)
return [parseSubtitle(lines) for lines in subtitles]
def add_cmd(cmd):
f = file("commands.txt", "at")
f.write(cmd)
f.write("\n")
f.close()
def read_commands():
if os.path.isfile('commands.txt'):
f = file('commands.txt', 'rt')
contents = f.readlines()
f.close()
return [line.strip() for line in contents]
else:
return []
def merge(subs_time, subs_text):
merged = []
result = []
force2 = False
cmds = read_commands()
while len(subs_time) > 0 and len(subs_text) > 0:
print "***********************************"
print subs_time[0]
print subs_text[1]
if not force2:
if len(cmds) == 0:
cmd = raw_input()
if cmd != '5':
add_cmd(cmd)
else:
cmd = cmds[0]
cmds = cmds[1:]
else:
cmd == '2'
if cmd == '1':
subs_time = subs_time[1:]
elif cmd == '3':
subs_text = subs_text[1:]
elif cmd == '2':
result.append(SrtEntry(len(result) + 1,
subs_time[0].startTime,
subs_time[0].endTime,
subs_text[0].text))
subs_time = subs_time[1:]
subs_text = subs_text[1:]
elif cmd == '4':
force2 = True
elif cmd == '5':
break
f = file("bbest.srt", "wt")
for srt in result:
f.write(str(srt))
f.close()
if __name__ == "__main__":
subs_time = parseSrtFile(sys.argv[1])
print (len(subs_time))
subs_text = parseSrtFile(sys.argv[2])
print (len(subs_text))
merge(subs_time, subs_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment