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