Philip Voronov Philip Voronov - 5 months ago 11
Javascript Question

Is there any way to get current caption's text from video tag?

I want to get current subtitles' text during playing a video (and than implement own subtitles block (i.e. to hide original) and also use the information in a few different ways). Currently I use videojs for my player. Is there any way to get current caption's string from it?

Answer

Solution:

    videojs("example_video_1").ready(function() {
        myvideo = this;

        var aTextTrack =  this.textTracks()[0];
        aTextTrack.on('loaded', function() {
            console.log('here it is');
            cues = aTextTrack.cues();   //subtitles content is here
            console.log('Ready State', aTextTrack.readyState()) 
            console.log('Cues', cues);
        });

        //this method call triggers the subtitles to be loaded and loaded trigger
        aTextTrack.show();
    });

Result:

Result


PS. Code found here.