在 Python 中将 annotations/subtitles 添加到 Kivy Videoplayer
Adding annotations/subtitles to Kivy Videoplayer in Python
我正在尝试从 URL 向我的 Kivy 视频播放器添加字幕。这是我到目前为止所做的。首先,我只是将字幕 link 添加到 属性,就像我为视频
添加来源 link 一样
VideoPlayer:
source: root.vid_source
options: {'allow_stretch': True, 'eos': 'loop'}
annotations: root.subs_source ## This doesnt work
根据 Kivy 文档,我需要一个包含这样列表的 'jsa' 文件,我想
[
{"start": 0, "duration": 2,
"text": "This is an example of annotation"},
{"start": 2, "duration": 2,
"bgcolor": [0.5, 0.2, 0.4, 0.5],
"text": "You can change the background color"}
]
但是来源 link 包含这种格式的文本(我需要的是带有 'captions' 键的字典)
{"captions":[{"duration":1961,"content":"When you have 21 minutes to speak,","startOfParagraph":true,"startTime":1610},{"duration":2976,"content":"two million years seems\nlike a really long time.","startOfParagraph":false,"startTime":3595}
所以我创建了一个新的 Class 来解析给定格式的字幕
class Subtitles:
def __init__(self, url):
self.parsed_subs = []
req = UrlRequest(url, self.got_subtitles)
def got_subtitles(self, req, results):
self.parsed_subs = [{"start":sub["startTime"],"duration":sub["duration"], "text": sub["content"]} for sub in results['captions']]
def get_subtitles(self):
return self.parsed_subs
对我的 Kv 文件进行了以下更改
#:import playerapp playerapp
VideoPlayer:
.......
#### str conversion since it says it accepts only string####
annotations: str(playerapp.Subtitles(root.subs_source).get_subtitles())
但是没用。
查看 VideoPlayer 的源代码后,我发现在初始化 VideoPlayer 时它创建了 self._annotations_labels
,它填充了 VideoAnnotation class 返回的内容,所以也许我需要以某种方式将上面的 parsed_subs
放在 self._annotations_labels
中,但我在这里感到困惑。
我已经设法解决了这个问题。首先,UrlRequest 没有工作,因为我在 Kivy 应用程序之外使用它,结果发现它不能像那样工作。所以我使用了 urllib 库,或者你可以使用 requests 库,这就是我犯的错误。
同样在解析之后,我将文件保存在 subtitles.jsa 文件中,这是 'annotations' 属性 所需要的,现在可以使用了。
我正在尝试从 URL 向我的 Kivy 视频播放器添加字幕。这是我到目前为止所做的。首先,我只是将字幕 link 添加到 属性,就像我为视频
添加来源 link 一样VideoPlayer:
source: root.vid_source
options: {'allow_stretch': True, 'eos': 'loop'}
annotations: root.subs_source ## This doesnt work
根据 Kivy 文档,我需要一个包含这样列表的 'jsa' 文件,我想
[
{"start": 0, "duration": 2,
"text": "This is an example of annotation"},
{"start": 2, "duration": 2,
"bgcolor": [0.5, 0.2, 0.4, 0.5],
"text": "You can change the background color"}
]
但是来源 link 包含这种格式的文本(我需要的是带有 'captions' 键的字典)
{"captions":[{"duration":1961,"content":"When you have 21 minutes to speak,","startOfParagraph":true,"startTime":1610},{"duration":2976,"content":"two million years seems\nlike a really long time.","startOfParagraph":false,"startTime":3595}
所以我创建了一个新的 Class 来解析给定格式的字幕
class Subtitles:
def __init__(self, url):
self.parsed_subs = []
req = UrlRequest(url, self.got_subtitles)
def got_subtitles(self, req, results):
self.parsed_subs = [{"start":sub["startTime"],"duration":sub["duration"], "text": sub["content"]} for sub in results['captions']]
def get_subtitles(self):
return self.parsed_subs
对我的 Kv 文件进行了以下更改
#:import playerapp playerapp
VideoPlayer:
.......
#### str conversion since it says it accepts only string####
annotations: str(playerapp.Subtitles(root.subs_source).get_subtitles())
但是没用。
查看 VideoPlayer 的源代码后,我发现在初始化 VideoPlayer 时它创建了 self._annotations_labels
,它填充了 VideoAnnotation class 返回的内容,所以也许我需要以某种方式将上面的 parsed_subs
放在 self._annotations_labels
中,但我在这里感到困惑。
我已经设法解决了这个问题。首先,UrlRequest 没有工作,因为我在 Kivy 应用程序之外使用它,结果发现它不能像那样工作。所以我使用了 urllib 库,或者你可以使用 requests 库,这就是我犯的错误。 同样在解析之后,我将文件保存在 subtitles.jsa 文件中,这是 'annotations' 属性 所需要的,现在可以使用了。