Video saved by opencv is zero bytes

asked 2016-07-12 10:16:54 -0600

JBR gravatar image

HI I followed the getting started guide and got the following code now:

import numpy as np
import cv2

cap = cv2.VideoCapture(0) #initialize the camera
out = cv2.VideoWriter('output.avi',-1, 20.0, (640,480)) #Define the codec and create VideoWriter object

while(cap.isOpened()):
    ret, frame = cap.read() #read frame
    if ret == True:
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        out.write(frame) #write frame to output 
        cv2.imshow('frame',frame) #show frame
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

the problem is that as soon as i hit the q button and try to watch my saved file i cant ebcasue it is 0 bytes big. WHy is that? Everything else works, i can see the video in the video frame but it simply doesnt save it.

Im using anaconda with python 2 and opencv 2 as well (newest of each).

thanks

edit retag flag offensive close merge delete

Comments

You're passing in -1 as the FOURCC. Are you getting the window allowing you to select the codec?

Try using cv2.VideoWriter.fourcc('X','V','I','D') instead and try that. (Is that how Python static functions work?) You should probably look up the right way to do that instead of just copying that.

Tetragramm gravatar imageTetragramm ( 2016-07-12 19:07:35 -0600 )edit

Also, be advised that some codecs will simply not write anything to file (for instance, most codecs on OSX will result in empty files). It wont throw an error, just not write anything to file.

B4silio gravatar imageB4silio ( 2016-07-13 14:03:39 -0600 )edit