diff --git a/backend/src/routers/signvideo.py b/backend/src/routers/signvideo.py index 3608c79..2b7f480 100644 --- a/backend/src/routers/signvideo.py +++ b/backend/src/routers/signvideo.py @@ -7,7 +7,8 @@ import random import string import subprocess -from fastapi import APIRouter, Depends, FastAPI, File, UploadFile, status +from fastapi import (APIRouter, BackgroundTasks, Depends, File, UploadFile, + status) from fastapi.responses import FileResponse from fastapi_jwt_auth import AuthJWT from joblib import Memory @@ -35,11 +36,20 @@ def extract_thumbnail(video_path): router = APIRouter(prefix="/signs/{sign_id}/video") +def convert_video(video_filename): + command = ['ffmpeg', '-y', '-i', settings.DATA_PATH + "/" + video_filename + ".test", '-c:v', 'libx264', '-c:a', 'aac', '-strict', '-2', '-b:a', '192k', '-c:a', 'aac', '-strict', '-2', '-b:a', '192k', settings.DATA_PATH + "/" + video_filename] + subprocess.run(command) + + # delete the temporary file + os.remove(settings.DATA_PATH + "/" + video_filename + ".test") + + # endpoint to upload a file and save it in the data folder @router.post("/", status_code=status.HTTP_201_CREATED, response_model=SignVideoOut) async def sign_video( sign_id: int, video: UploadFile, + background_tasks: BackgroundTasks, session: AsyncSession = Depends(get_session), Authorize: AuthJWT = Depends(), ): @@ -66,12 +76,8 @@ async def sign_video( with open(settings.DATA_PATH + "/" + video.filename + ".test", 'wb') as f: f.write(video.file.read()) - command = ['ffmpeg', '-y', '-i', settings.DATA_PATH + "/" + video.filename + ".test", '-c:v', 'libx264', '-c:a', 'aac', '-strict', '-2', '-b:a', '192k', '-c:a', 'aac', '-strict', '-2', '-b:a', '192k', settings.DATA_PATH + "/" + video.filename] - process = await asyncio.create_subprocess_exec(*command) - await process.wait() - - # delete the temporary file - os.remove(settings.DATA_PATH + "/" + video.filename + ".test") + # convert the video in the background + background_tasks.add_task(convert_video, video.filename) await sign_video.save(session)