如何使用 OpenCV 将数组(帧)列表保存到视频中?
How do I save a list of arrays (frames) to a video using OpenCV?
我尝试使用以下代码将数组列表保存到视频中,但它不起作用。
out = cv2.VideoWriter("output.mp4", cv2.VideoWriter_fourcc(*'mp4v'), 30, (1280, 720))
for frame in frames:
out.write(frame) # frame is a numpy.ndarray with shape (1280, 720, 3)
out.release()
它失败了,没有错误消息,但输出文件 (output.mp4) 大约有 200 字节长,并且无法在 QuickTime 或 VLC 中打开。我的假设是 out.write
不知何故悄无声息地失败了。是否可以通过这种方式将数组写入视频?如果不是,那怎么办?
非常感谢任何帮助。
Numpy 数组是(行,列),但 OpenCV 按(宽度,高度)定义图像。因此,在您的 numpy 数组 height=row=1080
和 width=column=720
中。所以,
将帧大小 (1080,720) 更改为 (720,1080)。
out = cv2.VideoWriter("output.mp4", cv2.VideoWriter_fourcc(*'mp4v'), 30, (720, 1080))
for frame in frames:
out.write(frame) # frame is a numpy.ndarray with shape (1280, 720, 3)
out.release()
我尝试使用以下代码将数组列表保存到视频中,但它不起作用。
out = cv2.VideoWriter("output.mp4", cv2.VideoWriter_fourcc(*'mp4v'), 30, (1280, 720))
for frame in frames:
out.write(frame) # frame is a numpy.ndarray with shape (1280, 720, 3)
out.release()
它失败了,没有错误消息,但输出文件 (output.mp4) 大约有 200 字节长,并且无法在 QuickTime 或 VLC 中打开。我的假设是 out.write
不知何故悄无声息地失败了。是否可以通过这种方式将数组写入视频?如果不是,那怎么办?
非常感谢任何帮助。
Numpy 数组是(行,列),但 OpenCV 按(宽度,高度)定义图像。因此,在您的 numpy 数组 height=row=1080
和 width=column=720
中。所以,
将帧大小 (1080,720) 更改为 (720,1080)。
out = cv2.VideoWriter("output.mp4", cv2.VideoWriter_fourcc(*'mp4v'), 30, (720, 1080))
for frame in frames:
out.write(frame) # frame is a numpy.ndarray with shape (1280, 720, 3)
out.release()