There are many outdated and incorrect online guides on this topic-- I think I tried almost every one. After looking at the source QTKit-based implementation of VideoWriter on Mac OSX, I was finally able to get VideoWriter to output valid video files using the following code:
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)
To write an image frame (note that the imgFrame must be the same size as capSize above or updates will fail):
self.vout.write(imgFrame)
When done be sure to:
vout.release()
self.vout = None
This works for me on Mac OS X 10.8.5 (Mountain Lion): No guarantees about other platforms. I hope this snippet saves someone else hours of experimentation!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…