如何获取m3u8标准播放列表的当前段.ts信息?

时间:2017-11-02 07:05:05

标签: javascript video.js hls m3u8 fairplay

我们正在使用 videojs.fairplay.js 在MAC safari浏览器中播放 DRM 内容。首先在播放DRM视频时,我们得到一个标准播放列表(。m3u8),它已经定义了一些段(。ts)。我们需要知道每个段信息,并有完整的元信息,如(长度,类型,网址等)。

1 个答案:

答案 0 :(得分:0)

var video = videojs("my-video");

video.on('playing', function () {
    console.log("The video has been playing");

    var segment = get_current_segment_info(this);
    console.log(segment.uri);
});

function get_current_segment_info(obj, old_segment = null) {
    var target_media = obj.tech().hls.playlists.media();
    var snapshot_time = obj.currentTime();

var segment;
var segment_time;

// Itinerate trough available segments and get first within which snapshot_time is
for (var i = 0, l = target_media.segments.length; i < l; i++) {
    // Note: segment.end may be undefined or is not properly set
    if (snapshot_time < target_media.segments[i].end) {
        segment = target_media.segments[i];
        break;
    }
}

// Null segment_time in case it's lower then 0.
if (segment) {
    segment_time = Math.max(0, snapshot_time - (segment.end - segment.duration));
    // Because early segments don't have end property
} else {
    segment = target_media.segments[0];
    segment_time = 0;
}
console.log(snapshot_time);
console.log(segment.uri);
console.log(segment.resolvedUri);
console.log(segment);
return segment;

}