如何从 Python 中给定时间戳的 .srt 文件中获取文本
How to fetch text from .srt file for a given timestamp in Python
我已经浏览了一些 python 字幕库(pysrt、pysubs2、srt),但还没有找到一种直接的方法来在给定时间戳从文件中提取 srt 条目。任何方向表示赞赏。
cat subtitle_file.srt
...
7
00:00:49,450 --> 00:00:51,219
Exactly as they looked
8
00:00:51,252 --> 00:00:53,454
Performing on the ed sullivan show.
...
-
>>> import SUBTITLE_LIBRARY
>>> subs = SUBTITLE_LIBRARY.load("subtitle_file.srt")
>>> subs.getSubtitleEntry(t=51.500)
Performing on the ed sullivan show.
您可以使用 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)
我已经浏览了一些 python 字幕库(pysrt、pysubs2、srt),但还没有找到一种直接的方法来在给定时间戳从文件中提取 srt 条目。任何方向表示赞赏。
cat subtitle_file.srt
...
7
00:00:49,450 --> 00:00:51,219
Exactly as they looked
8
00:00:51,252 --> 00:00:53,454
Performing on the ed sullivan show.
...
-
>>> import SUBTITLE_LIBRARY
>>> subs = SUBTITLE_LIBRARY.load("subtitle_file.srt")
>>> subs.getSubtitleEntry(t=51.500)
Performing on the ed sullivan show.
您可以使用 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)