【问题标题】:Having trouble Merging 2 subtitle blocks合并 2 个字幕块时遇到问题
【发布时间】: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


【解决方案1】:

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

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.

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

【讨论】:

  • 非常感谢@MartinEvans 没有你就无法做到! :D
  • 您可以使用 subs.clean_indexes() 重新索引 subs
猜你喜欢
  • 1970-01-01
  • 2017-02-01
  • 1970-01-01
  • 2019-10-18
  • 2019-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多