Writing an mp4 video using python opencv

前端 未结 10 681
Happy的楠姐
Happy的楠姐 2021-01-31 07:44

I want to capture video from a webcam and save it to an mp4 file using opencv. I found example code on stackoverflow (below) that works great. The only hitch is that I\'m trying

10条回答
  •  执笔经年
    2021-01-31 08:33

    What worked for me was to make sure the input 'frame' size is equal to output video's size (in this case, (680, 480) ).

    http://answers.opencv.org/question/27902/how-to-record-video-using-opencv-and-python/

    Here is my working code (Mac OSX Sierra 10.12.6):

    cap = cv2.VideoCapture(0)
    cap.set(3,640)
    cap.set(4,480)
    
    fourcc = cv2.VideoWriter_fourcc(*'MP4V')
    out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640,480))
    
    while(True):
        ret, frame = cap.read()
        out.write(frame)
        cv2.imshow('frame', frame)
        c = cv2.waitKey(1)
        if c & 0xFF == ord('q'):
            break
    
    cap.release()
    out.release()
    cv2.destroyAllWindows()
    

    Note: I installed openh264 as suggested by @10SecTom but I'm not sure if that was relevant to the problem.

    Just in case:

    brew install openh264
    

提交回复
热议问题