feat: allow selecting text SRT in SYNC command, using other for timestamp
This commit is contained in:
parent
17c7d50eef
commit
49d44a8cc0
1 changed files with 24 additions and 13 deletions
37
merge_srt.py
37
merge_srt.py
|
@ -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 == '-':
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue