Spaces:
Sleeping
Sleeping
File size: 1,659 Bytes
aff0a09 5ac7dcf aff0a09 155198f aff0a09 6fa32c7 aff0a09 155198f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
from fastapi import UploadFile, File, Form, BackgroundTasks
from fastapi.responses import FileResponse
from fastapi import APIRouter
from generate_subway_subtitles import generate_subway_subtitles
from endpoints.utils import remove_content_from_dir
import os
import shutil
from endpoints.utils import get_file_name
router = APIRouter()
@router.post("/subway")
async def create_subway_video(
top_video: UploadFile = File(...),
bottom_video: UploadFile = File(...),
color: str = Form(...),
size: int = Form(...),
font: str = Form(...),
credit: str = Form(...),
credit_size: int = Form(...),
background_tasks: BackgroundTasks = BackgroundTasks()
):
top_video_path = "./data/subway/videos/top_video.mp4"
bottom_video_path = "./data/subway/videos/bottom_video.mp4"
# Create the directory if it doesn't exist
if not os.path.exists("./data/subway/videos"):
os.makedirs("./data/subway/videos")
# Save the videos
with open(top_video_path, "wb") as buffer:
shutil.copyfileobj(top_video.file, buffer)
with open(bottom_video_path, "wb") as buffer:
shutil.copyfileobj(bottom_video.file, buffer)
options = {
"top_video": top_video_path,
"bottom_video": bottom_video_path,
"font_color": color,
"font_size": size,
"font_family": font,
"border_size": 1,
"credit": credit,
"credit_size": credit_size,
}
result_video_path = generate_subway_subtitles(options)
if not os.path.exists(result_video_path):
return {"message": "Error generating subtitles!"}
background_tasks.add_task(remove_content_from_dir, "./data/subway/videos")
return FileResponse(result_video_path, media_type="video/mp4", filename=get_file_name("subway"))
|