连接视频,在视频之间添加黑色间隙

连接视频,在视频之间添加黑色间隙

我正在尝试连接多个媒体文件。每个文件都包含视频和音频。我需要在每个媒体文件后添加黑屏静音帧(最后一个除外)。

我是说file + gap + file + gap + file

我发现非常有用的答案这里,但它只适用于音频。

然后我改变了命令。它也能工作,但没有声音。这是命令:

ffmpeg -i video-1489396334138.webm -i video-1-1489396340932.webm -i video-1489396346168.webm -f lavfi -i "color=c=black:s=640x480:r=25" \ -filter_complex " [3]trim=duration=3.932[g0];[3]trim=duration=3.168[g1]; [0][g0][1][g1][2]concat=n=5:v=1:a=0" output.webm

我知道这是因为a=0。但是当我将其更改为 时a=1,ffmpeg 会抛出一个错误:

[Parsed_trim_0 @ 0x4448080]“Parsed_trim_0”过滤器输出垫 0(视频)和“Parsed_concat_2”过滤器输入垫 1(音频)之间的媒体类型不匹配

[AVFilterGraph @ 0x4447600] 无法创建链接 trim:0 -> concat:1

可能是因为我们的lavfi输入不包含任何音频。那么问题来了,如何解决这个问题?

答案1

您还必须添加和修剪音频片段,并添加源视频中的音频,即

添加-f lavfi -i anullsrc到输入。

修剪这些

[4]atrim=duration=3.932[a0];[4]atrim=duration=3.168[a1];

并将所有音频添加到 concat 过滤器。

[0:v][0:a][g0][a0][1:v][1:a][g1][a1][2:v][2:a]concat=n=5:v=1:a=1

答案2

如何将视频连接起来,并在图像上叠加每个视频,并在每个视频之间设置 4 秒钟的静默间歇pic.jpg

#Create a silence.mp4 file with audio silence over any given image: 
#note this is all one line with the backslashes removed.
ffmpeg -loop 1 -i /home/youruser/pic.jpg -f lavfi \
    -i anullsrc=channel_layout=5.1:sample_rate=48000 -t 4 \
    -c:v libx264 -t 4 -pix_fmt yuv420p -vf scale=480:320 -y silence.mp4

#transform silence.mp4 to silence.ts so that its type is 
#fluid and conforms to the original
ffmpeg -i silence.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts silence.ts

#loop over all the mp4 files numerically making sure '10' doesn't come before '2'
for f in $(ls ./*.mp4 | sort -V)
do
    #Transform all the given mp4 files to ts files.
    ffmpeg -i $f -c copy -bsf:v h264_mp4toannexb -f mpegts $f.ts
    if [ $counter -eq 0 ]; then
        all_transport_files="$(basename $f.ts)"
    else 
        #inject the silence in between the previous and next
        all_transport_files="$all_transport_files|silence.ts|$(basename $f.ts)"
    fi
    #keep track of which one we're on, first is not like the others
    ((counter++))
    echo $counter
done

#final concate string with all videos in order, with silence between
concat_and_all_files="concat:$all_transport_files"

#Join all the ts files back together into output.mp4
ffmpeg -async 1 -i "$concat_and_all_files" -c copy -bsf:a aac_adtstoasc output.mp4

瞧,您的所有视频文件都被静音分隔开。如果这对您不起作用,那么您需要确保所有视频(包括 silent.mp4)的 channel_layout、sample_rate、帧速率和其他属性都相同。音频和视频编解码器地狱是那些试图从视频领域消除竞争对手的人专有混淆和滥用的垃圾场。

答案3

根据之前的答案,我运行了这一行:

ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -f lavfi -i anullsrc -f lavfi -i "color=c=black:s=1920x1080:r=30" -filter_complex \
       "[3]atrim=duration=1[ga1];[3]atrim=duration=1[ga2];[4]trim=duration=1[gv1];[4]trim=duration=1[gv2];
        [0:v][0:a][gv1][ga1][1:v][1:a][gv2][ga2][2:v][2:a]concat=n=5:v=1:a=1" -strict -2 out.mp4

由于我的输入文件,我必须添加额外的 -strict -2,正如 ffmpeg 的警告所建议的那样。

由于我需要它来制作许多视频,因此我编写了调用 ffmpeg 的 Python 脚本: https://gist.github.com/marc-moreaux/87115abc57c36eb5a8bb46bc77e8a2ce

相关内容