Created
November 3, 2018 14:55
-
-
Save msz/67a6b9cb1cf91f9ce0401ec68b654860 to your computer and use it in GitHub Desktop.
Offsets timestamps in an SRT file by given number of seconds.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import re | |
USAGE = """ | |
Usage: subtitle_mover.py [offset_seconds] < original.srt > processed.srt | |
""" | |
def move_timestamps(line, seconds_offset): | |
new_line = line | |
matches = re.findall("\\d\\d:\\d\\d:\\d\\d", line) | |
for match in matches: | |
vals = [int(x) for x in match.split(':')] | |
seconds = vals[0] * 60 * 60 + vals[1] * 60 + vals[2] | |
new_seconds = seconds + seconds_offset | |
val1 = new_seconds // (60**2) | |
new_seconds -= val1 * 60**2 | |
val2 = new_seconds // 60 | |
new_seconds -= val2 * 60 | |
val3 = new_seconds | |
r = ["{}".format(x).rjust(2, '0') for x in [val1, val2, val3]] | |
replacement = "{}:{}:{}".format(r[0], r[1], r[2]) | |
new_line = new_line.replace(match, replacement) | |
return new_line | |
try: | |
seconds_to_move = int(sys.argv[1]) | |
except: | |
print(USAGE) | |
sys.exit(-1) | |
for line in sys.stdin: | |
print(move_timestamps(line.rstrip(), seconds_to_move)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment