Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
import cv
import cv2
cap = cv2.VideoCapture(0)
cap.set(8,100)
out = cv2.VideoWriter('/home/pi/Desktop/output.mp4',cv2.cv.CV_FOURCC('D','I','V','X'),20.0,(640,480))
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)
        out.write(frame)
        cv2.imshow('frame',frame)
        if cv2.waitKey(10) == 27: 
            break
cv2.VideoCapture(0).release()
out.release()
cv2.destoryAllWindow()

This code worked but it never stopped and no video file had been saved. Does anyone know how to solve this? Thanks a lot. xx

share|improve this question
    
Do the frames show up on the screen? Are the frames exactly 640x480 pixels? What happened when you pressed Escape? –  w-m Mar 6 '14 at 17:35
    
@w.m Sorry i just saw the comment. Yes, the frames showed up on the screen and it was exactly 640*480. When i pressed ESC, an error occurred. It displayed that cv2.VideoWriter object has no attribute 'release' –  VaFancy Mar 6 '14 at 22:19

1 Answer 1

up vote 2 down vote accepted

Both release() calls can be removed.

cv2.VideoCapture(0).release()

would call release() on a new VideoCapture, what you meant was cap.release().

For the VideoWriter, the release method doesn't exist - you don't have to care about releasing the VideoWriter or VideoCapture in Python. They will be released when their object is destroyed at the end of your program.

share|improve this answer
    
Thank you for answering me. I tried remove this line, but a new error occurs. "object has no attribute 'destroyAllWindows'" –  VaFancy Mar 6 '14 at 23:06
    
And when I used "DestroyWindow('frame')", it had the same problem. –  VaFancy Mar 6 '14 at 23:07
    
destroy, not destory –  w-m Mar 6 '14 at 23:24
    
My spelling mistake. But I typed the right version in my script while having this problem. –  VaFancy Mar 6 '14 at 23:33
    
And you corrected it to Window_S_, too? OpenCV does have a destroyAllWindows() method, and you are correct to call it there. –  w-m Mar 7 '14 at 7:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.