How to record video using OpenCV and Python?

I reviewed the OpenCV Python example on how to use VideoCapture and VideoWriter to capture and record a video file. But I keep getting:

 OpenCV Error: Assertion failed (dst.data == dst0.data) in cvCvtColor, file /tmp/opencv-n8PM/opencv-2.4.7.1/modules/imgproc/src/color.cpp, line 4422 Traceback (most recent call last): File "examples/observer/observer.py", line 17, in <module> video_writer.write(frame) cv2.error: /tmp/opencv-n8PM/opencv-2.4.7.1/modules/imgproc/src/color.cpp:4422: error: (-215) dst.data == dst0.data in function cvCvtColor 

Purified chamber.

Here is the code:

 #!/usr/bin/env python import cv2 if __name__ == "__main__": # find the webcam capture = cv2.VideoCapture(0) # video recorder fourcc = cv2.cv.CV_FOURCC(*'XVID') # cv2.VideoWriter_fourcc() does not exist video_writer = cv2.VideoWriter("output.avi", fourcc, 20, (680, 480)) # record video while (capture.isOpened()): ret, frame = capture.read() if ret: video_writer.write(frame) cv2.imshow('Video Stream', frame) else: break capture.release() video_writer.release() cv2.destroyAllWindows() 
+8
python opencv video-recording
source share
3 answers

The size of the frames is probably incorrect:

 w=int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH )) h=int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT )) # video recorder fourcc = cv2.cv.CV_FOURCC(*'XVID') # cv2.VideoWriter_fourcc() does not exist video_writer = cv2.VideoWriter("output.avi", fourcc, 25, (w, h)) 

worked for me

+8
source share

In C ++, if you can pass -1 for the codec. You can then select the codec manually from all the codecs on your computer. Perhaps everything is the same in python, but I cannot find it in the documentation.

 video_writer = cv2.VideoWriter("output.avi", -1, 20, (680, 480)) 

Try to make sure opencv can find the XVID on your computer.

+4
source share

I had a similar problem. You should debug if the problem is with frame sizes and color depth or in the codec. Try writing an empty file to a file:

 capSize = (100, 100) # this is the size of my source video fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') out = cv2.VideoWriter('output.mov',fourcc, 1, capSize) ... out.write(125 * np.ones((100,100,3), np.uint8)) ... 
+4
source share

All Articles