通过媒体源扩展播放HLS段

时间:2018-02-11 19:08:05

标签: ffmpeg html5-video

我从此FFMPEG命令

获得了m4sinit.mp4的列表
ffmpeg -i bunny.mp4 -f hls -hls_segment_type fmp4 -c:v copy playlist.m3u8

我使用Socket发送这些块并尝试通过MSE播放它们。 当我按此顺序发送时:

init.mp4 + playlist0.m4s + playlist1.m4s ...

他们没有任何问题,但是当我想从3号块开始意味着init.mp4 + playlist3.m4s时,我得到了这个错误:

video frame with PTS 0us has negative DTS -80000us after applying timestampOffset, handling any discontinuity, and filtering against append window.

我希望能够从任何块开始,目前播放视频的唯一方法是init.mp4 + playlist0.m4s代表playlist0.m4s,因为init.mp4只包含视频标题,这是我使用的客户端代码:

var socket = io();
var video = document.querySelector('video');
var mimeCodec = 'video/mp4; codecs="avc1.64000d,mp4a.40.2"'; // true
if ('MediaSource' in window && MediaSource.isTypeSupported(mimeCodec)) 
{
    var mediaSource = new MediaSource;
    video.src = URL.createObjectURL(mediaSource);
    mediaSource.addEventListener('sourceopen', function () {
            var mediaSource = this;
            var sourceBuffer = mediaSource.addSourceBuffer(mimeCodec);
           sourceBuffer.mode = 'segments'; 
            sourceBuffer.addEventListener('updateend', function (_) { video.play().then(function() { }).catch(function(error) { }); });
            socket.on('broadcast', function (chunk) {
                downloadData(chunk.uri, function(arrayBuffer) {
                    sourceBuffer.appendBuffer(arrayBuffer);
                });
            });
    });
} else {
    console.error('Unsupported MIME type or codec: ', mimeCodec);
}
function downloadData(url, cb) {
    var xhr = new XMLHttpRequest;
    xhr.open('get', url);
    xhr.responseType = 'arraybuffer';
    xhr.onload = function () {
        cb(new Uint8Array(xhr.response));
    };
    xhr.send();
}

0 个答案:

没有答案