Saving a video capture in python with openCV : empty video

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 http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture

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))

for

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

and select the codec by hand.