0

I would like to get the cues from the chapters file loaded in my Video.js object. I've already find how to get the track but I need its id in order to access it.

player.textTracks().getTrackById(<trackID>);

I found out where the id is defined in the Video.js 5.14.0 library:

// video.js/dist/video.js (line 19195)
var trackProps = {
  id: options.id || 'vjs_track_' + Guid.newGUID(),
  kind: options.kind || '',
  label: options.label || '',
  language: options.language || ''
};

It seems that you can define your own id by passing an object to the function:

// video.js/dist/video.js (line 19178)
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

I load the chapter track as follow:

<track kind="chapters" src="chapters.vtt" srclang="en" default>

I've read that you can dynamically add track files but you will have to reload the Video.js object.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Quentin Walter
  • 29
  • 1
  • 12

1 Answers1

1

You could get all the tracks and then get one that is of type chapters:

player.on('loadedmetadata', function () {
    var tracks = player.textTracks(),
            chapterTrack;

    for (var i=0; tracks.length > i; i++) {
        if ('chapters' === tracks[i].kind) {
            chapterTrack = tracks[i];
        }
    }

    console.log(chapterTrack.cues);
});

https://github.com/videojs/video.js/blob/master/docs/guides/text-tracks.md#working-with-text-tracks

Hyddan
  • 1,297
  • 15
  • 24
  • Thanks for your answer. The `textTracks` object returns no track item even though I can see my track in its `tracks_` attribute from the console when using `console.log(vvplayer.textTracks());`. This attribute may be private now. – Quentin Walter Dec 08 '16 at 16:00
  • 2
    Just tested with video.js 5.14.0 & 5.14.1. This method works for me, but, it looks like the text tracks aren't always ready when the video.js ready callback is invoked so I had to wrap the code in a listener for the `loadedmetadata` event, that made it work reliably for me at least. Updated the answer with that. – Hyddan Dec 08 '16 at 21:19
  • Many thanks for your tests and your quality answers, I wish I had a debugging tool that give information regarding asynchronous calls. – Quentin Walter Dec 08 '16 at 22:41