From 321884ff6dfc4a256a0eeb73c3a2e5b42b85f679 Mon Sep 17 00:00:00 2001 From: xgopilot Date: Sun, 26 Oct 2025 12:23:58 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E5=90=88=E6=88=90=E6=8E=A5=E5=8F=A3=E4=BB=8E=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E8=AF=BB=E5=8F=96=E5=AE=8C=E6=95=B4=E5=9C=BA=E6=99=AF?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 VideoComposeRequest schema,使用 project_id 替代 scenes 和 project_title - 修改 /compose 接口从数据库查询所有场景和对话数据 - 确保包含所有场景信息:image_url、audio_url、对话等 - 按 sequence 排序确保场景顺序正确 - 从 Dialogue 表查询对话数据并构造 DialogueLine 对象 Closes #166 Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: dthcle <23205652+dthcle@users.noreply.github.com> --- backend/api/routes/video.py | 61 ++++++++++++++++++++++++++++++++++--- backend/models/schemas.py | 3 +- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/backend/api/routes/video.py b/backend/api/routes/video.py index 528733e..4371ab3 100644 --- a/backend/api/routes/video.py +++ b/backend/api/routes/video.py @@ -1,10 +1,13 @@ -from fastapi import APIRouter, HTTPException +from fastapi import APIRouter, HTTPException, Depends from typing import List, Optional +from sqlalchemy.orm import Session from backend.models.schemas import ( - Scene, NovelProject, + Scene, NovelProject, DialogueLine, VideoFromAssetsRequest, VideoFromAssetsResponse, VideoComposeRequest ) +from backend.models.database import Scene as DBScene, Project, Dialogue +from backend.database import get_db from backend.services.video_composition.service import VideoCompositionService router = APIRouter() @@ -12,11 +15,57 @@ @router.post("/compose") -async def compose_video(request: VideoComposeRequest): +async def compose_video(request: VideoComposeRequest, db: Session = Depends(get_db)): try: + project = db.query(Project).filter(Project.id == request.project_id).first() + if not project: + raise HTTPException(status_code=404, detail="项目不存在") + + db_scenes = db.query(DBScene).filter( + DBScene.project_id == request.project_id + ).order_by(DBScene.sequence).all() + + if not db_scenes: + raise HTTPException(status_code=404, detail="该项目没有场景数据") + + scenes = [] + for db_scene in db_scenes: + dialogues = db.query(Dialogue).filter( + Dialogue.scene_id == db_scene.id, + Dialogue.project_id == request.project_id + ).order_by(Dialogue.sequence).all() + + lines = [ + DialogueLine( + character=dialogue.character, + text=dialogue.text, + emotion=dialogue.emotion or "neutral", + is_monologue=dialogue.is_monologue or False, + audio_url=None + ) + for dialogue in dialogues + ] + + scene = Scene( + id=str(db_scene.id), + sequence=db_scene.sequence, + description=db_scene.description or "", + dialogue=db_scene.dialogue, + narrator_text=None, + characters=db_scene.characters or [], + setting=db_scene.setting or "", + mood=db_scene.mood or "", + image_prompt=db_scene.image_prompt, + image_url=db_scene.image_url, + audio_url=db_scene.audio_url, + duration=float(db_scene.duration) if db_scene.duration else 5.0, + lines=lines + ) + scenes.append(scene) + video_path = video_service.generate_video_from_scenes( - scenes=request.scenes, - project_title=request.project_title, + scenes=scenes, + project_title=project.title, add_bgm=request.add_bgm, bgm_path=request.bgm_path, ) @@ -25,6 +74,8 @@ async def compose_video(request: VideoComposeRequest): "success": True, "video_path": video_path, } + except HTTPException: + raise except Exception as e: raise HTTPException(status_code=500, detail=str(e)) diff --git a/backend/models/schemas.py b/backend/models/schemas.py index 1b4fa32..c44332e 100644 --- a/backend/models/schemas.py +++ b/backend/models/schemas.py @@ -230,7 +230,6 @@ class VideoFromAssetsResponse(BaseModel): class VideoComposeRequest(BaseModel): - scenes: List[Scene] - project_title: str + project_id: str add_bgm: bool = False bgm_path: Optional[str] = None