python - 如何在一个视频中将多个视频合并到一个网格中?
问题描述
我有 36 个具有相同尺寸的视频文件,并希望将它们连接成一个具有 6x6 网格的单个视频。
理想情况下,视频的数量是非正方形的,例如 12 个,一个 4x3 的网格,因为它可以是最大的正方形。
我尝试使用 matplotlib 的动画库,但未能将两个方向的视频首先连接为行,然后连接为列。
任何帮助都会得到帮助!
解决方案
OP 谈到了 256 个视频的缩放问题。就此而言,我建议使用自动化,例如使用 Python。您应该生成一个命令,例如此链接中使用的命令,但要使用您的视频和正确的数量。我们可以看到这部分是会随着视频数量而变化的部分:
假设你有一个 python 中所有视频的列表(你可以手动完成,但我建议使用os.listdir
这样的)
以同样的方式,您必须为overlay
过滤器生成输入,这将取决于您的输出分辨率。假设它由width
和height
变量定义。此外,在我的示例中,网格 (grid_width
和grid_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)
推荐阅读
- javascript - 如何在 Internet Explorer 11 中获得元素脚本的垂直反向顺序
- html - 如何解决轮播的图像路径问题
- mysql - Express 重定向错误:使用 NodeJS 和 MySQL 发送后无法设置标头
- javascript - WordPress wp_localize_script:JS脚本无法识别PHP变量
- sql - 如果另一列为空,则两列是唯一的
- c# - 通过鼠标拖动 Unity 3D 左右移动球
- webgl2 - WebGL2 中具有自定义大小的 mipmap 问题
- c - 使用 qsort() 对数组进行排序
- python - How to get a mapping from high to low resolution ndarray in numpy
- javascript - 如何在我的服务器中修复“POST http://localhost:9001/demos/dashboard/upload.php 401(未经授权)”