import re
import datetime
def shift_time(timestamp, delta):
time_format = "%H:%M:%S,%f"
time_obj = datetime.datetime.strptime(timestamp, time_format)
new_time = time_obj + delta
return new_time.strftime(time_format)[:-3]
def modify_srt(file_path, minutes, seconds, direction):
delta = datetime.timedelta(minutes=minutes, seconds=seconds)
if direction == 'backward':
delta = -delta
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
time_pattern = re.compile(r'(\d{2}:\d{2}:\d{2},\d{3}) --> (\d{2}:\d{2}:\d{2},\d{3})')
with open(file_path, 'w', encoding='utf-8') as file:
for line in lines:
match = time_pattern.match(line)
if match:
start_time = shift_time(match.group(1), delta)
end_time = shift_time(match.group(2), delta)
line = f"{start_time} --> {end_time}\n"
file.write(line)
if __name__ == "__main__":
file_path = input("Enter the path to your .srt file: ")
minutes = int(input("Enter the number of minutes to shift: "))
seconds = int(input("Enter the number of seconds to shift: "))
direction = input("Enter 'forward' to move times forward or 'backward' to move times backward: ").strip().lower()
modify_srt(file_path, minutes, seconds, direction)
print("Subtitle times have been modified successfully.")
To run it:
PS D:\projects\srt_update> python srt_update.py
Enter the path to your .srt file: input.srt
Enter the number of minutes to shift: 0
Enter the number of seconds to shift: 20
Enter 'forward' to move times forward or 'backward' to move times backward: forward
Subtitle times have been modified successfully.
No comments:
Post a Comment