修改fetch接口,实现两个位置的视频播放

This commit is contained in:
zhangkeyang 2024-05-07 16:01:07 +08:00
parent ca716a5ed1
commit 031e5d3064
3 changed files with 53 additions and 46 deletions

15
main.py
View File

@ -4,7 +4,7 @@ import uvicorn
import ffmpeg import ffmpeg
from pathlib import Path 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.responses import StreamingResponse
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
@ -37,9 +37,14 @@ async def upload_file(file: UploadFile = File(...)):
return {'status': 200, 'id': video_name} return {'status': 200, 'id': video_name}
@app.get("/fetch/{id}") @app.get("/fetch/{id}&location={location}")
async def fetch_file(id: str, range: str = Header(None)) -> StreamingResponse: 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')) 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 video_size = video_path.stat().st_size
start = 0 start = 0
end = video_size - 1 end = video_size - 1
@ -135,7 +140,9 @@ async def generate_file(id: str = Form(...)):
out.release() out.release()
cap.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} return {'status': 200, 'id': id}

View File

@ -284,7 +284,7 @@ function uploadFile(index, file) {
*/ */
function playFile(index, file, id) { function playFile(index, file, id) {
$('#video-modal').find('.modal-title').text(`预览${file ? file.name : '此视频'}的关键镜头`); $('#video-modal').find('.modal-title').text(`预览${file ? file.name : '此视频'}的关键镜头`);
$('#video-modal').find('video').attr('src', `http://${window.location.hostname}:${window.location.port}/fetch/${id}`); $('#video-modal').find('video').attr('src', `http://${window.location.hostname}:${window.location.port}/fetch/${id}&location=play`);
var player = new Plyr('video'); var player = new Plyr('video');
$('#video-modal').on('hidden.bs.modal', function(event) { $('#video-modal').on('hidden.bs.modal', function(event) {
player.pause(); player.pause();