feat: add MAP command to merge text and timestamps from different sources

This commit is contained in:
Emmanuel BENOîT 2025-04-06 10:53:33 +02:00
parent d616aed5df
commit 07ef135a82
Signed by: Emmanuel BENOîT
SSH key fingerprint: SHA256:l7PFUUF5TCDsvYeQC9OnTNz08dFY7Fvf4Hv3neIqYpg

View file

@ -28,24 +28,43 @@ def read_commands(filename):
if not line or line.startswith('#'):
continue
parts = line.split()
if len(parts) != 3:
if not parts:
continue
command = parts[0].upper()
if command != 'COPY':
continue
try:
source = int(parts[1])
if source not in (1, 2):
if command == 'COPY':
if len(parts) != 3:
continue
range_part = parts[2]
start_str, end_str = range_part.split('-')
start = int(start_str)
end = int(end_str)
if start > end:
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
except (ValueError, AttributeError):
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
commands.append((source, start, end))
return commands
if __name__ == '__main__':
@ -62,19 +81,42 @@ if __name__ == '__main__':
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)
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 i, entry in enumerate(output, start=1):
f.write(f"{i}\n")
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'):