Created
March 16, 2026 23:40
-
-
Save six519/5f10f7b14e0b78787bcb7142aa646558 to your computer and use it in GitHub Desktop.
Adjust the SRT (subtitle) file time in milliseconds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| **HOW TO INSTALL:** | |
| pip3 install srt_adjust | |
| **HOW TO USE:** | |
| srt_adjust file.srt 1000 | |
| **FOR MORE INFORMATION:** | |
| https://github.com/six519/srt_adjust | |
| """ | |
| import re | |
| import sys | |
| from datetime import ( | |
| datetime, | |
| timedelta, | |
| ) | |
| modified_file = [] | |
| time_format = '%H:%M:%S.%f' | |
| def add_ms(time_str, milliseconds): | |
| dt_obj = datetime.strptime(time_str, time_format) | |
| dt_obj += timedelta(milliseconds=int(milliseconds)) | |
| return dt_obj.time().strftime(time_format)[:-3].replace('.', ',') | |
| if __name__ == '__main__' and len(sys.argv) > 2: | |
| try: | |
| with open(sys.argv[1], 'r') as file: | |
| for line in file: | |
| line = line.strip() | |
| if re.match(r'[0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}', line): | |
| frm, to = line.replace(',', '.').split(' --> ') | |
| frm = add_ms(frm, sys.argv[2]) | |
| to = add_ms(to, sys.argv[2]) | |
| line = f'{frm} --> {to}' | |
| modified_file.append(line) | |
| with open(f'mod_{sys.argv[1]}', 'w') as file: | |
| for line in modified_file: | |
| file.write(f'{line}\n') | |
| print(f'Adjusted srt file: mod_{sys.argv[1]}\n') | |
| except FileNotFoundError: | |
| print(f'Unable to open srt file: {sys.argv[1]}\n') | |
| except ValueError: | |
| print(f'Invalid time value: {sys.argv[2]}\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment