python-3.xopencvcomputer-visionhttp-live-streamingstreamlink

Use OpenCV-Python on a Streamlink live stream?


I need to run OpenCV-Python image recognition on live streams pulled from Twitch, using Streamlink, without writing the stream to disk. I have all my image recognition tested and ready to go (I tested using a win32api screen capture), and I've also got Streamlink successfully pulling streams using the cli commands it offers, but I need to be able to analyze the streams one frame at a time using OpenCV in a Python script.

My question is: how would I go about analyzing each frame of the Streamlink stream with OpenCV?


Solution

  • I think this code gives you an idea. You just need to get stream by streamlink and capture by 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")
    

    Main

    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