当我试图将帧写入视频时,我不断收到断言错误.我得到的错误是这样的:
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值.
谢谢!
解决方法
你的一些帧有不同的颜色空间或深度?一些观察:
>您已经在代码中交换了高度和宽度,这是故意的吗?
>你的fourcc应该是一个整数> 0.见下面的例子.
我没有亲自使用OpenCV生成Quicktime视频,但这对我生成了一个未压缩的AVI文件.我使用cv.CV_FOURCC函数选择I420 fourcc:
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,(width,height),1) for i in range(90): cv.GrabFrame(cap) frame = cv.RetrieveFrame(cap) cv.WriteFrame(writer,frame)
更新:VLC播放的屏幕截图.avi:
在Quicktime中: