Created
July 4, 2026 01:59
-
-
Save ckhung/db58a62a32be8380caf89cbf2bad774b to your computer and use it in GitHub Desktop.
srt file + txt file => srt file with all text replaced
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
| #!/usr/bin/env python3 | |
| import argparse, re, sys | |
| parser = argparse.ArgumentParser( | |
| description='replace subtitle text with corrected text', | |
| formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
| parser.add_argument('sub', help='subtitle file') | |
| parser.add_argument('txt', help='corrected text file') | |
| args = parser.parse_args() | |
| timestamp = re.compile(r'^\d{2}:\d{2}:\d{2},\d{3} --> ') | |
| srt = open(args.sub, encoding='utf-8') | |
| txt = open(args.txt, encoding='utf-8') | |
| for line in srt: | |
| if timestamp.match(line): | |
| # Print timestamp | |
| sys.stdout.write(line) | |
| # Replace all subtitle text lines until the blank line | |
| while True: | |
| line = next(srt) | |
| if line.strip() == '': | |
| sys.stdout.write('\n') | |
| break | |
| sys.stdout.write(next(txt)) | |
| else: | |
| # Subtitle number or blank line | |
| sys.stdout.write(line) | |
| srt.close() | |
| txt.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment