在 Streamlink 直播流上使用 OpenCV-Python?

问题描述

我需要使用 Streamlink 在从 Twitch 拉取的实时流上运行 OpenCV-Python 图像识别,而不将流写入磁盘。我已经测试了所有图像识别并准备好了(我使用 win32api 屏幕截图进行了测试),并且我还使用 Streamlink 提供的 cli 命令成功地拉取了流,但我需要能够分析一帧的流一次在 Python 脚本中使用 OpenCV。

我的问题是:我将如何使用 OpenCV 分析 Streamlink 流的每一帧?

解决方法

我认为这段代码给了你一个想法。你只需要通过streamlink获取流并通过openCV捕获

def stream_to_url(url,quality='best'):
    streams = streamlink.streams(url)
    if streams:
        return streams[quality].to_url()
    else:
        raise ValueError("No steams were available")

主要

def main(url,quality='best',fps=30.0):
    stream_url = stream_to_url(url,quality)
    cap = cv2.VideoCapture(stream_url)

    frame_time = int((1.0 / fps) * 1000.0)

    while True:
        try:
            ret,frame = cap.read()

https://github.com/streamlink/streamlink/blob/6a30ed7524eff2cdb205e024294b187cb660e4e3/examples/opencv-face.py#L40