Answer the question
In order to leave comments, you need to log in
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
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
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question