fluent-ffmpeg / node-fluent-ffmpeg

A fluent API to FFMPEG (http://www.ffmpeg.org)
MIT License
7.97k stars 881 forks source link

How to replace audio from existing video with audio using command?? #1254

Open handita opened 9 months ago

handita commented 9 months ago

Sory I'm newbie in this library..

I have read several resources but it's still not working

I want to replace audio from current video with different audio with this command

const ffmpeg = require("fluent-ffmpeg");

const inputVideo =
  "C:\\Users\\user\\AppData\\Roaming\\envidi\\Envidi\\videos\\86.mp4";
const inputAudio = "C:\\Users\\user\\Downloads\\Music\\1.mp3";
const outputFile = "output2.mp4";
const outputFile2 = "output3.mp4";

ffmpeg(inputVideo)
  .addInput(inputAudio)
  .output(outputFile)
  .on("end", () => {
    console.log('end')
  })
  .on("error", (err) => console.error("Error:", err))
  .run();

But the output file still not having audio, it's like with parameter .noAudio()

When I running using command in terminal or cmd it works perfectly

ffmpeg -i "C:\Users\user\AppData\Roaming\envidi\Envidi\videos\86.mp4" -i "C:\Users\user\Downloads\Music\aa.mp3" -c:v copy -c:a aac -map 0:v -map 1:a "output2.mp4"

I also using this

const ffmpeg = require('fluent-ffmpeg');

const inputVideoPath = "C:\\Users\\user\\AppData\\Roaming\\envidi\\Envidi\\videos\\86.mp4";
const inputAudioPath = "C:\\Users\\user\\Downloads\\Music\\aa.mp3";
const outputFilePath = "output2.mp4";

ffmpeg()
  .input(inputVideoPath)
  .input(inputAudioPath)
  .videoCodec('copy')
  .audioCodec('aac')
  .outputOptions('-map 0:v')
  .outputOptions('-map 1:a')
  .save(outputFilePath)
  .on('end', () => {
    console.log('Conversion finished!');
  })
  .on('error', (err) => {
    console.error('Error:', err);
  });

But still audio is gone, video is no sound in the output file

Help me the correct function to run with this command, Thanks in advance