You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为FFmpeg循环拼接的视频批量添加淡入淡出效果?

解决方案

原命令仅能循环拼接视频,但无法自动给片段切换添加淡入淡出效果。以下是两种可行方案:

方案一:自动生成过渡效果(推荐,适配任意数量视频)

通过简单脚本自动读取input.txt中的视频文件,生成带过渡的FFmpeg命令,无需手动逐个指定文件。

脚本代码(适用于Linux/macOS)

#!/bin/bash

# 从input.txt提取所有视频路径
videos=()
while IFS= read -r line; do
    if [[ $line =~ file\ \'(.*)\' ]]; then
        videos+=("${BASH_REMATCH[1]}")
    fi
done < input.txt

# 构建滤镜链:统一帧率、添加片段间淡入淡出、循环序列
filter_chain="[0:v]fps=30,format=yuv420p[v0]"
last_vid="v0"

for i in "${!videos[@]}"; do
    if [ $i -eq 0 ]; then continue; fi
    current_idx=$i
    next_vid="v$current_idx"
    # 添加1秒淡入淡出过渡(可修改duration参数调整过渡时长)
    filter_chain+=";[$last_vid][$current_idx:v]xfade=transition=fade:duration=1[$next_vid]"
    last_vid=$next_vid
done

# 无限循环拼接好的视频序列
filter_chain+=";[$last_vid]loop=-1:size=1:start=0[v_final]"

# 执行FFmpeg命令
ffmpeg "${videos[@]/#/-i }" -i audio.mp3 \
-filter_complex "$filter_chain" \
-map "[v_final]" -map "$(( ${#videos[@]} )):a" \
-shortest -c:v libx264 -c:a aac output.mp4

使用说明

  1. 将脚本保存为add_transitions.sh,赋予执行权限:chmod +x add_transitions.sh
  2. 确保input.txt和脚本在同一目录,运行脚本即可生成带过渡的视频。

方案二:近似过渡(仅适合片段时长相近的场景)

若视频片段时长大致相同,可直接给循环的视频流添加周期性淡入淡出,但需手动估算过渡时间点,精度较低:

ffmpeg -stream_loop -1 -f concat -i input.txt -i audio.mp3 \
-filter_complex "[0:v]fps=30,format=yuv420p,fade=t=out:st=5:d=1,fade=t=in:st=6:d=1,fade=t=out:st=11:d=1,fade=t=in:st=12:d=1[v]" \
-map "[v]" -map 1:a -shortest output.mp4
  • 示例中假设每个片段时长约6秒,st参数为过渡开始时间,d为过渡时长,可根据实际情况调整。

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 04:01:19