51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
|
|
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
|
|
|