【发布时间】:2017-12-17 19:55:32
【问题描述】:
我正在尝试合并 2 个字幕块,以便更轻松地用于 deepl 的翻译。虽然可以合并句子并更改结束时间,但我在更改索引号时遇到了麻烦。 count 变量会递增,但不会从索引中减去。
例如,如果我们有这个字幕块:
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
【问题讨论】:
-
您能否包括合并输出的实际外观,这样会更容易理解。
-
@MartinEvans 立即查看
标签: python python-3.x merge subtitle