Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singler line FFMPEG cmd to Merge Video /Audio and retain both audios

Tags:

I have a project that requires merging of a video file with another audio file. The expected out put is an video file that will have both the audio from actual video and the merged audio file. The length of the output video file will be same to the size of the actual video file.

Is there a single line FFMPEG command to achieve this using copy and -map parameters ?

The video form I will be using is either flv or mp4 And the audio file format will be mp3

like image 927
Deepak Prakash Avatar asked Jul 17 '14 13:07

Deepak Prakash


People also ask

Can we merge video and audio?

After importing your videos to the media library, you can drag-n-drop the video and audio files to the video and audio track respectively. After that, you can align the video with the audio file on the timeline to merge them.


1 Answers

There can be achieved without using map also.

ffmpeg -i video.mp4 -i audio.mp3 output.mp4

In case you want the output.mp4 to stop as soon as one of the input stops (audio/video) then use

-shortest

For example: ffmpeg -i video.mp4 -i audio.mp3 -shortest output.mp4

This will make sure that the output stops as and when any one of the inputs is completed.

Since you have asked that you want to do it with map. this is how you do it:

ffmpeg -i video.mp4 -i audio.mp3 -map 0:0 -map 1:0 -shortest output.mp4

Now, since you want to retain the audio of the video file, consider you want to merge audio.mp3 and video.mp4. These are the steps:

  1. Extract audio from the video.mp4

ffmpeg -i video.mp4 1.mp3

  1. Merge both audio.mp3 and 1.mp3

ffmpeg -i audio.mp3 -i 1.mp3 -filter_complex amerge -c:a libmp3lame -q:a 4 audiofinal.mp3

  1. Remove the audio from video.mp4 (this step is not required. but just to do it properly)

ffmpeg -i video.mp4 -an videofinal.mp4

  1. Now merge audiofinal.mp3 and videofinal.mp4

ffmpeg -i videofinal.mp4 -i audiofinal.mp3 -shortest final.mp4

note: in the latest version of ffmpeg it will only prompt you to use '-strict -2' in case it does then use this:

ffmpeg -i videofinal.mp4 -i audiofinal.mp3 -shortest -strict -2 final.mp4

hope this helps.

like image 139
Rajath Avatar answered Sep 19 '22 15:09

Rajath