简体   繁体   English

HTML5视频,MediaSource,appendBuffer,如何跳过一些块

[英]HTML5 Video, MediaSource, appendBuffer, How to skip some chunks

if skip some chunks, the video element will cannot play. 如果跳过一些块,则视频元素将无法播放。

HTML: HTML:

<div>
    <video id="vdo0" autoplay muted width="300"></video>
    <video id="vdo1" autoplay width="300"></video>
</div>
<div>
    <button id="btnStart">Start</button>
</div>

Javascript: Javascript:

var vdo0 = document.getElementById('vdo0');
var vdo1 = document.getElementById('vdo1');
var ms = new MediaSource();
var sourceBuffer = null;
var chunks = [];

vdo1.src = URL.createObjectURL(ms);
var i = 0;
var recv = function(){
    var chunk = chunks.shift();
    if(chunk){
        i++;
        if(i < 5 || i > 10){ // HERE, skipped some chunks, vdo1 will cannot play
            sourceBuffer.appendBuffer(chunk);
        }
        recv();
    }else{
        setTimeout(recv, 20);
    }
};

ms.addEventListener('sourceopen', function () {
    sourceBuffer = ms.addSourceBuffer('video/webm;codecs=vp8');
    recv();
}, false);

document.getElementById('btnStart').addEventListener('click', function(){
    navigator.mediaDevices.getUserMedia({
        video: true,
        audio: true
    }).then(function (s) {
        vdo0.srcObject = s;
        vdo0.oncanplay = function(){
            var mediaRecorder = new MediaRecorder(s, 'video/webm;codecs=vp8');
            mediaRecorder.ondataavailable = function (e) {
                if (e.data && e.data.size > 0) {
                    var reader = new FileReader();
                    reader.addEventListener("loadend", function () {
                        var arr = new Uint8Array(reader.result);
                        chunks.push(arr);
                    });
                    reader.readAsArrayBuffer(e.data);
                }
            };
            mediaRecorder.start(20);
        };
    }).catch(function (err) {
        A.log('ERROR: ', err);
    });
}, false);

i want to do, when skip some chunks, the vdo1 still can play. 我想做,当跳过一些块时,vdo1仍然可以播放。 MediaSource how can recv any blobs ? MediaSource如何接收任何斑点?

if not push first recorder-blob to chunks, the vdo1 cannot play too. 如果不将第一个记录器blob推送到块,则vdo1也无法播放。 why ? 为什么呢?

How do ? 怎么办

Thanks. 谢谢。

You can't arbitrarily skip chunks. 您不能随意跳过大块。 The demuxer will not be able to re-sync at a random point. 解复用器将无法在随机点重新同步。

Since you're using WebM, you can skip to another Cluster. 由于您正在使用WebM,因此可以跳到另一个群集。 Clusters begin with 0x1F43B675. 群集以0x1F43B675开头。 https://matroska.org/technical/specs/index.html#Cluster https://matroska.org/technical/specs/index.html#Cluster

Note that for this to work, your video much be encoded in such a way that each Cluster begins with a keyframe. 请注意,要使此功能起作用,您的视频将以某种方式编码,即每个群集均以关键帧开头。 Otherwise, your video isn't going to continue playing as the codec won't be happy with it. 否则,您的视频将无法继续播放,因为编解码器对此不会满意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM