Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

python - opencv VideoWriter under OSX producing no output

I am trying to create a video from the python wrapper for OpenCV under OSX. I am using python 2.7.1, opencv 2.3.1a, and the python wrappers from willowgarage that come with that version of opencv. I have:

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 says

WARNING: Could not create empty movie file container.
Didn't successfully update movie file.
... [ 100 repetitions]

I'm not sure what to try next

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...