From 031e5d30649827e0774a175cf43d168a907a58cc Mon Sep 17 00:00:00 2001 From: zhangkeyang Date: Tue, 7 May 2024 16:01:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9fetch=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=EF=BC=8C=E5=AE=9E=E7=8E=B0=E4=B8=A4=E4=B8=AA=E4=BD=8D=E7=BD=AE?= =?UTF-8?q?=E7=9A=84=E8=A7=86=E9=A2=91=E6=92=AD=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 17 ++++++++---- static/index.html | 80 +++++++++++++++++++++++++++--------------------------- static/js/index.js | 2 +- 3 files changed, 53 insertions(+), 46 deletions(-) diff --git a/main.py b/main.py index cffe852..cd7b95a 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ import uvicorn import ffmpeg from pathlib import Path -from fastapi import FastAPI, UploadFile, Form, File, Header +from fastapi import FastAPI, UploadFile, Form, File, Header, HTTPException from fastapi.responses import StreamingResponse from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles @@ -37,9 +37,14 @@ async def upload_file(file: UploadFile = File(...)): return {'status': 200, 'id': video_name} -@app.get("/fetch/{id}") -async def fetch_file(id: str, range: str = Header(None)) -> StreamingResponse: - video_path = Path(os.path.join(ServerConfig.videos_play_path, id + '.mp4')) +@app.get("/fetch/{id}&location={location}") +async def fetch_file(id: str, location: str, range: str = Header(None)) -> StreamingResponse: + if location == "play": + video_path = Path(os.path.join(ServerConfig.videos_play_path, id + '.mp4')) + elif location == "upload": + video_path = Path(os.path.join(ServerConfig.videos_upload_path, id + '.mp4')) + else: + raise HTTPException(status_code=442, detail=f"location {location} is not defined!") video_size = video_path.stat().st_size start = 0 end = video_size - 1 @@ -135,7 +140,9 @@ async def generate_file(id: str = Form(...)): out.release() cap.release() - ffmpeg.input(target_video_path, format='mp4', vcodec='mpeg4').output(final_video_path, format='mp4', vcodec='h264').run() + ffmpeg.input(target_video_path, format='mp4', vcodec='mpeg4') \ + .output(final_video_path, format='mp4', vcodec='h264') \ + .run(overwrite_output=True) return {'status': 200, 'id': id} diff --git a/static/index.html b/static/index.html index 4fcbeaf..c5d517b 100644 --- a/static/index.html +++ b/static/index.html @@ -37,49 +37,49 @@ - - -