I'm trying to add Subtitles to my videoplayer in Kivy from a URL. This is what I have done so far. First I just added the subtitle link to the property, just like I would add the source link for the video
VideoPlayer:
source: root.vid_source
options: {'allow_stretch': True, 'eos': 'loop'}
annotations: root.subs_source ## This doesnt work
According to the Kivy documentation I require a 'jsa' file with list like this I suppose
[
{"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"}
]
but the source link contains text of this format (a dictionary with 'captions' key is what I need)
{"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}
So I created a new Class to parse the subtitles in the given format
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
with following changes to my Kv file
#:import playerapp playerapp
VideoPlayer:
.......
#### str conversion since it says it accepts only string####
annotations: str(playerapp.Subtitles(root.subs_source).get_subtitles())
But it didnt work.
After taking at a look at the source code for VideoPlayer I see that while initializing the VideoPlayer it creates self._annotations_labels
which it populates with what's returned by VideoAnnotation class, so maybe somehow I need to put the above parsed_subs
inside the self._annotations_labels
but I'm getting confused here.
I have managed a workaround to the problem. Firstly, the UrlRequest wasn't working because I was using this outside the Kivy App and turns out it doesn't work like that. So I used urllib library or maybe you could use requests library and that was pretty much it, the mistake I was making. Also after parsing I saved the file in a subtitles.jsa file , which is what 'annotations' property requires and it works now.