【问题标题】:How to fetch text from .srt file for a given timestamp in Python如何从.srt文件中获取Python中给定时间戳的文本
【发布时间】:2019-09-19 12:30:26
【问题描述】:

我浏览了一些 python 字幕库(pysrt、pysubs2、srt),但还没有找到一种直接的方法来从给定时间戳的文件中提取 srt 条目。任何方向表示赞赏。

cat subtitle_file.srt

...

7

00:00:49,450 --> 00:00:51,219

一模一样

8

00:00:51,252 --> 00:00:53,454

在 ed sullivan 节目中表演。

...

-

>>> import SUBTITLE_LIBRARY
>>> subs = SUBTITLE_LIBRARY.load("subtitle_file.srt")
>>> subs.getSubtitleEntry(t=51.500)
Performing on the ed sullivan show.

【问题讨论】:

  • 你有什么问题?

标签: python subtitle


【解决方案1】:

您可以使用pysrt,它可以从 srt 文件中加载和分割字幕。

您的代码如下所示:

import pysrt

subs = pysrt.open('subtitle_file.srt')
parts = subs.slice(starts_before={'minutes': 0, 'seconds': 51.500}, ends_after={'minutes': 0, 'seconds': 51.500})

for part in parts:
    print(part)

【讨论】:

  • 正是我想要的。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 2016-10-03
  • 1970-01-01
  • 2015-06-08
  • 1970-01-01
相关资源
最近更新 更多