0

How to use FFmpeg.wasm to add audio to a video in Nodejs. Like adding some background audio to a video in Nodejs? Can anyone here please help me with this? https://github.com/FFmpeg-wasm/FFmpeg.wasm

Reel3.mp4 is a video and wolf-howl-6310.mp3 is the audio that i want to merge. What i tried?

import { readFile, writeFile } from "fs/promises";
import { FFmpeg } from "@ffmpeg.wasm/main";
import path from "path"; 

const currentDirectory = process.cwd();

const inputFilePath = path.join(currentDirectory, "Reel3.mp4"); 
const outputFilePath = path.join(currentDirectory, "Reel3.mp4"); 

const ffmpeg = await FFmpeg.create({ core: "@ffmpeg.wasm/core-mt" });

try {
  await ffmpeg.fs.writeFile("Reel3", await readFile(inputFilePath));
  await ffmpeg.run("-i", "wolf-howl-6310", "wolf-howl-6310.mp3");
  await writeFile(outputFilePath, ffmpeg.fs.readFile("Reel3.mp4"));
  console.log("Conversion successful!");
} catch (error) {
  console.error("Error:", error);
} finally {
  process.exit(0);
}
James
  • 73
  • 6
  • Please show your code. What isn't working and where are you stuck? [the Readme's usage section](https://github.com/FFmpeg-wasm/FFmpeg.wasm#usage) combined with [this SO answer](https://stackoverflow.com/a/11783474/3083470) are probably your answer... – Taxel Aug 24 '23 at 12:43
  • @Taxel Please check my edited post – James Aug 24 '23 at 13:37
  • What is the behavior you're seeing? Does it throw an error? Have you tried using a different output path than the input file path? – Taxel Aug 24 '23 at 13:47
  • @Taxel getting Error [RuntimeError]: memory access out of bounds. can you please check in your machine? – James Aug 24 '23 at 14:12
  • set `outputFilePath` to another name – mortalis Aug 24 '23 at 17:48
  • Check their example first, from Github Readme. Does it work? As I understand their flow, you first write to MEMFS with `ffmpeg.fs.write`. Then execute the command with all the params as normal ffmpeg run, with input and output. Then get the result from the MEMFS to your file system. So I guess you'd have `ffmpeg.run("-i", "video.mp4", "-i", "audio.mp3", "output.mp4")`. Start from the example and move step by step to your objective. And check the correct command with ffmpeg.exe first. – mortalis Aug 24 '23 at 17:58
  • @mortalis Can you give me a example that work for you? – James Aug 25 '23 at 04:04
  • @James is your input file bigger than 1gb? If so, [you can't use ffmpeg.wasm](https://github.com/FFmpeg-wasm/FFmpeg.wasm#what-is-the-maximum-size-of-input-file) – Taxel Aug 25 '23 at 12:12
  • @Taxel its2mb size – James Aug 25 '23 at 13:53
  • Sorry, you have to compose the code yourself, I cannot install it and test right now. As I said start from the example, does it work? Then go by small steps, checking the code works at every step and understanding what it does. I'd say at least the params in the run() are incorrect. Test with PC version of ffmpeg first, combine a video with audio and get the correct command line, then separate it by spaces and put each part as argument in run(). – mortalis Aug 25 '23 at 18:44
  • @mortalis its a issue on github https://github.com/FFmpeg-wasm/FFmpeg.wasm/issues/8 – James Aug 26 '23 at 05:16

0 Answers0