Python / OpenCV 2.4.9不支持cv2.VideoWriter_fourcc,即3.x版本。如果您使用的是2.4.x:
更换
fourcc = cv2.VideoWriter_fourcc(*'XVID')
与
fourcc = cv2.cv.CV_FOURCC(*'XVID')
这里的好例子如何使用OpenCV和Python录制视频 转载以供参考:
#!/usr/bin/env python import cv2if __name__ == "__main__": # find the webcam capture = cv2.VideoCapture(0) # video recorder fourcc = cv2.cv.CV_FOURCC(*'XVID') # cv2.VideoWriter_fourcc() does not exist videoOut = cv2.VideoWriter("output.avi", fourcc, 20.0, (640, 480)) # record video while (capture.isOpened()): ret, frame = capture.read() if ret: videoOut.write(frame) cv2.imshow('Video Stream', frame) else: break # Tiny Pause key = cv2.waitKey(1) capture.release() videoOut.release() cv2.destroyAllWindows()


