Nane
Nane

Reputation: 423

How to add audio to video with node fluent-ffmpeg?

I am trying to add audio on top video using ffmpeg

ffmpeg -i sample.mp4 -i sample.mp3 -c copy -map 0:v:0 -map 1:a:0 output.mp4

Above code works in my terminal but I would like to use this via ffmpeg node wrapper fluent-ffmpeg

This code is not working and it also not throwing any error

ffmpeg()
    .input('DATA/sample.mp4')
    .input('DATA/sample.mp3')
    .videoCodec('copy')
    .outputOptions(['-map 0:v:0', '-map 1:a:0'])
    // .outputOption('-map 1:a:0')
    .save('node.mp4') 

Upvotes: 3

Views: 5961

Answers (3)

CijoDojo
CijoDojo

Reputation: 21

TO pass the output in other function:-

ffmpeg()
  .addInput(videoInput) //your video file input path
  .addInput(audioInput) //your audio file input path
  .output(output) //your output path
  .outputOptions(['-map 0:v', '-map 1:a', '-c:v copy', '-shortest'])
  .on('start', (command) => {
    console.log('TCL: command -> command', command)
  })
  .on('error', (error) => console.log("errrrr",error))
  .on('end',()=>console.log("Completed"))
  .run()  

This command will also adjust if you have a video duration less than the audio duration, (-shortest) this flag will pick the file with less duration

TO save the file:-

 ffmpeg()
  .addInput(videoInput) //your video file input path
  .addInput(audioInput) //your audio file input path
  .output(output) //your output path
  .outputOptions(['-map 0:v', '-map 1:a', '-c:v copy', '-shortest'])
  .saveToFile(destination, "./temp")

Upvotes: 2

Alex
Alex

Reputation: 401

MergeAdd and mergeToFile actually have misleading names. They are used to concatenate videos into a single video file.

You should use addInput to add your audio file and then use saveToFile to save the merged output.

Upvotes: 0

Dürrani
Dürrani

Reputation: 1266

new FFmpeg()
    .addInput(video)
    .addInput(audio)
    .saveToFile(destination, "./temp");

This worked for me having a video with no audio.

Upvotes: 3

Related Questions