如何使用ffmpeg在指定时间为视频添加多段音频
Got it, let's expand on your existing FFmpeg command to handle multiple audio tracks easily. Your current command uses the concat filter to join a 30-second silent track with a single input audio—scaling this to 2, 3, or more audio clips just requires extending that concat chain correctly.
First, let's recap your original command logic
Your command does two key things:
- Generates a 30-second silent audio track with
aevalsrc=0:d=30[s1] - Uses
concatto join that silent track with your input audio ([s1][1:a]concat=n=2:v=0:a=1[aout]), wheren=2tells FFmpeg you're joining 2 audio segments
Adding 2 audio clips (plus the initial silent track)
To add a second audio file, you just need to:
- Add another
-iflag for the second audio file - Update the
concatfilter to include this new audio stream, and setn=3(since we now have 3 segments: silent + audio1 + audio2)
Here's the command:
ffmpeg -i input_video.mp4 -i audio1.mp3 -i audio2.mp3 -filter_complex " # Generate 30-second silent track aevalsrc=0:d=30[s1]; # Concatenate silent track + audio1 + audio2 [s1][1:a][2:a]concat=n=3:v=0:a=1[aout] " -c:v copy -map 0:v -map [aout] output_video.mp4
Adding 3+ audio clips
For three audio clips, follow the same pattern: add another -i for the third audio, extend the concat input list, and set n=4 (silent + audio1 + audio2 + audio3):
ffmpeg -i input_video.mp4 -i audio1.mp3 -i audio2.mp3 -i audio3.mp3 -filter_complex " aevalsrc=0:d=30[s1]; [s1][1:a][2:a][3:a]concat=n=4:v=0:a=1[aout] " -c:v copy -map 0:v -map [aout] output_video.mp4
Bonus: Mixing silent tracks between audio clips
If you want to add silence between audio clips (not just at the start), generate multiple silent tracks and include them in the concat chain. For example: audio1 → 10s silence → audio2 → 5s silence → audio3
ffmpeg -i input_video.mp4 -i audio1.mp3 -i audio2.mp3 -i audio3.mp3 -filter_complex " # Generate two different silent tracks aevalsrc=0:d=10[s1]; aevalsrc=0:d=5[s2]; # Concatenate all segments in order [1:a][s1][2:a][s2][3:a]concat=n=5:v=0:a=1[aout] " -c:v copy -map 0:v -map [aout] output_video.mp4
Critical Notes to Avoid Errors
- Matching audio specs: All audio segments (silent tracks included) must have the same sample rate, channel count, and format. If they don't, add the
aformatfilter to standardize them first. For example:# Standardize audio1 to stereo, 44.1kHz, FLTP format [1:a]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo[a1]; # Do the same for audio2 [2:a]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo[a2]; # Then concatenate [s1][a1][a2]... - Update
ninconcat: Thenvalue must always equal the number of segments you're joining. If you have 5 segments, setn=5—this is a common mistake that causes failures.
内容的提问来源于stack exchange,提问作者呂裕翔

