OpenCV load video from url

前端 未结 2 1843
礼貌的吻别
礼貌的吻别 2020-12-10 13:19

I have a video file (i.e. https://www.example.com/myvideo.mp4) and need to load it with OpenCV.

Doing the equivalent with an image is fairly trivial:

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-10 13:58

    It seems that cv2.videocode is not a valid OpenCV API either in OpenCV 2.x or OpenCV 3.x.

    Below is a sample code it works in OpenCV 3 which uses cv2.VideoCapture class.

    import numpy as np
    import cv2
    
    # Open a sample video available in sample-videos
    vcap = cv2.VideoCapture('https://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_2mb.mp4')
    #if not vcap.isOpened():
    #    print "File Cannot be Opened"
    
    while(True):
        # Capture frame-by-frame
        ret, frame = vcap.read()
        #print cap.isOpened(), ret
        if frame is not None:
            # Display the resulting frame
            cv2.imshow('frame',frame)
            # Press q to close the video windows before it ends if you want
            if cv2.waitKey(22) & 0xFF == ord('q'):
                break
        else:
            print "Frame is None"
            break
    
    # When everything done, release the capture
    vcap.release()
    cv2.destroyAllWindows()
    print "Video stop"
    

    You may check this Getting Started with Videos tutorial for more information.

    Hope this help.

提交回复
热议问题