Skip to content

Commit

Permalink
feat: add /api/v1/get_bgm_list and /api/v1/upload_bgm_file
Browse files Browse the repository at this point in the history
  • Loading branch information
normal-ai committed Apr 4, 2024
1 parent 28199c9 commit 6d8911f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
54 changes: 52 additions & 2 deletions app/controllers/v1/video.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from fastapi import Request, Depends, Path, BackgroundTasks
import os
import glob
from fastapi import Request, Depends, Path, BackgroundTasks, UploadFile
from fastapi.params import File
from loguru import logger

from app.config import config
from app.controllers import base
from app.controllers.v1.base import new_router
from app.models.exception import HttpException
from app.models.schema import TaskVideoRequest, TaskQueryResponse, TaskResponse, TaskQueryRequest
from app.models.schema import TaskVideoRequest, TaskQueryResponse, TaskResponse, TaskQueryRequest, BgmListResponse, \
BgmUploadResponse
from app.services import task as tm
from app.services import state as sm
from app.utils import utils
Expand Down Expand Up @@ -52,6 +56,52 @@ def get_task(request: Request, task_id: str = Path(..., description="Task ID"),
uri_path = v.replace(task_dir, "tasks")
urls.append(f"{endpoint}/{uri_path}")
task["videos"] = urls
if "combined_videos" in task:
combined_videos = task["combined_videos"]
task_dir = utils.task_dir()
urls = []
for v in combined_videos:
uri_path = v.replace(task_dir, "tasks")
urls.append(f"{endpoint}/{uri_path}")
task["combined_videos"] = urls
return utils.get_response(200, task)

raise HttpException(task_id=task_id, status_code=404, message=f"{request_id}: task not found")


@router.get("/get_bgm_list", response_model=BgmListResponse, summary="get local bgm file list")
def get_bgm_list(request: Request):
suffix = "*.mp3"
song_dir = utils.song_dir()
files = glob.glob(os.path.join(song_dir, suffix))
bgm_list = []
for file in files:
bgm_list.append({
"filename": os.path.basename(file),
"size": os.path.getsize(file),
"filepath": file,
})
response = {
"bgm_list": bgm_list
}
return utils.get_response(200, response)


@router.post("/upload_bgm_file", response_model=BgmUploadResponse, summary="upload bgm file to songs directory")
def upload_bgm_file(request: Request, file: UploadFile = File(...)):
request_id = base.get_task_id(request)
# check file ext
if file.filename.endswith('mp3'):
song_dir = utils.song_dir()
save_path = os.path.join(song_dir, file.filename)
# save file
with open(save_path, "wb+") as buffer:
# If the file already exists, it will be overwritten
file.file.seek(0)
buffer.write(file.file.read())
response = {
"uploaded_path": save_path
}
return utils.get_response(200, response)

raise HttpException('', status_code=400, message=f"{request_id}: Only *.mp3 files can be uploaded")
37 changes: 36 additions & 1 deletion app/models/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ class Config:
"progress": 100,
"videos": [
"http://127.0.0.1:8080/tasks/6c85c8cc-a77a-42b9-bc30-947815aa0558/final-1.mp4"
],
"combined_videos": [
"http://127.0.0.1:8080/tasks/6c85c8cc-a77a-42b9-bc30-947815aa0558/combined-1.mp4"
]
}
},
Expand All @@ -222,7 +225,39 @@ class Config:
"status": 200,
"message": "success",
"data": {
"video_terms": []
"video_terms": ["sky", "tree"]
}
},
}


class BgmListResponse(BaseResponse):
class Config:
json_schema_extra = {
"example": {
"status": 200,
"message": "success",
"data": {
"bgm_list": [
{
"filename": "output000.mp3",
"size": 2249517,
"filepath": "C:\\Users\\cathy\\Desktop\\MoneyPrinterTurbo\\resource\\songs\\output000.mp3"
}
]
}
},
}


class BgmUploadResponse(BaseResponse):
class Config:
json_schema_extra = {
"example": {
"status": 200,
"message": "success",
"data": {
"uploaded_path": "/root/home/MoneyPrinterTurbo/resource/songs/example.mp3"
}
},
}

0 comments on commit 6d8911f

Please sign in to comment.