OSX下的opencv VideoWriter不产生任何输出
我正在尝试从OSX下的OpenCV的python包装器创建视频。我正在使用python 2.7.1,opencv
2.3.1a和该版本的opencv附带的willowgarage的python包装器。我有:
import cv,cv2
w = cv2.VideoWriter('foo.avi', cv.FOURCC('M','J','P','G'), 25, (100,100))
for i in range(100):
w.write(np.ones((100,100,3), np.uint8))
OpenCV说
WARNING: Could not create empty movie file container.
Didn't successfully update movie file.
... [ 100 repetitions]
我不确定下一步该怎么做
-
关于此主题的在线指南有很多过时且不正确-我想我几乎都尝试过。在Mac
OSX上查看VideoWriter的基于QTKit的源实现之后,我终于能够使用以下代码使VideoWriter输出有效的视频文件:fps = 15 capSize = (1028,720) # this is the size of my source video fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') # note the lower case self.vout = cv2.VideoWriter() success = self.vout.open('output.mov',fourcc,fps,capSize,True)
编写图像帧(请注意imgFrame的大小必须与上述capSize相同,否则更新将失败):
self.vout.write(imgFrame)
完成后,请确保:
vout.release() self.vout = None
这对我在Mac OS X 10.8.5(Mountain Lion)上有效:不保证有关其他平台。我希望此代码段可以节省其他人的实验时间!