简体   繁体   中英

python opencv won't write video using Video-writer object?

I am trying to save a OpenCV video stream using video writer object when the button is pressed but it saves only 5.5kb file.

def OnRecord(self, evt):
    capture = cv2.VideoCapture(0)
    if not(capture.isOpened()):
       print "Error"
    # video recorder
    fourcc = cv2.cv.CV_FOURCC(*'XVID')  
    video_writer = cv2.VideoWriter.open("output.avi", fourcc, 20, (640, 480), True)

    # record video
    while (capture.isOpened()):
        ret, frame = capture.read()
        if ret==True:
            video_writer.write(frame)
            cv2.imshow('Video', frame)
        else:
            break

def OnCancel(self, evt):
    capture.release()
    video_writer.release()
    cv2.destroyAllWindows()

what's the problem?

Note - I am on Raspberry-pi.

video_writer = cv2.VideoWriter.open("output.avi", fourcc, 20, (640, 480),True)

gives me error. I tried

video_writer = cv2.VideoWriter("output.avi", fourcc, 20, (640, 480), True)

and it works.

Also make sure you release capture and video_writer using capture.release() and video_writer.release() .

And cv2.imshow('Video', frame) wont work like this, you have to add cv2.waitKey(1) after it to make it work.

EDIT:

It maybe a problem with your device driver. Follow the instructions here and it should work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM