Henry Navarro
Henry Navarro

Reputation: 953

OpenCV and live video streaming videos from m3u8 urls

I am trying to build an application which needs to read a video from a stream url. I saw several lags while OpenCV shows the images, but using a fantastic url to test my application with a video, it turns out that the problem is not OpenCV is going too slow but too fast.

If I try the following code, I can see the video is reproducing at least 3x faster that it should be.

My code:

import cv2
url="http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"
vcap = cv2.VideoCapture(url)

while(True):
    # Capture frame-by-frame
    ret, frame = vcap.read()

    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")

Result

enter image description here

How it should be

enter image description here

Do you know a way to make openCV read the video correctly, I mean, I would like the same speed that video should be reproduced.

Thanks in advance

Upvotes: 0

Views: 3677

Answers (1)

crackanddie
crackanddie

Reputation: 708

here it is:

import cv2
import time


url = "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"
vcap = cv2.VideoCapture(url)
fps = vcap.get(cv2.CAP_PROP_FPS)
wt = 1 / fps

while True:
    start_time = time.time()
    # Capture frame-by-frame
    ret, frame = vcap.read()

    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
        dt = time.time() - start_time
        if wt - dt > 0:
            time.sleep(wt - dt)
    else:
        print("Frame is None")
        break

# When everything done, release the capture
vcap.release()
cv2.destroyAllWindows()
print("Video stop")

Upvotes: 3

Related Questions