Read an SRT file and adjust it by minutes + seconds master
authorJacob Casper <dev@jacobcasper.com>
Sun, 22 May 2022 00:43:23 +0000 (19:43 -0500)
committerJacob Casper <dev@jacobcasper.com>
Sun, 22 May 2022 00:44:44 +0000 (19:44 -0500)
timestamps.py [new file with mode: 0755]

diff --git a/timestamps.py b/timestamps.py
new file mode 100755 (executable)
index 0000000..d0bcc66
--- /dev/null
@@ -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))