合并 2 个字幕块时遇到问题

Having trouble Merging 2 subtitle blocks

我正在尝试合并 2 个字幕块,以便更轻松地用于 deepl 翻译。虽然可以合并句子并更改结束时间,但我在更改索引号时遇到了麻烦。计数变量递增但从未从索引中减去。

例如,如果我们有这个字幕块:

5
00:00:23,315 --> 00:00:25,108
A streetwise but soulful
teen needed somewhere to live

6
00:00:25,192 --> 00:00:26,610
as he waited for his Juilliard audition.

7
00:00:26,693 --> 00:00:29,488
We'd support his dancing and let
him stay in the guest room, right.

5 和 6 将合并。结束时间将是 6。那工作很好,除非合并时我应该得到 5 和 6 的索引,但我得到 5 和 7。

我正在尝试制作的示例:

5
00:00:23,315 --> 00:00:26,610
A streetwise but soulful
teen needed somewhere to live
as he waited for his Juilliard audition.

6
00:00:26,693 --> 00:00:29,488
We'd support his dancing and let
him stay in the guest room, right.

这是我的代码。我尝试添加 2 个位置,尝试 subs[sub.index].index = subs[sub.index] - count 但 none 有效。

import pysrt
import os

count = 0

# Init pysrt
subs = pysrt.open(" Bojack Horseman36.srt")
# Go through each subtitle
for sub in subs:
    try:
        # Check if it's a sentence if not check if there is another sentence there if not nothing just remove index
        sentence = None
        if subs[sub.index].text.endswith('.') or subs[sub.index].text.endswith('?') or subs[sub.index].text.endswith('!'):
            subs[sub.index].index - count
        else:
            subs[sub.index].text = subs[sub.index].text + '\n' + subs[sub.index+1].text
            count+=1
            subs[sub.index].index - count
            subs[sub.index].end = subs[sub.index+1].end
            del subs[sub.index+1]
    except IndexError:      
        pass

subs.save('translatedsubs.srt', encoding='utf-8')

任何帮助将不胜感激谢谢:D

以下内容可以帮助您入门:

import pysrt

subs = pysrt.open("test.srt")
append_index = None
remove_list = []                # List of unwanted indexes
sub_index = subs[0].index       # Existing starting index

for index, sub in enumerate(subs):
    if append_index is not None:
        subs[append_index].text += "\n" + sub.text
        subs[append_index].end = sub.end
        remove_list.append(index)
    if sub.text[-1] not in '.?!':
        append_index = index
    else:
        append_index = None

# Remove orphaned subs in reverse order        
for index in remove_list[::-1]:     
    del subs[index]

# Reindex remaining subs
for index in range(len(subs)):
    subs[index].index = index + sub_index

subs.save('test out.srt', encoding='utf-8')

如果连续需要多个连接,它可能 运行 会出现问题。

它产生以下输出:

5
00:00:23,315 --> 00:00:26,610
A streetwise but soulful
teen needed somewhere to live
as he waited for his Juilliard audition.

6
00:00:26,693 --> 00:00:29,488
We'd support his dancing and let
him stay in the guest room, right.

请注意,最好不要在正在迭代的列表中删除或添加项目。相反,我创建了一个要删除的索引列表。然后以相反的顺序删除不需要的索引,这样剩余要删除的项目的索引保持不变。