Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cv2.videowriter writes 0 bytes file (python) (opencv)

I am currently doing a motion detection project that records down video when motion is detected. Right now there is no error when recording the video but when i check on the video, it is 0 bytes. Any help would be very much appreciated.

This is my code:

camera = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640, 480))
like image 472
Aplin Avatar asked Mar 08 '23 15:03

Aplin


1 Answers

The problem comes when your input frame size does not match the output video.

out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640, 480))

Here you are promissing that your output video is 640,480 and it depends on your input source (if you don't resize it)

You can either hard code it (checking the frame size of the input video or stream source) or using the following code:

w = cap.get(cv2.CAP_PROP_FRAME_WIDTH);
h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT); 
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('output.mp4',fourcc, 15.0, (int(w),int(h)))

My suggestion is grabbing a frame outside the while loop and declaring the VideoWritter there with the frame width and height. Also, try changing the codec from XVID to DIVX or MJPG if that does not work.

like image 146
asehu Avatar answered Mar 15 '23 15:03

asehu