demuxer icon indicating copy to clipboard operation
demuxer copied to clipboard

Remuxing into MP4

Open TroyKomodo opened this issue 3 years ago • 1 comments

Hi, i was curious how i would approach the remux into mp4?

Currently i am reading the data via WebSocket and is being sent over mpegts and I want to add this into a HTMLMediaElement via the SourceBuffer API but i need to demux and then remux in to mp4 to allow for this, as far as i know.

TroyKomodo avatar Dec 20 '20 23:12 TroyKomodo

I am doing exactly what you're asking. Take a look at the jMuxer library, available here: https://github.com/samirkumardas/jmuxer

Here's the code I'm using to do this. The only down side is I am currently having to hard-code the frame rate.

let debug = true;

function startup() {
    // JMuxer will re-mux the H.264 video stream into the MediaSource object
    const jmuxer = new JMuxer({
        node: 'videoElement',
        mode: 'video',
        flushingTime: 1000,
        fps: 30,
        debug
    });

    // Use Demuxer.TSDemux to demux the MPEG-TS transport stream, then pass the H.264 video stream to JMuxer
    let videoStreamID;
    var demuxer = new Demuxer.TSDemux({ debug });
    demuxer.on('DEMUX_DATA', function(e) {
        // Find the first H.264 stream
        if (!videoStreamID) {
            if (e.stream_type === h264StreamType) {
                videoStreamID = e.pid;
            }
        }
        if (videoStreamID && e.pid === videoStreamID && e.pes && e.pes.data_byte) {
            jmuxer.feed({ video: e.pes.data_byte });
        }
    });

    // Open WebSocket connection to the video relay server
    const ws = new WebSocket(wsURL);
    ws.binaryType = "arraybuffer";
    ws.onmessage = function(event) {
        demuxer.push(event.data, {});
    };
}

neodescis avatar Dec 31 '20 22:12 neodescis