From a7032083b6bb6d8dc48efba9262c1b1d20381b37 Mon Sep 17 00:00:00 2001 From: Jacob Casper Date: Sat, 21 May 2022 19:43:23 -0500 Subject: [PATCH] Read an SRT file and adjust it by minutes + seconds --- timestamps.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100755 timestamps.py diff --git a/timestamps.py b/timestamps.py new file mode 100755 index 0000000..d0bcc66 --- /dev/null +++ b/timestamps.py @@ -0,0 +1,25 @@ +import argparse +import datetime +import sys +import re + +def get_new_line(line, delta): + match = re.match('(\d{2}:\d{2}:\d{2},\d{3}) --> (\d{2}:\d{2}:\d{2},\d{3})', line) + if match: + start = match.group(1) + end = match.group(2) + start_time = datetime.datetime.strptime(start, '%H:%M:%S,%f') + end_time = datetime.datetime.strptime(end, '%H:%M:%S,%f') + new_start_time = start_time + delta + new_end_time = end_time + delta + return f"{new_start_time.strftime('%H:%M:%S,%f')[:-3]} --> {new_end_time.strftime('%H:%M:%S,%f')[:-3]}\n" + return line + +parser = argparse.ArgumentParser(description='Adjust timestamps in a .srt file') +parser.add_argument('--minutes', '-m', nargs='?', type=int, default=0) +parser.add_argument('--seconds', '-s', nargs='?', type=int, default=0) +args = parser.parse_args() + +delta = datetime.timedelta(minutes=args.minutes, seconds=args.seconds) +for line in sys.stdin: + sys.stdout.write(get_new_line(line, delta)) -- 2.20.1