feat: Add merge_srt.py to merge SRT files using COPY commands from a file

This commit is contained in:
Emmanuel BENOîT 2025-04-06 10:38:39 +02:00
commit d616aed5df
Signed by: Emmanuel BENOîT
SSH key fingerprint: SHA256:l7PFUUF5TCDsvYeQC9OnTNz08dFY7Fvf4Hv3neIqYpg

82
merge_srt.py Normal file
View file

@ -0,0 +1,82 @@
import sys
def parse_srt(filename):
with open(filename, 'r', encoding='utf-8') as f:
content = f.read().strip()
blocks = content.split('\n\n')
entries = []
for block in blocks:
lines = block.split('\n')
if len(lines) < 3:
continue
index = int(lines[0])
timestamp = lines[1]
text = '\n'.join(lines[2:]).strip()
entries.append({
'index': index,
'timestamp': timestamp,
'text': text,
})
return entries
def read_commands(filename):
with open(filename, 'r', encoding='utf-8') as f:
lines = f.readlines()
commands = []
for line in lines:
line = line.strip()
if not line or line.startswith('#'):
continue
parts = line.split()
if len(parts) != 3:
continue
command = parts[0].upper()
if command != 'COPY':
continue
try:
source = int(parts[1])
if source not in (1, 2):
continue
range_part = parts[2]
start_str, end_str = range_part.split('-')
start = int(start_str)
end = int(end_str)
if start > end:
continue
except (ValueError, AttributeError):
continue
commands.append((source, start, end))
return commands
if __name__ == '__main__':
if len(sys.argv) != 5:
print("Usage: python merge_srt.py <srt1> <srt2> <commands> <output>")
sys.exit(1)
srt1_filename = sys.argv[1]
srt2_filename = sys.argv[2]
commands_filename = sys.argv[3]
output_filename = sys.argv[4]
srt1 = parse_srt(srt1_filename)
srt2 = parse_srt(srt2_filename)
commands = read_commands(commands_filename)
output = []
for source, start, end in commands:
source_list = srt1 if source == 1 else srt2
start_idx = start - 1
end_idx = end - 1
if start_idx < 0 or end_idx >= len(source_list) or start_idx > end_idx:
print(f"Skipping invalid command: source {source}, range {start}-{end}")
continue
entries = source_list[start_idx:end_idx + 1]
output.extend(entries)
with open(output_filename, 'w', encoding='utf-8') as f:
for i, entry in enumerate(output, start=1):
f.write(f"{i}\n")
f.write(entry['timestamp'] + '\n')
text = entry['text'].strip()
for line in text.split('\n'):
f.write(line + '\n')
f.write('\n')