16

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:

imgReq = requests.get("https://www.example.com/myimage.jpg")
imageBytes = np.asarray(bytearray(data), dtype=np.uint8)
loadedImage = cv2.imdecode(image, cv2.IMREAD_COLOR)

I would like to do something similar to the following (where loadedVideo will be similar to what OpenCV returns from cv2.VideoCapture):

videoReq = requests.get("https://www.example.com/myimage.mp4")
videoBytes = np.asarray(bytearray(data), dtype=np.uint8)
loadedVideo = cv2.videodecode(image, cv2.IMREAD_COLOR)

But cv2.videodecode does not exist. Any ideas?


Edit: Seeing as this may be a dead end with only OpenCV, I'm open for solutions that combine other imaging libraries before loading into OpenCV...if such a solution exists.

abagshaw
  • 6,162
  • 4
  • 38
  • 76
  • if you download the video to your hard disk and try VideoCapture there, does it work? – Micka May 05 '18 at 05:35
  • Yes it would, but I can't imagine that's the most efficient method as it introduces disk IO into the problem rather than keeping everything in memory. I'm looking for the lowest latency possible here. – abagshaw May 05 '18 at 05:39
  • no, it was just a test to reduce error sources (like mossing codecs) – Micka May 05 '18 at 05:46
  • Do you want to hold the entire video in memory or do you want it to act as a streaming video? – zindarod May 08 '18 at 21:21
  • I'd like to hold the entire video in memory. – abagshaw May 08 '18 at 21:29
  • OpenCV currently does not support decoding video from memory. If you can tinker with source code, then see [this answer](https://stackoverflow.com/a/10918972/2286337). – zindarod May 08 '18 at 22:36

2 Answers2

21

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.

thewaywewere
  • 8,128
  • 11
  • 41
  • 46
  • 2
    It's not working in ubuntu 16.04. How can I stream directly on OpenCV? I have installed OpenCV using the pip install opencv-python==3.4.5 or also tried to version 4. – Akash Panchal Jan 30 '19 at 11:13
  • @Akash Did you check the url? The one mentioned above in the answer is broken. – abggcv Jun 20 '19 at 00:24
  • @abggcv, I have found the solution. The HTTPS URL is not working directly in Ubuntu due to SSL issue. Might work when building OpenCV with OpenSSL FLAG. In my case, I have changed the URL HTTPS to HTTP it's working perfectly. – Akash Panchal Jun 20 '19 at 07:29
  • I know that this is out of the scope of the original question but would You know how to read the audio stream at the same time if one is available? – Kaszanas Feb 09 '21 at 10:58
  • @Kaszanas you should open a new question and provide more information of your use case for getting answer. Good luck. – thewaywewere Feb 10 '21 at 07:30
3

You will have to read the video using VideoCapture. there is no other way around that for now. unless you define it yourself.

remember a video is a combination of images changing at defined frame rate.

So You can read each frame in a while loop. as you apply the imdecode function.

import numpy as np
import cv2

cap = cv2.VideoCapture('https://www.example.com/myimage.mp4')

while(cap.isOpened()):
    ret, image = cap.read()    
    loadedImage = cv2.imdecode(image, cv2.IMREAD_COLOR)
    cv2.imshow('frame',loadedImage)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
kishea
  • 637
  • 1
  • 6
  • 17