Advertisement

fastapi加快视频播放速度

阅读量:

从库中导入FastAPI框架及其相关的Request、HTTPException和Query组件。
从响应模块中导入StreamingResponse。
导入os模块。
导入aiofiles模块。

app = FastAPI()

@app.get("/video")
async def stream_video(request: Request, name: str = Query(default=None, description="Name of the video file")):
file_path = f"videos/{name}"

if check whether the file exists at the given path:
raise HTTPException(status_code=404, detail='the video is not located')

通过os.path模块获取文件的大小。
从请求头中提取范围信息。
解析范围信息并获取起始和结束位置。

async def iterfile(file_path, start integer, end integer):
async with aiofiles.open(file_path, mode='rb') as an

全部评论 (0)

还没有任何评论哟~