video.js - 使用 video.js 是否可以获得当前的 HLS 时间戳?

标签 video.js http-live-streaming

我有一个在其中嵌入实时流的应用程序。为了满足延迟,我想知道流的当前时间戳是什么,并将其与服务器上的时间进行比较。

到目前为止我测试的是检查视频的缓冲时间与视频当前时间之间的差异:

player.bufferedEnd() -player.currentTime()

但是我想将时间与服务器进行比较,为此我需要获取上次请求的 .ts 文件的时间戳。

所以,我的问题是使用 video.js,是否有某种 Hook 来获取最后请求的 .ts 文件的时间戳?

Video.js版本:7.4.1

最佳答案

我已经设法解决了这个问题,但是请耐心等待,我不记得在哪里找到了这段代码的文档。

就我而言,我正在使用 Angular 应用程序,我有一个视频组件负责使用 video.js 加载实时流。无论如何,让我们看一些代码......

视频初始化

private videoInit() {
    this.player = videojs('video', {
        aspectRatio: this.videoStream.aspectRatio,
        controls: true,
        autoplay: false,
        muted: true,
        html5: {
            hls: {
                overrideNative: true
            }
        }
    });

    this.player.src({
        src: '://some-stream-url.com',
        type: 'application/x-mpegURL'
    });

    // on video play callback
    this.player.on('play', () => {
        this.saveHlsObject();
    });
}

保存 HLS 对象

private saveHlsObject() {
    if (this.player !== undefined) {
        this.playerHls = (this.player.tech() as any).hls;

        // get and syncing server time...
        // make some request to get server time...
        // then calculate difference...
        this.diff = serverTime.getTime() - this.getVideoTime().getTime();
    }
}

获取视频片段的时间戳

// access the player's playlists, get the last segment and extract time
// in my case URI of segments were for example: 1590763989033.ts
private getVideoTime(): Date {
    const targetMedia = this.playerHls.playlists.media();

    const lastSegment = targetMedia.segments[0];

    const uri: string = lastSegment.uri;
    const segmentTimestamp: number = +uri.substring(0, uri.length - 3);

    return new Date(segmentTimestamp);
}

所以上面的要点是getVideoTime函数。分段的时间可以在分段 URI 中找到,因此该函数从分段 URI 中提取时间,然后将其转换为 Date 对象。现在说实话,我不知道这个 URI 格式是 HLS 标准还是为我要连接的特定流设置的格式。希望这对您有所帮助,抱歉我没有更多具体信息!

关于video.js - 使用 video.js 是否可以获得当前的 HLS 时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55060676/

相关文章:

javascript - 播放 video.js ustream m3u8 文件流

javascript - 使用 Underscore _.bind 时无法在监听器上获取 videojs

android - 如何在 Google Cast 发送器应用程序的 hls 文件上启用字幕

FFmpeg:1个输入流,2个不同属性的输出流

node.js - 使用 Node JS 的 HLS 流

javascript - 大视频 : How to Hide Control Options Under Video

video - MPEG-DASH 流中的不同视频时长

jquery - 了解 Video.JS 是否使用 HTML5 或 Flash Player

iphone - 关于 HTTP 直播

ffmpeg - 如何使用 ffmpeg 加密 AES-128 HLS m3u8 播放列表?