The problem in your code lies in the path parameters start
and end
. This was creating the problem when reading the parameters from the Header
. This caused an 500 Internal Server Error
exception, which was not visible, unless navigating to /video
endpoint.
Here's two possible solutions, taken from the docs https://fastapi.tiangolo.com/advanced/custom-response/?h=streamingre#using-streamingresponse-with-file-like-objects
Option 1
FileResponse
@app.get("/video")
async def video_endpoint():
return FileResponse(video_path)
Option 2
StreamingResponse
@app.get("/video")
async def video_endpoint():
def iterfile():
with open(video_path, mode="rb") as file_like:
yield from file_like
return StreamingResponse(iterfile(), media_type="video/mp4")
This will allow you to stream the video without getting a headache of handling single chunks.
Option 2
is the suggested one, as this will asynchronously stream
the video the client's browser. This will allow to do automatically
the start
end
parameters instead of controlling them yourself.
Nevertheless, if you still want to specify a start
and an end
, you can create the endpoint with the following parameters. Reading and returning the partial file is not a simple task, because some information may be at the beginning of the file or at the end (depending on the data format). that I'm not sure how to do (nor sure if it's possible with fastapi
)
@app.get("/video")
async def video_endpoint(start: int = None, end: int = None):
if start and end:
if end - start < 0:
return
else:
if start is None:
start = 0
end = start + CHUNK_SIZE
# Read and return the file