首页 > 解决方案 > 如何在一个视频中将多个视频合并到一个网格中?

问题描述

我有 36 个具有相同尺寸的视频文件,并希望将它们连接成一个具有 6x6 网格的单个视频。

理想情况下,视频的数量是非正方形的,例如 12 个,一个 4x3 的网格,因为它可以是最大的正方形。

我尝试使用 matplotlib 的动画库,但未能将两个方向的视频首先连接为行,然后连接为列。

任何帮助都会得到帮助!

标签: pythonmatplotlibffmpeg

解决方案


OP 谈到了 256 个视频的缩放问题。就此而言,我建议使用自动化,例如使用 Python。您应该生成一个命令,例如此链接中使用的命令,但要使用您的视频和正确的数量。我们可以看到这部分是会随着视频数量而变化的部分:

假设你有一个 python 中所有视频的列表(你可以手动完成,但我建议使用os.listdir这样

以同样的方式,您必须为overlay过滤器生成输入,这将取决于您的输出分辨率。假设它由widthheight变量定义。此外,在我的示例中,网格 (grid_widthgrid_width) 的视频数量是手动设置的。这是我没有资源或时间来测试的代码示例,但这应该是您工作的良好基础:

###list_videos contains the path the the videos
width = 1920
height = 1080
input_videos = ""
input_setpts = "nullsrc=size={}x{} [base];".format(width, height)
input_overlays = "[base][video0] overlay=shortest=1 [tmp0];"
grid_width = 16
grid_height = 16
for index, path_video in enumerate(list_video):
        input_videos += " -i " + path_video
        input_setpts += "[{}:v] setpts=PTS-STARTPTS, scale={}x{} [video{}];".format(index, width//grid_width, height//grid_height, index)
        if index > 0 and index < len(list_video) - 1:
            input_overlays += "[tmp{}][video{}] overlay=shortest=1:x={}:y={} [tmp{}];".format(index-1, index, width//grid_width * (index%grid_width), height//grid_height * (index//grid_width), index)
        if index == len(list_video) - 1:
            input_overlays += "[tmp{}][video{}] overlay=shortest=1:x={}:y={}".format(index-1, index, width//grid_width * (index%grid_width), height//grid_height * (index//grid_width))

complete_command = "ffmpeg" + input_videos + " -filter_complex \"" + input_setpts + input_overlays + "\" -c:v libx264 output.mp4"

print(complete_command) 

最后你可以complete_commandos.system运行,那里有资料。


推荐阅读