用 OpenCV + Python + Mac 写视频 [英] Writing video with OpenCV + Python + Mac
问题描述
当我尝试将帧写入视频时,我不断收到断言错误.我得到的错误是这样的:
I keep getting an assertion error when I'm trying to write frames to video. The error I'm getting is this:
Traceback (most recent call last):
File "VideoMixer.py", line 23, in <module>
cv.WriteFrame(writer, cv.LoadImage(fileName))
cv.error: dst.data == dst0.data
这是我的脚本:
import cv
import sys
files = sys.argv[1:]
for f in files:
capture = cv.CaptureFromFile(f)
height = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH)
width = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT)
fps = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS)
fourcc = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FOURCC)
print fourcc
writer = cv.CreateVideoWriter('ok.mov', int(fourcc),fps,(int(width),int(height)),1)
print writer
for i in range(30):
frame = cv.QueryFrame(capture)
print frame
if frame:
cv.WriteFrame(writer, frame)
将帧保存为图像工作正常,所以我知道捕获没有任何问题.我创造作家错了吗?'print Fourcc' 输出 0.0,但我尝试了许多 FOUR_CC 值.
Saving the frames as images works fine so I know there's nothing wrong with the capture. Am I creating the writer wrong? The 'print fourcc' outputs 0.0 but I've tried with many FOUR_CC values.
谢谢!
推荐答案
您的某些框架是否具有不同的色彩空间或深度?一些观察:
Do some of your frames have different colorspaces or depths? A few observations:
- 您在代码中交换了高度和宽度,这是故意的吗?
- 你的
fourcc
应该是一个大于 0 的整数.请看我下面的例子.
- You have swapped the height and width in your code, is that on purpose?
- Your
fourcc
should be an integer > 0. See my example below.
我没有亲自使用 OpenCV 生成 Quicktime 视频,但这对我生成未压缩的 AVI 文件有用.我使用 cv.CV_FOURCC
函数选择了 I420 Fourcc:
I haven't personally generated Quicktime video using OpenCV, but this worked for me generating an uncompressed AVI file. I choose the I420 fourcc using the cv.CV_FOURCC
function:
import cv
import sys
# standard RGB png file
path = 'stack.png'
cap = cv.CaptureFromFile(path)
fps = 24
width = int(cv.GetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_WIDTH))
height = int(cv.GetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_HEIGHT))
# uncompressed YUV 4:2:0 chroma subsampled
fourcc = cv.CV_FOURCC('I','4','2','0')
writer = cv.CreateVideoWriter('out.avi', fourcc, fps, (width, height), 1)
for i in range(90):
cv.GrabFrame(cap)
frame = cv.RetrieveFrame(cap)
cv.WriteFrame(writer, frame)
更新:VLC播放out.avi
的截图:
在 Quicktime 中:
In Quicktime:
这篇关于用 OpenCV + Python + Mac 写视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!