简体   繁体   中英

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') 

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

This worked for me having a video with no audio.

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.

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM