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

谁都会走 提交于 2019-12-08 02:58:27

问题


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?


回答1:


This code gets the current cue and put into the <span> element

(function(){

    var video = document.querySelector('video');
    var span = document.querySelector('span');

    if (!video.textTracks) return;

    var track = video.textTracks[0];
    track.mode = 'hidden';

    track.oncuechange = function(e) {

        var cue = this.activeCues[0];
        if (cue) {
            span.innerHTML = '';
            span.appendChild(cue.getCueAsHTML());
        }

    };
})()

Here is the tutorial :Getting Started With the Track Element




回答2:


Yes, you can add a cuechange event listener when your video loads. This can get you the current track's caption text. Here is my working example using videojs.




回答3:


var videoElement = document.querySelector("video");
var textTracks = videoElement.textTracks; 
var textTrack = textTracks[0]; 
var kind = textTrack.kind // e.g. "subtitles"
var mode = textTrack.mode

Try this one




回答4:


Use the HTML5 Video API to get the current source, the split the src using / and . to get the name.

Media API

The above link has all the available API in the HTML5 video player. Your plugin uses HTML5 video player!




回答5:


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:


PS. Code found here.



来源:https://stackoverflow.com/questions/32043406/is-there-any-way-to-get-current-captions-text-from-video-tag

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