How can I stream multiple videos in FastAPI? With the following approach, I can only have one request open in the browser, any other request I make is not working.
async def stream_video(url):
"""
frames generator from video feed
"""
global outputFrame
while(True):
cap = cv2.VideoCapture(url)
ret, frame = cap.read()
(flag, encodedImage) = cv2.imencode(".jpg", frame)
if not flag:
continue
yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
bytearray(encodedImage) + b'\r\n')
@router.get("/{camera_id}")
async def video_feed(camera_id):
"""
return the response generated along with specific media type
"""
if cam_list.get(camera_id) is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail=f"Camera with id {camera_id} not found")
url = cam_list[camera_id]
return StreamingResponse(stream_video(url), media_type="multipart/x-mixed-replace;boundary=frame")
On browser, http://127.0.0.1:8000/endpoint1/camera1 works fine and I can see the camera feed, but if I open another tab and open http://127.0.0.1:8000/endpoint1/camera2, this one is not showing anything.