Z
Z
zax20022019-08-17 16:45:51
Python
zax2002, 2019-08-17 16:45:51

How to make OpenCV work with Twitch?

The link comes out normal, plays through VLC, but OpenCV returns False when reading the frame. Also, everything worked with some kind of m3u8 tv stream.

streams = streamlink.streams("twitch.tv/streamer_id")
url = streams["best"].url
cap = cv2.VideoCapture(url)

while True:
  succ, frame = cap.read()
  if not succ:
    break
  pil_img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
rPman, 2019-08-17
@rPman

there is no video content at twitch.tv/streamer_id, there is html, but vlc is smart, with the help of plugins and complex logic it has a parser that pulls video from a bunch of typical videos and streaming services, but opencl does not have such logic
ps twitch hides the link well in html but it seems there is api read their docs how to pull a link to a stream

D
dodo101000101, 2019-10-28
@dodo101000101

You need to use the av library:

def get_image_from_stream(stream_url: str) -> np.ndarray:
    '''
    Get last frame from stream using av modul.
    '''
    container = None
    try:
        container = av.open(stream_url) # sadad
    except Exception:
        # quit
        raise Exception("ERROR! Stream not working!")
    
    video_stream = next(s for s in container.streams if s.type == 'video')
    
    image_pil = None
    for packet in container.demux(video_stream):
        for frame in packet.decode():
            image_pil = frame.to_image()
            
            if image_pil:
                break
        if image_pil:
            break
    image = np.asarray(image_pil)
    return image

stream_url you already have. And just run the function in an infinite loop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question