You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

FFmpeg合并视频添加背景音乐求助:如何用amerge与loop实现

Adding Background Music to Your Merged FFmpeg Video

Alright, let's get that background music added to your merged video using FFmpeg's amerge and loop filters. I'll tailor this to fit right into your existing workflow since you already have splicing, watermarking, and intro/outro handling sorted.

Full Command Example

Here's a complete command that combines your existing video拼接, adds looping background music, and merges the audio tracks:

/usr/bin/ffmpeg \
  -i "/home/demo/intro.mp4" \
  -i "/home/main/1515b7e4471944edce419f8e24f5c1a8::13.mp4" \
  -i "/home/main/2b011d97e633049ec3c4605470cd8306::8.mp4" \
  -i "/home/main/5ee6a1869d4f072a796b9d10b1426c2e::14.mp4" \
  -i "/home/main/6a145097d70ce2e95..." \ # Add your remaining video files here
  -i "/path/to/your/background-music.mp3" \ # Replace with your BGM file path
  -filter_complex "
    # Step 1: Concatenate all videos (including intro)
    [0:v][0:a][1:v][1:a][2:v][2:a][3:v][3:a][4:v][4:a]concat=n=5:v=1:a=1[merged_v][merged_a];
    # Step 2: Loop the background music to match the merged video's duration
    [5:a]loop=loop=-1:size=0:start=0[looped_bgm];
    # Step 3: Merge original video audio with looped BGM (adjust volume as needed)
    [merged_a][looped_bgm]amerge=inputs=2,volume=0.8[final_a];
    # Optional: Add your watermark filter here (example below)
    # [merged_v]overlay=10:10:enable='between(t,0,30)'[watermarked_v];
  " \
  -map "[merged_v]" \ # Use [watermarked_v] instead if you added the watermark
  -map "[final_a]" \
  -c:v libx264 \
  -c:a aac \
  "/path/to/your/output-final.mp4"

Breakdown of Key Parts

Let's walk through the critical bits so you understand what's happening:

  • Video Concatenation: The concat filter stitches all your input videos (including the intro) into a single video stream ([merged_v]) and audio stream ([merged_a]). Update n=5 to match the total number of video files you're combining.
  • Looping BGM: The loop filter takes your background music stream ([5:a], since it's the 6th input file, indexed from 0) and loops it infinitely. This ensures the music plays for the entire duration of your merged video, even if the BGM file is shorter.
  • Audio Merging: amerge combines the original video audio with the looped BGM into one track. The volume=0.8 parameter prevents clipping—tweak this value (0.1 to 1.0) to get the right balance between your video's original audio and the background music.

Important Notes

  • If you don't want the original video audio: Skip the amerge step and just use [looped_bgm] as your final_a stream.
  • Audio Format Consistency: If you get errors about mismatched audio formats, add the aformat filter to standardize both audio streams before merging:
    [merged_a]aformat=sample_rates=44100:channel_layouts=stereo[formatted_a];
    [looped_bgm]aformat=sample_rates=44100:channel_layouts=stereo[formatted_bgm];
    [formatted_a][formatted_bgm]amerge=inputs=2,volume=0.8[final_a];
    
  • Watermark Integration: If you want to keep your existing watermark, uncomment and adjust the overlay filter line in the filter_complex block, then map [watermarked_v] instead of [merged_v].

内容的提问来源于stack exchange,提问作者Muhammad Sanaullah

火山引擎 最新活动