Deepak Prakash
Deepak Prakash

Reputation: 241

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

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

Upvotes: 24

Views: 53469

Answers (6)

Jack
Jack

Reputation: 170

You can not do that using one cmd.


1. Get the audio from video file, the audio file name is a.mp3

ffmpeg.exe -i video.mp4 a.mp3

2. Merge two audio files(audio.mp3+a.mp3=audiofinal.mp3)

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

3. Merge video file and audio file(video.mp4+audiofinal.mp3=output.mp4)

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

Upvotes: 16

Dami
Dami

Reputation: 306

I don't think extracting the audio from the video is necessary. We can just use -filter_complex amix to merge both audios:

ffmpeg -i videowithaudio.mp4 -i audiotooverlay.mp3 -filter_complex amix -map 0:v -map 0:a -map 1:a -shortest videowithbothaudios.mp4

-filter_complex amix overlays the audio from the first input file on top of audio in the second input file.

-map 0:v video stream of the first input file.

-map 0:a audio stream of the first input file.

-map 1:a audio stream of the second input file.

-shortest the length of the output is the length of the shortest input

Upvotes: 7

Nikolay Gogol
Nikolay Gogol

Reputation: 999

Use case:

  • add music to your background
  • you rendered a video, but muted some part of it, so you don't want to render it again(coz it's too long), instead you render only audio track(fast) and wanna merge it with original video.

Assuming

  • you have your video with you speech (or just audio track, whatever)
  • your music_file is not loud. Otherwise, you will not hear yourself D:

Steps:

1) Extract audio from the video

ffmpeg -i test.mp4 1.mp3

test.mp4 - your file

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

audiofinal.mp3 - audio with music

3) Delete audio from original

ffmpeg -i example.mkv -c copy -an example-nosound.mkv

example-nosound.mkv - your video without audio

4) Merge with proper audio

ffmpeg -i audiofinal.mp3 -i example-nosound.wmv -c:v copy -vcodec copy final.wmv

final.wmv - your perfect video.

Upvotes: 4

Rajath
Rajath

Reputation: 1326

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.

Upvotes: 62

Muzammil
Muzammil

Reputation: 1539

First remove the sound from video if you are not able to merge video and audio by using this command: ffmpeg -i video.mp4 -an videofinal.mp4

Upvotes: -1

jrkt
jrkt

Reputation: 2715

This is very easy with FFmpeg:

ffmpeg -i vid.mp4 -i audio.mp3 -codec:a libmp3lame -ar 44100 -ab 64k -ac 1 -q:v 1 -pix_fmt yuv420p -map 0:0 -map 1:0

Upvotes: 0

Related Questions