如何将多个视频合并为一个视频并使用 Python 设置它们的位置

How can I combine a multiple videos into one single video and set the position of them using Python

我的问题是,我有 4 个视频,我想将它们组合成一个视频并使用 Python 一次播放它们。每个视频都设置在像全息视频一样的位置(例如顶部、底部、左侧、右侧)like this。有什么方法可以帮助我实现这个吗?我找到了一些与我的问题类似的相关资源,但我无法将其应用于我的问题。

提前致谢

您可以尝试通过将所有图像复制到一个黑框中来将它们合并在一起。这是在所有 4 个位置使用相同图像的示例:

import cv2
import numpy as np

#loads images and gets data
img = cv2.imread("img.png")
h,w,_ = img.shape    

# creates the resulting image with double the size and 3 channels 
output = np.zeros((h * 2, w * 2, 3), dtype="uint8")

# copies the image to the top left
output[0:h, 0:w] = img 
# copies the image to the top right
output[0:h, w:w * 2] = img 
# copies the image to the bottom left
output[h:h * 2, w:w * 2] = img 
# copies the image to the bottom right
output[h:h * 2, 0:w] = img 

您可以随时将 img 更改为其他内容。您也可以像这样连接它们:

top = np.hstack((img, img))
bottom = np.hstack((img, img))
result = np.vstack((top, bottom))

结果是一样的

这里是使用此代码生成的 img 示例:

但是你的图像有点不同,你需要一个旋转,而不是完全串联,而是复制一个。示例如下:

# creates the resulting image with double the size and 3 channels 
output = np.zeros((w+h+h , w + h + h, 3), dtype="uint8")

# top img
output[0:h, h:h+w] = img 
# left img (rotated 90°)
output[h:h+w, 0:h] = np.rot90(img,1) 
# right img (rotated 270°)
output[h:h + w, h + w:h +w +h] = np.rot90(img,3)  
# bottom img (rotated 180°)
output[h+w:h+w+h, h:h+w] = np.rot90(img,2) 

结果是这样的:

如果您使用黑色背景的图像,您或多或少会得到那里的效果。您可能需要使用复制参数,但基本上您会执行以下操作:

imgToCopyTo[y1:y2, x1:x2] = imgToCopyFrom

其中 y1 和 x1 是您要开始复制的左上角坐标,y2 和 x2 是您要复制到的右下角坐标。另外 y2-y1 应该有 imgToCopyFrom x2-x1 的高度和宽度(它可以大于宽度或高度但不能小于)。