Spaces:
Sleeping
Sleeping
from fastapi import UploadFile, File, Form, BackgroundTasks | |
from fastapi.responses import FileResponse | |
from fastapi import APIRouter | |
from generate_basic_subtitles import generate_basic_subtitles | |
from endpoints.utils import remove_content_from_dir | |
import os | |
import shutil | |
from endpoints.utils import get_file_name | |
router = APIRouter() | |
async def create_basic_video( | |
video: UploadFile = File(...), | |
color: str = Form(...), | |
size: int = Form(...), | |
font: str = Form(...), | |
credit: str = Form(...), | |
credit_size: int = Form(...), | |
background_tasks: BackgroundTasks = BackgroundTasks() | |
): | |
# Save the videos | |
basic_video_path = "./data/basic/videos/video.mp4" | |
# create the directory if it doesn't exist | |
if not os.path.exists("./data/basic/videos"): | |
os.makedirs("./data/basic/videos") | |
# Save the videos | |
with open(basic_video_path, "wb") as buffer: | |
shutil.copyfileobj(video.file, buffer) | |
options = { | |
"video": basic_video_path, | |
"font_color": color, | |
"font_size": size, | |
"font_family": font, | |
"border_size": 1, | |
"credit": credit, | |
"credit_size": credit_size, | |
} | |
result_video_path = generate_basic_subtitles(options) | |
if not os.path.exists(result_video_path): | |
return {"message": "Error generating subtitles!"} | |
background_tasks.add_task(remove_content_from_dir, "./data/basic/videos") | |
# return the video with the current date and time | |
return FileResponse(result_video_path, media_type="video/mp4", filename=get_file_name("basic")) | |