feat: allow selecting text SRT in SYNC command, using other for timestamp

This commit is contained in:
Emmanuel BENOîT 2025-04-06 12:19:07 +02:00
parent 17c7d50eef
commit 49d44a8cc0
Signed by: Emmanuel BENOîT
SSH key fingerprint: SHA256:l7PFUUF5TCDsvYeQC9OnTNz08dFY7Fvf4Hv3neIqYpg

View file

@ -64,14 +64,19 @@ def read_commands(filename):
continue
commands.append(('MAP', text_source, text_start, time_source, time_start, count))
elif command == 'SYNC':
if len(parts) != 3:
if len(parts) != 4:
continue
try:
source1_index = int(parts[1])
source2_index = int(parts[2])
text_source = int(parts[1])
text_index = int(parts[2])
time_index = int(parts[3])
if text_source not in (1, 2):
continue
if text_index < 1 or time_index < 2:
continue
except (ValueError, AttributeError):
continue
commands.append(('SYNC', source1_index, source2_index))
commands.append(('SYNC', text_source, text_index, time_index))
else:
continue
return commands
@ -151,19 +156,25 @@ if __name__ == '__main__':
'text': text_entry['text'],
})
elif cmd[0] == 'SYNC':
source1_index, source2_index = cmd[1:]
if source1_index < 2 or source1_index > len(srt1) or source2_index < 1 or source2_index > len(srt2):
print(f"Skipping invalid SYNC command: source1 index {source1_index} must be >=2 and <= {len(srt1)}, source2 index {source2_index} must be >=1 and <= {len(srt2)}")
text_source, text_index, time_index = cmd[1:]
text_list = srt1 if text_source == 1 else srt2
time_list = srt2 if text_source == 1 else srt1
text_start_idx = text_index - 1
time_start_idx = time_index - 1
prev_time_idx = time_start_idx - 1
if (text_start_idx < 0 or text_start_idx >= len(text_list) or
time_start_idx < 1 or prev_time_idx < 0 or time_start_idx >= len(time_list)):
print(f"Skipping invalid SYNC command: text index {text_index} must be >=1 and <= {len(text_list)}, time index {time_index} must be >=2 and <= {len(time_list)}")
continue
entry_prev = srt1[source1_index - 2]
entry1 = srt1[source1_index - 1]
entry2 = srt2[source2_index - 1]
delta = compute_delta(entry_prev['timestamp'], entry1['timestamp'])
new_ts = add_delta_to_timestamp(entry2['timestamp'], delta)
text_entry = text_list[text_start_idx]
time_entry = time_list[time_start_idx]
prev_time_entry = time_list[prev_time_idx]
delta = compute_delta(prev_time_entry['timestamp'], time_entry['timestamp'])
new_ts = add_delta_to_timestamp(time_entry['timestamp'], delta)
output.append({
'index': len(output) + 1,
'timestamp': new_ts,
'text': entry2['text'],
'text': text_entry['text'],
})
if output_filename == '-':