Skip to content

Instantly share code, notes, and snippets.

@djsutherland
Created March 15, 2011 09:19
Show Gist options
  • Save djsutherland/870490 to your computer and use it in GitHub Desktop.
Save djsutherland/870490 to your computer and use it in GitHub Desktop.
Shift all the times in SRT subtitles by a constant amount
#!/usr/bin/env python
# A little script to move the times in SRT subtitle files to
# align properly with your video.
import datetime
import re
time_re = re.compile(r'(\d\d):(\d\d):(\d\d),(\d\d\d)')
def next(i): # for python 2.5
return i.next()
def moveTime(time, delta):
t = datetime.datetime(2000,1,1, *map(int, time)) + delta
return t.strftime('%H:%M:%S') + ',%03d' % t.microsecond
def time_line(line, delta):
a, b = time_re.findall(line)
return ' --> '.join(moveTime(x, delta) for x in (a,b))
def process_block(i, delta):
print next(i), # number
print time_line(next(i), delta)
s = next(i)
while s and s.strip():
print s,
s = next(i)
print
def process_file(lines, delta):
i = iter(lines)
try:
while True:
process_block(i, delta)
except StopIteration:
pass
if __name__ == '__main__':
import sys
delta = datetime.timedelta(seconds=int(sys.argv[1]))
process_file(sys.stdin, delta)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment