From fbf4b17312815ed6edb30b0de522b742a542ee2d Mon Sep 17 00:00:00 2001 From: likho Date: Thu, 6 Mar 2025 22:16:16 -0800 Subject: [PATCH] Added script to update timestamps. --- scripts/update-subs.py | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 scripts/update-subs.py diff --git a/scripts/update-subs.py b/scripts/update-subs.py new file mode 100644 index 0000000..c95b19a --- /dev/null +++ b/scripts/update-subs.py @@ -0,0 +1,50 @@ + +import re +import sys +import argparse + +def offset_timestamp(timestamp, offset): + hours, minutes, seconds, microseconds = map(float, re.split('[:.]', timestamp)) + total_seconds = hours * 3600 \ + + minutes * 60 \ + + seconds \ + + (microseconds * 0.001) \ + + offset + new_hours = int(total_seconds // 3600) + new_minutes = int((total_seconds % 3600) // 60) + new_seconds = total_seconds % 60 + return f"{new_hours:02}:{new_minutes:02}:{new_seconds:.3f}" + +def update_webvtt(file_path, offset): + with open(file_path, 'r', encoding='utf-8') as file: + lines = file.readlines() + + updated_lines = [] + for line in lines: + ts = line[:line.find("align")] + if '-->' in ts: + start, end = ts.split(' --> ') + updated_start = offset_timestamp(start.strip(), offset) + updated_end = offset_timestamp(end.strip(), offset) + updated_lines.append(f"{updated_start} --> {updated_end}\n") + else: + updated_lines.append(line) + + with open('updated_' + file_path, 'w', encoding='utf-8') as file: + file.writelines(updated_lines) + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('input_file', type=str, help='The input file name') + parser.add_argument('time_offset', type=str, help='The output file name') + args = parser.parse_args() + + vtt = args.input_file + t = args.time_offset + + minutes, seconds = map(float, re.split('[:.]', t)) + offset = minutes * 60 + seconds + + print("bumping timestamps in file %s by %f seconds" % (vtt, offset)) + update_webvtt(vtt, -offset) # Use -40 to offset back by 40 seconds +