修改fetch接口,实现两个位置的视频播放
This commit is contained in:
parent
ca716a5ed1
commit
031e5d3064
15
main.py
15
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:
|
||||
@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}
|
||||
|
||||
|
||||
|
|
|
@ -284,7 +284,7 @@ function uploadFile(index, file) {
|
|||
*/
|
||||
function playFile(index, file, id) {
|
||||
$('#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');
|
||||
$('#video-modal').on('hidden.bs.modal', function(event) {
|
||||
player.pause();
|
||||
|
|
Loading…
Reference in New Issue