Zack Lee
Zack Lee

Reputation: 3044

How to add multiple audio files to specific time in a video using ffmpeg?

I'm trying to add 2 audio files each at 10 and 15 seconds after the video playback.

I used the following command, but the output video plays both audio files at the same time after 10 seconds:

ffmpeg -y -i video.mp4 \ 
-itsoffset 10 -i audio1.mp3 \
-itsoffset 15 -i audio2.mp3 \
-filter_complex amix -map 0:v -map 1:a -map 2:a \
-c:v copy -c:a aac -strict experimental output.mp4

How can I make the 2 files play at 10 and 15 seconds each after the video playback?

Upvotes: 2

Views: 2046

Answers (3)

Nunatic
Nunatic

Reputation: 11

Abdullah Nouraldaim's answer above is working great for me if I want to keep the original sound from the video.

But in case you want to remove sound from the original video, you can run the modified version below!

ffmpeg -y -an -i video.mp4 -itsoffset 10 -i note1.mp3 -itsoffset 15 -i note2.mp3 -itsoffset 90.7 -i note3.mp3 -itsoffset 120.58 -i note4.mp3 -filter_complex amix=inputs=4[a] -map 0:v -map [a] -c:v copy -async 1 -c:a aac output.mp4

Modifications:

  1. add -an before video.mp4 to remove its audio stream
  2. change amix=inputs=5[a] to amix=inputs=4[a] because after removing audio stream from our video.mp4, there are now only 4 audio streams.

Upvotes: 1

Abdullah Nouraldaim
Abdullah Nouraldaim

Reputation: 31

yes the solution is using -async 1 I have added a general formula when adding N audio to video use amix filter with inputs=N+1

example: I have added 4 audios to one video

ffmpeg -y -i video.mp4 -itsoffset 10 -i note1.mp3 -itsoffset 15 -i note2.mp3 -itsoffset 90.7 -i note3.mp3 -itsoffset 120.58 -i note4.mp3 -filter_complex amix=inputs=5[a] -map 0:v -map [a] -c:v copy -async 1 -c:a aac output.mp4

Upvotes: 3

Zack Lee
Zack Lee

Reputation: 3044

I found the solution. I had to add -async 1 to the command:

ffmpeg -y -i video.mp4 \ 
-itsoffset 10 -i audio1.mp3 \
-itsoffset 15 -i audio2.mp3 \
-filter_complex amix -map 0:v -map 1:a -map 2:a \
-c:v copy -async 1 -c:a aac -strict experimental output.mp4

Now it works fine!

Upvotes: 1

Related Questions