用Python向Kivy Videoplayer添加注释/字幕
我正在尝试从URL向我的Kivy视频播放器添加字幕。这就是我到目前为止所做的。首先,我只是将字幕链接添加到属性中,就像添加视频的源链接一样用Python向Kivy Videoplayer添加注释/字幕,python,kivy,kivy-language,Python,Kivy,Kivy Language,我正在尝试从URL向我的Kivy视频播放器添加字幕。这就是我到目前为止所做的。首先,我只是将字幕链接添加到属性中,就像添加视频的源链接一样 VideoPlayer: source: root.vid_source options: {'allow_stretch': True, 'eos': 'loop'} annotations: root.subs_source ## This doesnt work 根据Kivy文档,我需要一个包含如下列表的“jsa”文件 [
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"}
]
但是源链接包含这种格式的文本(我需要一个带有“标题”键的字典)
所以我创建了一个新类来解析给定格式的字幕
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.\u annotations\u label
,并用VideoAnnotation类返回的内容填充这些标签,因此,也许我需要把上面的解析的放在self.\u注解\u标签中,但我在这里感到困惑 我已经设法解决了这个问题。首先,UrlRequest不起作用,因为我在Kivy应用程序之外使用它,结果证明它不起作用。所以我使用了urllib库,或者你可以使用requests库,这就是我犯的错误。
同样在解析之后,我将该文件保存在subtitles.jsa文件中,这是“annotations”属性所需要的,它现在可以工作了。我已经设法解决了这个问题。首先,UrlRequest不起作用,因为我在Kivy应用程序之外使用它,结果证明它不起作用。所以我使用了urllib库,或者你可以使用requests库,这就是我犯的错误。
同样在解析之后,我将该文件保存在subtitles.jsa文件中,这是“annotations”属性所需要的,它现在可以工作了
#:import playerapp playerapp
VideoPlayer:
.......
#### str conversion since it says it accepts only string####
annotations: str(playerapp.Subtitles(root.subs_source).get_subtitles())