Javascript parser for RDF/JSON from WEBVTT

此生再无相见时 提交于 2019-12-03 15:16:56

I've written a WebVTT parser for my <track>/HTML5 video captioning polyfill Captionator.

Feel free to pick apart the source of the development branch (which has the best WebVTT compliance, so it's probably better to look at that rather than the stable branch.)

The parser code starts here: https://github.com/cgiffard/Captionator/blob/captioncrunch/js/captionator.js#L1686

Ultimately though, what you're describing seems to roughly match the intended use case for the metadata track type (as described in the WHATWG's TimedTextTrack spec.) You can use Captionator (I'd love to recommend to you another library as well, but I'm not aware of anything else that doesn't come bundled with an entire video player, or that implements the TimedTextTrack JS API you'll need) to provide support for it - the TextTrack.oncuechange event and TextTrack.activeCues list enable you to listen for changes to cues when the user seeks within the video timeline. You can then get the text of each cue (less the cue metadata and header) and parse it as JSON. Just set up a caption track like below:

<video src="myvideo.webm" poster="poster.jpg" width="512" height="288">
    <track kind="metadata" src="meta.webvtt" type="text/webvtt" srclang="en" label="Metadata Track" default />
</video>

Then, include the captionator library, initialise it as per the documentation, select your track and set up an event handler. You can access the text of an individual cue like so:

var cueText = document.getElementById("video").tracks[0].activeCues[0].getCueAsSource();

Then just:

var RDFData = JSON.parse(cueText);

Good luck :)

It seems that the RDF/JSON is in fact complex and nested JSON structure with vectors, so getJSON function will successfully parse data from it once its fetched from WEBVTT timed structure.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!