0

I am trying to combine the audio with video and download the final file as video. How can I do that ? I tried following approach and throws error

enter image description here

async function mergeVideo(video, audio) {
    let { createFFmpeg, fetchFile } = FFmpeg;
    let ffmpeg = createFFmpeg();
    await ffmpeg.load();
    ffmpeg.FS('writeFile', 'video.mp4', await fetchFile(video));
    ffmpeg.FS('writeFile', 'audio.mp4', await fetchFile(audio));
    await ffmpeg.run('-i', 'video.mp4', '-i', 'audio.mp4', '-c', 'copy', 'output.mp4');
    let data = await ffmpeg.FS('readFile', 'output.mp4');
    return new Uint8Array(data.buffer);
};

(()=>{
  mergeVideo("video here","audio here");
})();
<script src='https://unpkg.com/@ffmpeg/ffmpeg@0.9.6/dist/ffmpeg.min.js'></script>
VC.One
  • 14,790
  • 4
  • 25
  • 57
Hari Das
  • 10,145
  • 7
  • 62
  • 59
  • Is your code working in Node.js? – MfyDev Jul 21 '23 at 09:25
  • You need to find a Javascript based MP4 muxer or write your own JS code. This means learning about the MP4 format and learning how to write integers (32-bits or 4 slots) into an array. From there you write the required integers (defining expected sections of the file like metadata etc), and finally add you a/v bytes. The logic is simple if just muxing a/v into MP4 but what is tedious is the multiple parts of an MP4 structure. I recommend you learn how to make a fragmented MP4 bytes structure using JS (create/write array values). – VC.One Jul 28 '23 at 08:27
  • @HariDas See if my example [write_32bit_integer](https://stackoverflow.com/a/76783166/2057709) function is useful to your starting to make an MP4 file by Javascript code... – VC.One Jul 28 '23 at 11:53

0 Answers0