I need to return mp4 video with json in one endpoint. How can i do it?
Or do I need to return json with a link to the video stream and text?
My code sample below:
from http import HTTPStatus
from pathlib import Path
import uvicorn
from fastapi import FastAPI
from fastapi import Header
from fastapi import Request, Response
from fastapi.templating import Jinja2Templates
app = FastAPI()
templates = Jinja2Templates(directory="templates")
CHUNK_SIZE = 1024*1024
video_path = Path("dataclasses_part2.mp4")
@app.get("/")
async def read_root(request: Request):
return templates.TemplateResponse("index.htm", context={"request": request})
@app.get("/video")
async def video_endpoint(range: str = Header(None)):
start, end = range.replace("bytes=", "").split("-")
start = int(start)
end = int(end) if end else start + CHUNK_SIZE
with open(video_path, "rb") as video:
# some operations with video, returning data
data_oper = "Some info"
video.seek(start)
video_data = video.read(end - start)
filesize = str(video_path.stat().st_size)
headers = {
'Content-Range': f'bytes {str(start)}-{str(end)}/{filesize}',
'Accept-Ranges': 'bytes'
}
data = {
"video_data": video_data,
"text_data": data_oper,
}
return Response(data, status_code=HTTPStatus.PARTIAL_CONTENT, headers=headers)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
Now it's falling with error, because i can do only this:
return Response(video_data, status_code=HTTPStatus.PARTIAL_CONTENT, headers=headers, media_type="video/mp4")
My html template below
<!DOCTYPE html>
<html>
<head>
<title>FastAPI video streaming</title>
</head>
<body>
<video width="1200" controls muted="muted">
<source src="http://localhost:8000/video"/>
</video>
<h1>
# Text from responce
</h1>
</body>
</html>
This topic does not answer my question How to return video frame and text to HTML page using FastAPI? - i need to return video with sound
Also i saw this Streaming video with FastAPI - but i need to return text/json too