# -*- Mode: Python -*- # fix subtitles that are too far off by adjusting all the timestamps # this is for files with a '.SRT' extension, whatever those are. import sys import re from pdb import set_trace as trace stamp = re.compile ('([0-9][0-9]):([0-9][0-9]):([0-9][0-9]),([0-9]+) --> ([0-9][0-9]):([0-9][0-9]):([0-9][0-9]),([0-9]+)') def pack (h,m,s): return int(h) * 60 * 60 + int(m) * 60 + int(s) def unpack (t): m, s = divmod (t, 60) h, m = divmod (m, 60) return h, m, s def adjust_line (line, offset): m = stamp.match (line) if not m: return line else: h0,m0,s0,x,h1,m1,s1,y = m.groups() t0 = pack (h0, m0, s0) t1 = pack (h1, m1, s1) t0 += offset t1 += offset h0, m0, s0 = unpack (t0) h1, m1, s1 = unpack (t1) return '%02d:%02d:%02d,%s --> %02d:%02d:%02d,%s%s' % ( h0, m0, s0, x, h1, m1, s1, y, line[m.end():] ) input = open (sys.argv[1], 'rb') output = open (sys.argv[1] + '.adjusted', 'wb') offset = int (sys.argv[2]) while 1: line = input.readline() if not line: break else: output.write (adjust_line (line, offset))