5

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))
5
  • What OS are you on? Have you tried a different fourCC? What video I/O does opencv_version -v show? Commented Jul 12, 2017 at 7:38
  • Windows 7 professional. I have tried XVID and MJPG. Where do i run the command "opencv_version -v" Commented Jul 12, 2017 at 7:41
  • I use OpenCV on Mac/Linux and it that command is run in the Terminal - it may not be on Windows, I don't know. Commented Jul 12, 2017 at 7:44
  • @MarkSetchell I found out that my opencv version is 3.2.0 Commented Jul 12, 2017 at 8:06
  • @Aplin Do you have the XVID codec installed on your OS? Commented Jul 12, 2017 at 13:09

3 Answers 3

11

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.

Sign up to request clarification or add additional context in comments.

1 Comment

resolved this issue following your response by just changing the file extension to .mp4 (instead of .avi) - thanks!
0

A mismatch between camera's color sensors and what you specify in the function can also cause this. It probably extends to any incorrect parameter in the writer call.

out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (width, height), isColor=False)

isColor defaults to True; ran into this when trying to capture from greyscale Basler cameras

Comments

0

In my case I was saving images to a video, image_data.shape gave me (1080, 1920, 4), which I had to change to (1080, 1920, 3) using

image_data = image_data[:, :, :3]

MP4 video format does not support an alpha channel (transparency) by default.

Not directly related to the original question but thought I might put this here since I could not find this solution anywhere.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.