首页 > 解决方案 > OpenCV 和来自 m3u8 url 的实时视频流视频

问题描述

我正在尝试构建一个需要从流 url 读取视频的应用程序。当 OpenCV 显示图像时,我看到了一些滞后,但是使用一个很棒的 url 来测试我的应用程序的视频,结果证明问题不是 OpenCV 速度太慢而是速度太快。

如果我尝试以下代码,我可以看到视频的再现速度至少比应有的速度快 3 倍。

我的代码:

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

结果

在此处输入图像描述

应该如何

在此处输入图像描述

你知道让openCV正确读取视频的方法吗,我的意思是,我想要与视频相同的速度。

提前致谢

标签: pythonopencv

解决方案


这里是:

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

推荐阅读