9

I have two files, video.mp4 and genaudio.mp3 (both same length). How can I merge the MP3 file with the MP4 using FFMPEG (or any command line program)?

This is what I have so far, but it doesn't replace the audio after spitting it out:

ffmpeg -i video.mp4 -i genaudio.mp3 -c:v copy -c:a mp3 output.mp4 -y
XantiuM
  • 93
  • 1
  • 1
  • 4

1 Answers1

15

You'll have to map the input streams:

ffmpeg -i video.mp4 -i genaudio.mp3 -map 0:v -map 1:a -c:v copy -c:a copy output.mp4 -y

When the inputs have multiple streams of a type among them, ffmpeg picks the 'best' one. For audio, that's the one with most number of channels. If those are the same, then it picks a stream from the earliest input.

(No need to re-encode the audio, so I've switched to copy)

Gyan
  • 34,439
  • 6
  • 56
  • 98
  • I would totally re encode the mp3 file with an AAC codec [See this answer](https://superuser.com/questions/370625/ffmpeg-command-to-convert-mp3-to-aac), otherwise some players won't read the sound of your video. – PaulCo Jan 25 '19 at 13:05
  • What does the `-y` option do? – Josh Desmond May 11 '20 at 15:00
  • If the output file already exists, ffmpeg will prompt the user to check if it should be overwritten. `-y` forces the overwrite. `-n` forces exit and existing file is left untouched. – Gyan May 11 '20 at 15:49
  • @Gyan what if my input is ffmpeg -i video.mp4 -i genaudio.mp3 -i image.png , how do i copy ? beacuse while copying with "-map 0:v -map 1:a -c:v copy -c:a copy output.mp4 -y" image does not appear – bipin May 13 '23 at 23:18