Spaces:
Sleeping
Sleeping
| import os | |
| from typing import List | |
| import tempfile | |
| from fastapi import APIRouter, HTTPException, UploadFile, File, Form | |
| from src.schema import ReqAutoMixVideo, ResAutoMixVideo | |
| from src.media.video import VideoManager | |
| from src.service.auto_mix_video import MixVideoService | |
| api = APIRouter() | |
| async def get_audio_server(req: ReqAutoMixVideo): | |
| try: | |
| mix_video_manager = MixVideoService() | |
| bucket_name, video_url = await mix_video_manager.start(req) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| return ResAutoMixVideo( | |
| bucket_name=bucket_name, | |
| video_url=video_url, | |
| ) | |
| async def get_audio_minio_url(bucket_name: str = Form(...), video_files: List[UploadFile] = File(...)): | |
| video_manager = VideoManager() | |
| video_url_list = [] | |
| for video_file in video_files: | |
| # 创建临时文件 | |
| with tempfile.NamedTemporaryFile(delete=False) as temp_file: | |
| # 写入上传内容 | |
| temp_file.write(await video_file.read()) | |
| file_path = temp_file.name | |
| try: | |
| # 上传文件 | |
| bucket_name, video_url = video_manager.upload_file(file_path=file_path, bucket_name=bucket_name) | |
| video_url_list.append( | |
| ResAutoMixVideo( | |
| bucket_name=bucket_name, | |
| video_url=video_url, | |
| ) | |
| ) | |
| finally: | |
| # 确保删除临时文件 | |
| try: | |
| os.unlink(file_path) | |
| except Exception: | |
| pass # 忽略删除错误 | |
| return video_url_list | |