Search code examples
pythonpython-2.7opencvvideo-processingopencv3.0

Saving videos doesnt work in opencv2-python


I am trying to run this example code for saving a video:

cap = cv2.VideoCapture('input.mp4')

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

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        out.write(frame)
        cv2.imshow('frame',frame)
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

It shows the video without problems, but 'output.avi' is only a 100kb file that cannot be opened with any video player. I tried changing the codec from *'XVID'. I tried at least a 100 different codec types from http://www.fourcc.org/codecs.php with the same result. I ran out of ideas unfortunately. How can i solve this issue ?

Thanks in advance.


Solution

  • For OpenCV 2.4.X, you’ll need to change cv2.VideoWriter_fourcc(*'XVID')

    function to cv2.cv.FOURCC(*'XVID') .