用于 5 个视频之间交叉淡入淡出的 FFmpeg 命令。如何管理 setpts=PTS-STARTPTS?

FFmpeg command for crossfading between 5 videos .How to manage setpts=PTS-STARTPTS?

在这里,FFmpeg 中的新功能。我正在 FFmpeg 的控制台中使用和测试。

I already done with 2 video join with cross fading with this question : I am doing for 5 videos merging with cross fade I just done 90% in merging i just need to manage setpts=PTS-STARTPTS Just look into this pls.

ffmpeg -i big_buck.mp4 -i big_buck.mp4 -i big_buck.mp4 -i big_buck.mp4 -i 
big_buck.mp4 -filter_complex "[0:v]trim=0:4,setpts=PTS-
STARTPTS,fade=out:st=4:d=1:alpha=1[1]; [1:v]trim=1:4,setpts=PTS-
STARTPTS,format=yuva420p,fade=in:st=0:d=1:alpha=1,fade=out:st=4:d=1:alpha=1[2]; 
[2:v]trim=1:4,setpts=PTS-
STARTPTS,format=yuva420p,fade=in:st=0:d=1:alpha=1,fade=out:st=4:d=1:alpha=1[3]; 
[3:v]trim=1:4,setpts=PTS-
STARTPTS,format=yuva420p,fade=in:st=0:d=1:alpha=1,fade=out:st=4:d=1:alpha=1[4]; 
[4:v]trim=1:4,setpts=PTS-STARTPTS,format=yuva420p,fade=in:st=0:d=1:alpha=1[5]; 
[1][2]overlay,format=yuv420p[12]; [12][3]overlay,format=yuv420p[123]; [4]
[5]overlay,format=yuv420p[45]; [123][45]concat=n=2 [v]" -map [v]  result.mp4

****请注意,每个输入视频 big_buck.mp4 长度为 5 秒 ****。现在在代码中看到 setpts=PTS-STARTPTS 如何在每个视频输入中管理它????

我也在各种论坛上看到过,但是我没找到!!! 谢谢

使用

ffmpeg -i big_buck.mp4 -i big_buck.mp4 -i big_buck.mp4 -i big_buck.mp4 -i 
big_buck.mp4 -filter_complex \
"[0:v]setpts=PTS-STARTPTS[v1]; \
 [1:v]format=yuva420p,fade=in:st=0:d=1:alpha=1,setpts=PTS-STARTPTS+(4/TB)[v2]; 
[2:v]format=yuva420p,fade=in:st=0:d=1:alpha=1,setpts=PTS-STARTPTS+(8/TB)[v3]; 
[3:v]format=yuva420p,fade=in:st=0:d=1:alpha=1,setpts=PTS-STARTPTS+(12/TB)[v4]; 
[4:v]format=yuva420p,fade=in:st=0:d=1:alpha=1,setpts=PTS-STARTPTS+(16/TB)[v5]; 
[v1][v2]overlay[v12]; [v12][v3]overlay[v123]; [v123][v4]overlay[v1234]; [v1234][v5]overlay,format=yuv420p[v]" \
-map [v]  result.mp4

必须修改 PTS,以便每个新剪辑在当前剪辑组合结束前 1 秒开始,即第三个剪辑应该在 8 秒开始淡入,因为前两个剪辑的组合是 9 秒(第一个剪辑 4 秒 + 过渡 1 秒 + 第二个剪辑 4 秒)。

您不需要淡出,因为下一个剪辑在顶部淡入。仅当您想要剪切时才需要 concat。


使用音频交叉淡入淡出:

ffmpeg -i big_buck.mp4 -i big_buck.mp4 -i big_buck.mp4 -i big_buck.mp4 -i 
big_buck.mp4 -filter_complex \
"[0:v]setpts=PTS-STARTPTS[v1]; \
 [1:v]format=yuva420p,fade=in:st=0:d=1:alpha=1,setpts=PTS-STARTPTS+(4/TB)[v2]; 
[2:v]format=yuva420p,fade=in:st=0:d=1:alpha=1,setpts=PTS-STARTPTS+(8/TB)[v3]; 
[3:v]format=yuva420p,fade=in:st=0:d=1:alpha=1,setpts=PTS-STARTPTS+(12/TB)[v4]; 
[4:v]format=yuva420p,fade=in:st=0:d=1:alpha=1,setpts=PTS-STARTPTS+(16/TB)[v5]; 
[v1][v2]overlay[12]; [12][v3]overlay[123]; [123][v4]overlay[1234]; [1234][v5]overlay,format=yuv420p[v]; \
[1][2]acrossfade=d=1[a12]; [a12][3]acrossfade=d=1[a123]; [a123][4]acrossfade=d=1[a];" \
-map [v] -map [a] result.mp4

基于 Gyan's ,我创建了一个方便的 Bash 脚本 video_crossfade.sh 来交叉淡化任意数量的不同持续时间的视频。

#!/bin/bash

INPUT=""
CMD="ffmpeg"
SIZE=$(find . -iname "$INPUT" | wc -l)

if (( SIZE < 2 ))
then
  echo "2 or more videos are required"
  exit 1
fi

VIDEO=""
OUT=""

i="0"
total_duration="0"
for file in $(find . -iname "$INPUT" | sort)
do
  echo $file
  CMD="$CMD -i $file"

  duration=$(ffprobe -v error -select_streams v:0 -show_entries stream=duration -of csv=p=0 "$file" | cut -d'.' -f1)

  if [[ "$i" == "0" ]]
  then
    VIDEO="[0:v]setpts=PTS-STARTPTS[v0];"
  else
    fade_start=$((total_duration-$i))
    VIDEO="${VIDEO}[${i}:v]format=yuva420p,fade=in:st=0:d=1:alpha=1,setpts=PTS-STARTPTS+(${fade_start}/TB)[v${i}];"
    if (( i < SIZE-1 ))
    then
      if (( i == 1 ))
      then
        OUT="${OUT}[v0][v1]overlay[outv1];"
      else
        OUT="${OUT}[outv$((i-1))][v${i}]overlay[outv${i}];"
      fi
    else
      if (( SIZE == 2 ))
      then
        OUT="${OUT}[v0][v1]overlay,format=yuv420p[outv]"
      else
        OUT="${OUT}[outv$((i-1))][v${i}]overlay,format=yuv420p[outv]"
      fi
    fi
  fi

  total_duration=$((total_duration+duration))

  i=$((i+1))
done

CMD="$CMD -filter_complex \"${VIDEO}${OUT}\" -c:v libx264 -map [outv] crossfade.MP4"

echo "$CMD"

bash -c "$CMD"

示例:

./video_crossfade.sh '*.MP4'

结果:

该脚本通过通配符模式获取所有视频形式,并使用 ffprobe 获取视频时长。