video - 如何连接具有不同属性的ffmpeg中的视频?

标签 video ffmpeg concatenation

我正在尝试合并一些视频,但出现时间戳错误。
我试图使它们都具有相同的尺寸、帧速率、采样率,并在没有音轨的情况下添加音轨。

ffmpeg -i input1.mp4 -y -i audio1.mp3 -c:v copy -c:a aac -shortest output1.mp4
ffmpeg -i input2.mp4 -y -i audio2.mp3 -c:v copy -c:a aac -shortest output2.mp4
ffmpeg -y -safe 0 -f concat -i list.txt -c copy output.mp4
错误信息:
Non-monotonous DTS in output stream 0:0; previous: 8052684, current: 4127401; changing to 8052685. This may result in incorrect timestamps in the output file.

最佳答案

由于我假设您的输入将是任意的,我建议使用 concat filter而不是 concat demuxer因为无论如何您都需要执行过滤以使所有内容符合一组通用参数,并且您可以在一个命令中完成所有操作。

制作所有视频 1280x720、1:1 SAR、30 fps、yuv420p
使用 scale (宽 x 高/分辨率),setsar (纵横比),fps (帧率),format (色度二次采样)和concat (连接/连接)过滤器。

ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex \
"[0:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v0];
 [1:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v1];
 [2:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v2];
 [v0][0:a][v1][1:a][v2][2:a]concat=n=3:v=1:a=1[v][a]" \
-map "[v]" -map "[a]" -c:v libx264 -c:a aac -movflags +faststart output.mp4
与上述相同,但也将音频处理为具有 48000 采样率的立体声
添加了aformat (采样率和 channel 布局)过滤器。
ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex \
"[0:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v0];
 [1:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v1];
 [2:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v2];
 [0:a]aformat=sample_rates=48000:channel_layouts=stereo[a0];
 [1:a]aformat=sample_rates=48000:channel_layouts=stereo[a1];
 [2:a]aformat=sample_rates=48000:channel_layouts=stereo[a2];
 [v0][a0][v1][a1][v2][a2]concat=n=3:v=1:a=1[v][a]" \
-map "[v]" -map "[a]" -c:v libx264 -c:a aac -movflags +faststart output.mp4
带水印
ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -i logo.png -filter_complex \
"[0:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v0];
 [1:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v1];
 [2:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v2];
 [0:a]aformat=sample_rates=48000:channel_layouts=stereo[a0];
 [1:a]aformat=sample_rates=48000:channel_layouts=stereo[a1];
 [2:a]aformat=sample_rates=48000:channel_layouts=stereo[a2];
 [v0][a0][v1][a1][v2][a2]concat=n=3:v=1:a=1[vid][a];[vid][3]overlay=W-w-5:H-h-5[v]" \
-map "[v]" -map "[a]" -c:v libx264 -c:a aac -movflags +faststart output.mp4
欲了解更多信息,请参阅 overlay filter documentationHow to add and position watermark with ffmpeg?
为没有音频的输入添加静音虚拟音频
anullsrc如果您的输入之一不包含音频,则过滤器用于提供无声的虚拟音频。这可能是必需的,因为要连接的所有段必须具有相同数量和类型的流。换句话说,您不能将没有音频的视频连接到有音频的视频。因此可以添加静音音频,如下例所示:
ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -t 0.1 -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=48000 -filter_complex \
"[0:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v0];
 [1:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v1];
 [2:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720::-1:-1,setsar=1,fps=30,format=yuv420p[v2];
 [0:a]aformat=sample_rates=48000:channel_layouts=stereo[a0];
 [2:a]aformat=sample_rates=48000:channel_layouts=stereo[a2];
 [v0][a0][v1][3:a][v2][a2]concat=n=3:v=1:a=1[v][a]" \
-map "[v]" -map "[a]" -c:v libx264 -c:a aac -movflags +faststart output.mp4
注:离开 -t 0.1原样:anullsrc 的持续时间只需要短于相关视频输入的持续时间。 concat 过滤器将自动扩展无声音频以匹配相关视频输入的长度。

关于video - 如何连接具有不同属性的ffmpeg中的视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69324419/

相关文章:

laravel - 如何在 Laravel Blade 中连接变量与数据库字段名称?

javascript - 尝试从数字列表中提取数组值,但返回的值无效数组长度

apache-flex - AS3 视频在运行时进行高压缩编码(H.264?)

javascript - 使用 'this.currentTime' 获取视频的时间并将其重置为 'hover out' 上的起点

audio - iOS - 转换音频格式(作品到 mp3)

javascript - 使用 fluent-ffmpeg 将 ffprobe 元数据返回到另一个函数

string - 通过强制转换和使用串联运算符将整数附加到字符串

perl - 用 Perl 解析 AVI 视频

asp.net-mvc - 部署到 windows azure 后 html5 视频无法工作

ruby-on-rails-4 - 如何从源代码在 Red Hat OS 中安装 FFMPEG?