Reading metadata from the of an HTML5 <video> using Captionator

前端 未结 2 1257
深忆病人
深忆病人 2020-12-14 04:37

I am having trouble getting a working example that reads metadata from a WebVTT file, which was specified by the element of a

2条回答
  •  無奈伤痛
    2020-12-14 04:45

    You may need to load your metadata VTT file via Ajax and parse and display it yourself.

    I looked at the example from the HTML5 Doctors' article on video subtitling. They're using Playr, so I checked out its source code, and they're definitely requesting the VTT file asynchronously and parsing the content once it's loaded.

    I was able to load the contents of the VTT file and dump it into the specified element with the following code:

    function changeText() {
        var track = document.getElementById("videoTest").querySelector("track");
        var req_track = new XMLHttpRequest();
        req_track.open('GET', track.getAttribute("src"));
        req_track.onreadystatechange = function(){
            if(req_track.readyState == 4 && (req_track.status == 200 || req_track.status == 0)){
                if(req_track.responseText != ''){
                  document.getElementById("metadataText").innerHTML = req_track.responseText;
                }
            }
        }
        req_track.send(null);
    }
    

    I'm not familiar with Captionator, but it looks like it has some capabilities for parsing VTT files into some sort of data structure, even if it doesn't necessarily support the metadata track type. Maybe you can use a combination of this code and Captionator's existing VTT parser?

提交回复
热议问题