ffmpegwebrtcmp4mediarecordermediastream

webRTC convert webm to mp4 with ffmpeg.js


I am trying to convert webM files to mp4 with ffmpeg.js. I am recording a video from canvas(overlayer with some information) and recording the audio data from the video.

stream = new MediaStream();
var videoElem = document.getElementById('video');
var videoStream = videoElem.captureStream();
stream.addTrack(videoStream.getAudioTracks()[0]);
stream.addTrack(canvas.captureStream().getVideoTracks()[0]);
var options = {mimeType: 'video/webm'};
  recordedBlobs = [];
  mediaRecorder = new MediaRecorder(stream, options);
  mediaRecorder.onstop = handleStop;
  mediaRecorder.ondataavailable = handleDataAvailable;
  mediaRecorder.start(100); // collect 100ms of data

function handleDataAvailable(event) {
  if (event.data && event.data.size > 0) {
    recordedBlobs.push(event.data);
  }
}
mediaRecorder.stop();

This code works as expected and returns a webm video

var blob = new Blob(recordedBlobs, {type: 'video/webm'});

Now I want a mp4 file and checked the ffmpeg.js from muaz-khan. The examples just show how to convert to mp4 when you have 2 single streams (audio and video). But I have one stream with an additional audio track. Can I convert such a stream to mp4? How can that be done?


Solution

  • As per the provided code sample, your recorder stream is having only one audio & one video tracks.

    If your input file is having both Audio & Video, then you need to specify output codec for both tracks here as following.

    worker.postMessage({
        type: 'command',
        arguments: [
           '-i', 'audiovideo.webm',
           '-c:v', 'mpeg4',
           '-c:a', 'aac', // or vorbis
           '-b:v', '6400k',  // video bitrate
           '-b:a', '4800k',  // audio bitrate
           '-strict', 'experimental', 'audiovideo.mp4'
         ],
        files: [
            {
                data: new Uint8Array(fileReaderData),
                name: 'audiovideo.webm'
            }
         ]
        });
    

    Trans-coding the video inside browser is not recommend, as it will consume more CPU Time & Memory. And ffmpeg_asm.js is heavy. May be ok for POC :)

    What is your use case? webm(vp8/vp9) is widely using these days.

    Chrome will support following mime types:

    "video/webm"
    "video/webm;codecs=vp8"
    "video/webm;codecs=vp9"
    "video/webm;codecs=h264"
    "video/x-matroska;codecs=avc1"
    

    So you can get mp4 recording directly from chrome MediaRecorder with following hack

    var options = {mimeType: 'video/webm;codecs=h264'}; 
    mediaRecorder = new MediaRecorder(stream, options);
    .....
    //Before merging blobs change output mime 
    var blob = new Blob(recordedBlobs, {type: 'video/mp4'});
    // And name your file as video.mp4