124 lines
4.5 KiB
Python
124 lines
4.5 KiB
Python
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 not parts:
|
|
continue
|
|
command = parts[0].upper()
|
|
if command == 'COPY':
|
|
if len(parts) != 3:
|
|
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(('COPY', source, start, end))
|
|
elif command == 'MAP':
|
|
if len(parts) != 6:
|
|
continue
|
|
try:
|
|
text_source = int(parts[1])
|
|
text_start = int(parts[2])
|
|
time_source = int(parts[3])
|
|
time_start = int(parts[4])
|
|
count = int(parts[5])
|
|
if text_source not in (1, 2) or time_source not in (1, 2):
|
|
continue
|
|
if text_start < 1 or time_start < 1 or count < 1:
|
|
continue
|
|
except (ValueError, AttributeError):
|
|
continue
|
|
commands.append(('MAP', text_source, text_start, time_source, time_start, count))
|
|
else:
|
|
continue
|
|
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 cmd in commands:
|
|
if cmd[0] == 'COPY':
|
|
_, source, start, end = cmd
|
|
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 COPY command: source {source}, range {start}-{end}")
|
|
continue
|
|
entries = source_list[start_idx:end_idx + 1]
|
|
output.extend(entries)
|
|
elif cmd[0] == 'MAP':
|
|
_, text_source, text_start, time_source, time_start, count = cmd
|
|
text_list = srt1 if text_source == 1 else srt2
|
|
time_list = srt1 if time_source == 1 else srt2
|
|
text_start_idx = text_start - 1
|
|
time_start_idx = time_start - 1
|
|
if text_start_idx < 0 or text_start_idx + count > len(text_list):
|
|
print(f"Skipping invalid MAP command: text source {text_source}, start {text_start}, count {count}")
|
|
continue
|
|
if time_start_idx < 0 or time_start_idx + count > len(time_list):
|
|
print(f"Skipping invalid MAP command: time source {time_source}, start {time_start}, count {count}")
|
|
continue
|
|
for i in range(count):
|
|
text_entry = text_list[text_start_idx + i]
|
|
time_entry = time_list[time_start_idx + i]
|
|
new_entry = {
|
|
'index': len(output) + 1,
|
|
'timestamp': time_entry['timestamp'],
|
|
'text': text_entry['text'],
|
|
}
|
|
output.append(new_entry)
|
|
|
|
with open(output_filename, 'w', encoding='utf-8') as f:
|
|
for entry in output:
|
|
f.write(f"{entry['index']}\n")
|
|
f.write(entry['timestamp'] + '\n')
|
|
text = entry['text'].strip()
|
|
for line in text.split('\n'):
|
|
f.write(line + '\n')
|
|
f.write('\n')
|