gpt4 book ai didi

python - 合并 2 个字幕 block 时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:56 25 4
gpt4 key购买 nike

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

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

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 但都没有用。

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.

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

关于python - 合并 2 个字幕 block 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47858885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com