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);
}
ffmpeg-wasm does not support nodejs

import { FFmpeg } from '@ffmpeg/ffmpeg';
import { fetchFile } from '@ffmpeg/util';
import fs from 'fs';
const transcodeToMp3 = async (inputPath: string) => {
const ffmpeg = new FFmpeg();
await ffmpeg.load();
// Read the input WAV file
const inputData = await fetchFile(inputPath);
const inputFilename = 'input.wav';
await ffmpeg.writeFile(inputFilename, inputData);
// Set the output file name
const outputFilename = inputPath.replace('.wav', '.mp3');
// Transcode to MP3
await ffmpeg.exec(['-i', inputFilename, '-ar', '44100', '-ac', '2', '-b:a', '192k', outputFilename]);
// Read the output MP3 file
const data = await ffmpeg.readFile(outputFilename);
// Write the MP3 file to the disk
fs.writeFileSync(outputFilename, Buffer.from(data));
// Clean up and terminate the ffmpeg instance
await ffmpeg.terminate();
console.log(`Transcoding complete. File saved as ${outputFilename}`);
};
transcodeToMp3('./ffmpeg-transcoder/responseTest.wav');

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With