I have never worked with openCV, but I'm sure the problem is
cap = cv2.VideoCapture(0)
This is the 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. Sort of
cap = cv2.VideoCapture(0) if (not cap.isOpened()): print "Error"
EDIT: Just downloaded Python and OpenCV and found that the problem was in 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 manually.
source share