From: Jacob Casper Date: Sun, 22 May 2022 00:43:23 +0000 (-0500) Subject: Read an SRT file and adjust it by minutes + seconds X-Git-Url: https://git.jacobcasper.com/?a=commitdiff_plain;h=HEAD;p=srt-adjust.git Read an SRT file and adjust it by minutes + seconds --- a7032083b6bb6d8dc48efba9262c1b1d20381b37 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))