probaPerception probaPerception - 1 year ago 234
Python Question

Saving a video capture in python with openCV : empty video

I'm new in Python (2.7) and I try to work on video processing (with module openCv "cv2"). Starting with tutorials, I try to use the script of this tutorial : paragraph "Saving a video".
Everything works fine excepting that the video I'm saving is empty. I can find output.avi in my directory but its memory size is 0kb an, of course when I run it, no video is displayed.

After a few changes here is my code :

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'DIVX')
fourcc =*'DIVX')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

ret, frame =
if ret==True:

# write the flipped frame

if cv2.waitKey(1) & 0xFF == ord('q'):

# Release everything if job is finished

Does anyone know why it is not working properly ?

Thanks a lot.

Answer Source

I never worked with openCV, but I bet the problem is in

cap = cv2.VideoCapture(0)

This is a C version of the VideoCapture method

Maybe you can try to do the same. Something like

cap = cv2.VideoCapture(0)
if (not cap.isOpened()):
    print "Error"

EDIT: just downloaded Python and OpenCV and discovered the problem was the codec. Try to change

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


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

and select the codec by hand.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download