Search code examples
ffmpegfluent-ffmpegffmpeg-php

How to add multiple audio to a video at specific time for specific duration


I have a video.mp4 file of 20 seconds without audio stream, and I have 2 audio files audio1.mp3 and audio2.mp3. What I want is audio1 should start playe at 3 seconds for 5 seconds, and audio 2 should start play at 10 seconds for 6 seconds.

ffmpeg -y -i video1.mp4 -i audio1.mp3 -i audio2.mp3 -filter_complex "[1]adelay=3000|3000[aud1];[2]adelay=10000|10000[aud2];[aud1][aud2]amix=2,apad[a];
[0][a]amix=duration=first[a]" -map 0:v -map "[a]" -c:v copy -ac 2 output.mp4

what I'm trying to achieve is the final output will be a 20 seconds video where audio1 will start palying at 3 seconds to 8 seconds and audio2 will start from 10 seconds to 16 seconds.


Solution

  • Use atrim to limit the audio duration for the MP3s and the final output.

    ffmpeg -y -i video1.mp4 -i audio1.mp3 -i audio2.mp3 -filter_complex "[1]atrim=0:5,adelay=3000|3000[aud1];[2]atrim=0:6,adelay=10000|10000[aud2];[aud1][aud2]amix=2,apad,atrim=0:20[a]" -map 0:v -map "[a]" -c:v copy -ac 2 output.mp4