Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaSource closed after appendBuffer

I have two videos that I'm trying to play using MediaSource, but only one of them works. Both have the codecs set as avc1.4d401f and mp4a.40.2, but while one of them plays just fine, the other one closes the MediaSource as soon as I call the appendBuffer on the SourceBuffer. The relevant bits of the code are as follows:

var mainSource;
var mimeCoded = 'video/mp4; codecs="avc1.4d401f,mp4a.40.2"';

    mainSource = new MediaSource();
    var sourceBuffer;

    mainSource.addEventListener('sourceopen', () => {
        console.log('readystate', mainSource.readyState);
        var xhr = new XMLHttpRequest();
        xhr.responseType = 'arraybuffer';
        xhr.open('get', 'main.mp4');
        xhr.addEventListener('load', (e) => {
            sourceBuffer = mainSource.addSourceBuffer(mimeCoded);
            sourceBuffer.mode = 'sequence';
            sourceBuffer.addEventListener('updateend', onUpdateEnd);
            sourceBuffer.appendBuffer(e.target.response);
            console.log('updating', sourceBuffer.updating);
        });
        xhr.send();

    });

    vid.src = URL.createObjectURL(mainSource);

    onUpdateEnd = function ()
    {
        console.log('readystate2', mainSource.readyState, sourceBuffer.updating);
        vid.play();
        sourceBuffer.removeEventListener('updateend', onUpdateEnd);
    };

When using one of the videos, both logs on mainSource.readyState will output open, but with another one it shows closed on the second log (and consequently errors on vid.play()). I'm already out of ideas on what may be going on, so any help is appreciated.

like image 925
cvelenosi Avatar asked Sep 11 '25 14:09

cvelenosi


1 Answers

Answering for further reference, for it to work the video must be fragmented on encoding. Using the following ffmpeg command did the trick:

ffmpeg -i input.mp4 -movflags frag_keyframe+empty_moov+default_base_moof output_frag.mp4

like image 192
cvelenosi Avatar answered Sep 13 '25 04:09

cvelenosi