log
2026-06-25 7c4c956be0c3c4c2f76f181e356779003bc8f998
video-claw/video-claw/backend/core/agents/video_agent.py
@@ -2,7 +2,7 @@
"""
阶段5: 视频生成智能体
- 从编排器注入的 artifacts["storyboard"] 读取拍摄片段(Segments)
- 视频提示词:风格前缀 + 分镜列表(分镜1: [时长] content...)
- 视频提示词:风格控制 + 人物列表 + 分镜列表(分镜1: [时长] content...)
- 参考图:从 artifacts["reference_generation"] 读取
- 支持逐项并发生成、实时预览、重新生成
"""
@@ -61,17 +61,32 @@
    # ─── 视频生成 ───
    def _generate_one(self, sid: str, segment_id: str, prompt: str,
                      img_path: str, video_model: str,
                      img_path: Optional[str], video_model: str,
                      duration: int = 10, sound: str = "",
                      shot_type: str = "multi",
                      video_ratio: str = "16:9",
                      video_resolution: str = "720P") -> tuple:
                      video_resolution: str = "720P",
                      video_generation_mode: str = "first_frame",
                      last_image_path: Optional[str] = None,
                      reference_image_paths: Optional[List[str]] = None) -> tuple:
        """生成单个视频片段,返回 (segment_id, path_or_None)"""
        if self.cancellation_check and self.cancellation_check():
            logger.info(f"VideoDirectorAgent: {segment_id} 跳过(用户取消)")
            return segment_id, None
        if not os.path.exists(img_path):
        # 限制最大总版本数(防止无限计费)
        existing_versions = self._list_versions(sid, segment_id)
        if len(existing_versions) >= 10:
            logger.warning(f"[{segment_id}] 历史版本已达最大限制(10个),为保护计费不再继续生成视频。")
            return segment_id, existing_versions[-1] if existing_versions else None
        reference_image_paths = reference_image_paths or []
        if video_generation_mode == "reference":
            missing_refs = [path for path in reference_image_paths if not os.path.exists(path)]
            if not reference_image_paths or missing_refs:
                logger.warning("Reference images missing for %s: %s", segment_id, missing_refs or reference_image_paths)
                return segment_id, None
        elif not img_path or not os.path.exists(img_path):
            logger.warning(f"Image missing for {segment_id}: {img_path}")
            return segment_id, None
@@ -89,6 +104,8 @@
                shot_type=shot_type,
                video_ratio=video_ratio,
                resolution=video_resolution,
                last_image_path=last_image_path if video_generation_mode == "start_end_frame" else None,
                reference_image_paths=reference_image_paths if video_generation_mode == "reference" else None,
            )
            return segment_id, save_path
        except Exception as e:
@@ -102,30 +119,88 @@
    # ─── 提示词组装 ───
    def _assemble_prompt(self, segment: dict, style_prompt: str, video_data: Optional[dict]=None) -> str:
        """组装视频提示词
        格式:
        风格控制:用户选择的风格提示词, 电影质感
        分镜列表:分镜1:[时长] content... 分镜2:[时长] content...
        """
        # 如果 video_data 中有 description,则优先使用
        # 前端修改后的提示词会存入 artifacts.video_generation.clips.description
        if video_data and "description" in video_data:
            return video_data["description"]
        # 否则从 segment 中组装
        prompt = f"风格控制:{style_prompt}\n"
        prompt += "分镜列表:"
    def _format_shot_section(self, segment: dict) -> str:
        prompt = "分镜列表:"
        shots = segment.get("shots", [])
        for i, shot in enumerate(shots, 1):
            dur = shot.get("duration", 5)
            content = shot.get("content", "").strip()
            prompt += f"\n分镜{i}:[{dur}秒] {content}"
        prompt += "\n不要生成字幕或水印"
        return prompt
    @staticmethod
    def _extract_shot_section(prompt: str) -> str:
        text = (prompt or "").strip()
        if not text:
            return "分镜列表:"
        marker = "分镜列表:"
        alt_marker = "分镜列表:"
        if marker in text:
            text = text[text.index(marker):].strip()
        elif alt_marker in text:
            text = marker + text[text.index(alt_marker) + len(alt_marker):].strip()
        else:
            text = f"{marker}\n{text}"
        no_caption_marker = "\n不要生成字幕或水印"
        if no_caption_marker in text:
            text = text[:text.index(no_caption_marker)].rstrip()
        return text
    def _display_prompt(self, segment: dict, video_data: Optional[dict] = None) -> str:
        """前端只展示可编辑的分镜列表;风格和人物信息在调用视频模型前即时拼接。"""
        if video_data and video_data.get("description"):
            return self._extract_shot_section(video_data["description"])
        return self._format_shot_section(segment)
    def _build_character_section(self, segment: dict, character_artifact: Optional[dict]) -> str:
        characters = character_artifact.get("characters", []) if isinstance(character_artifact, dict) else []
        if not characters:
            return "人物列表:无"
        character_map = self._build_name_asset_map(characters)
        selected_assets = []
        seen = set()
        for character_name in segment.get("characters") or []:
            asset = self._match_asset_by_name(str(character_name), characters, character_map)
            asset_key = str(asset.get("id") or asset.get("name") or "") if asset else ""
            if asset and asset_key not in seen:
                selected_assets.append(asset)
                seen.add(asset_key)
        lines = []
        for asset in selected_assets:
            name = str(asset.get("name") or asset.get("id") or "").strip()
            desc = str(asset.get("description") or "").strip()
            if name and desc:
                lines.append(f"- {name}:{desc}")
            elif name:
                lines.append(f"- {name}")
        return "人物列表:\n" + "\n".join(lines) if lines else "人物列表:无"
    def _assemble_prompt(self, segment: dict, style_prompt: str,
                         character_artifact: Optional[dict] = None,
                         video_data: Optional[dict] = None) -> str:
        """组装视频提示词
        格式:
        风格控制:用户选择的风格提示词, 电影质感
        人物列表:从第二阶段人物描述读取
        分镜列表:分镜1:[时长] content... 分镜2:[时长] content...
        """
        if video_data and "description" in video_data:
            # 前端修改后的提示词会存入 artifacts.video_generation.clips.description。
            # 兼容旧格式:保留用户修改过的分镜部分,重新补齐最新风格和人物列表。
            shot_section = self._extract_shot_section(video_data["description"])
        else:
            shot_section = self._format_shot_section(segment)
        return (
            f"风格控制:{style_prompt}\n"
            f"{self._build_character_section(segment, character_artifact)}\n"
            f"{shot_section}\n"
            "不要生成字幕或水印"
        )
    def _get_style_keywords(self, session_data: dict) -> str:
        """从会话数据获取风格关键词"""
@@ -160,10 +235,108 @@
        # 3. 默认路径
        return os.path.abspath(os.path.join('code/result/image', str(sid), 'Scenes', f"{segment_id}.jpg"))
    def _get_next_reference_image(self, sid: str, segment_index: int, segments: list, scene_map: dict) -> Optional[str]:
        """首尾帧模式下,优先用下一个片段参考图作为尾帧。"""
        if segment_index + 1 >= len(segments):
            return None
        next_segment_id = segments[segment_index + 1].get("segment_id")
        if not next_segment_id:
            return None
        path = self._get_reference_image(sid, next_segment_id, scene_map)
        return path if path and os.path.exists(path) else None
    @staticmethod
    def _asset_selected_path(asset: dict) -> str:
        selected = asset.get("selected") or ""
        if selected and os.path.exists(selected):
            return selected
        # Legacy session compatibility: some old artifacts only have versions and no selected field.
        for path in reversed(asset.get("versions") or []):
            if path and os.path.exists(path):
                return path
        return ""
    @staticmethod
    def _build_name_asset_map(assets: list[dict]) -> dict[str, dict]:
        """Build name -> asset mapping from character_design characters/settings."""
        mapping = {}
        for asset in assets:
            name = str(asset.get("name") or "").strip()
            asset_id = str(asset.get("id") or "").strip()
            if name:
                mapping[name] = asset
            if asset_id:
                mapping[asset_id] = asset
        return mapping
    @staticmethod
    def _match_asset_by_name(name: str, assets: list[dict], asset_map: Optional[dict[str, dict]] = None) -> Optional[dict]:
        clean_name = (name or "").strip()
        if not clean_name:
            return None
        if asset_map and clean_name in asset_map:
            return asset_map[clean_name]
        for asset in assets:
            asset_name = str(asset.get("name") or "").strip()
            if asset_name and asset_name == clean_name:
                return asset
        for asset in assets:
            asset_name = str(asset.get("name") or "").strip()
            if asset_name and (asset_name in clean_name or clean_name in asset_name):
                return asset
        return None
    def _get_segment_reference_assets(self, segment: dict, character_artifact: dict) -> List[str]:
        """参考图生视频:按 segment.characters/location 读取第二阶段用户选中的人物图和场景图。"""
        characters = character_artifact.get("characters", []) if isinstance(character_artifact, dict) else []
        settings = character_artifact.get("settings", []) if isinstance(character_artifact, dict) else []
        character_map = self._build_name_asset_map(characters)
        setting_map = self._build_name_asset_map(settings)
        reference_paths: List[str] = []
        seen = set()
        def add_asset(asset: Optional[dict]) -> None:
            if not asset:
                return
            path = self._asset_selected_path(asset)
            if path and path not in seen:
                reference_paths.append(path)
                seen.add(path)
        location = str(segment.get("location") or "").strip()
        add_asset(self._match_asset_by_name(location, settings, setting_map))
        for character_name in segment.get("characters") or []:
            add_asset(self._match_asset_by_name(str(character_name), characters, character_map))
        return reference_paths
    @staticmethod
    def _select_video_model(input_data: dict, session_meta: dict) -> tuple[str, str]:
        mode = (
            input_data.get("video_generation_mode")
            or session_meta.get("video_generation_mode")
            or "first_frame"
        )
        model_key = {
            "first_frame": "video_first_frame_model",
            "start_end_frame": "video_start_end_model",
            "reference": "video_reference_model",
        }.get(mode, "video_first_frame_model")
        model = input_data.get(model_key) or session_meta.get(model_key)
        if not model:
            # Legacy session compatibility: sessions created before mode-specific video models only have video_model.
            model = input_data.get("video_model") or session_meta.get("video_model")
        if not model:
            raise ValueError("Missing required model configuration: video_model")
        return mode, model
    # ─── 预览 / Payload ───
    def _build_preview(self, sid: str, segments: list, scene_map: dict, style_prompt: str = "") -> list:
    def _build_preview(self, sid: str, segments: list, scene_map: dict,
                       video_clips: Optional[list] = None) -> list:
        preview = []
        clip_map = {c.get("id"): c for c in (video_clips or []) if isinstance(c, dict) and c.get("id")}
        for idx, seg in enumerate(segments, 1):
            segment_id = seg["segment_id"]
            versions = self._list_versions(sid, segment_id)
@@ -174,7 +347,7 @@
                "name": f"第{ep_n}集-片段{seg_n}",
                "episode": ep_n,
                "index": seg_n,
                "description": self._assemble_prompt(seg, style_prompt),
                "description": self._display_prompt(seg, clip_map.get(segment_id)),
                "duration": seg.get('total_duration', 10),
                "selected": versions[-1] if versions else "",
                "versions": versions,
@@ -182,8 +355,9 @@
            })
        return preview
    def _build_payload(self, sid: str, segments: list, style_prompt: str = "") -> dict:
    def _build_payload(self, sid: str, segments: list, video_clips: Optional[list] = None) -> dict:
        clips = []
        clip_map = {c.get("id"): c for c in (video_clips or []) if isinstance(c, dict) and c.get("id")}
        for idx, seg in enumerate(segments, 1):
            segment_id = seg["segment_id"]
            versions = self._list_versions(sid, segment_id)
@@ -194,7 +368,7 @@
                "name": f"第{ep_n}集-片段{seg_n}",
                "episode": ep_n,
                "index": seg_n,
                "description": self._assemble_prompt(seg, style_prompt),
                "description": self._display_prompt(seg, clip_map.get(segment_id)),
                "duration": seg.get('total_duration', 10),
                "selected": versions[-1] if versions else "",
                "versions": versions,
@@ -231,24 +405,26 @@
            segments = []
            for ep in episodes:
                segments.extend(ep.get("segments", []))
            payload = self._build_payload(sid, segments)
            video_clips = artifacts.get('video_generation', {}).get('clips', [])
            payload = self._build_payload(sid, segments, video_clips)
            for clip in payload.get("payload", {}).get("clips", []):
                clip_id = clip.get("id")
                if clip_id in selected_clips:
                    clip["selected"] = selected_clips[clip_id]
            return payload
        video_model = self._require_input(input_data, "video_model")
        session_meta = self._session_meta(input_data)
        video_generation_mode, video_model = self._select_video_model(input_data, session_meta)
        enable_concurrency = input_data.get("enable_concurrency", True)
        from models.config_model import get_max_concurrency
        concurrency = get_max_concurrency(video_model, enable_concurrency)
        
        video_ratio = input_data.get("video_ratio", "16:9")
        video_resolution = input_data.get("video_resolution", "720P")
        video_shot_type = input_data.get("video_shot_type", "multi")
        video_sound = "on"
        video_shot_type = "multi"
        artifacts = self._session_artifacts(input_data)
        session_meta = self._session_meta(input_data)
        
        # 1. 获取拍摄片段列表 (从 Storyboard)
        episodes = artifacts.get('storyboard', {}).get('episodes', [])
@@ -264,6 +440,7 @@
        ref_art = artifacts.get('reference_generation', {})
        scene_list = ref_art.get('scenes', [])
        scene_map = {s['id']: s for s in scene_list if 'id' in s}
        character_art = artifacts.get('character_design', {})
        
        style_zh = input_data.get('style') or session_meta.get('style') or 'realistic'
        # 简单映射为中文显示名
@@ -296,19 +473,34 @@
                            seg = segment_map.get(seg_id)
                            clip = clip_map.get(seg_id) if clip_map.get(seg_id) else None
                            if not seg: continue
                            prompt = self._assemble_prompt(seg, style_prompt, video_data=clip)
                            prompt = self._assemble_prompt(seg, style_prompt, character_art, video_data=clip)
                            print("#"*100)
                            print(clip)
                            print(prompt)
                            return
                            img_path = self._get_reference_image(sid, seg_id, scene_map)
                            reference_image_paths = None
                            if video_generation_mode == "reference":
                                img_path = None
                                reference_image_paths = self._get_segment_reference_assets(seg, character_art)
                                if not reference_image_paths:
                                    logger.warning("VideoDirectorAgent: %s 参考图模式未匹配到第二阶段人物/场景图", seg_id)
                            else:
                                img_path = self._get_reference_image(sid, seg_id, scene_map)
                            duration = seg.get("total_duration", 10)
                            seg_index = segments.index(seg)
                            last_img_path = self._get_next_reference_image(sid, seg_index, segments, scene_map)
                            if video_generation_mode == "start_end_frame" and not last_img_path:
                                logger.warning("VideoDirectorAgent: %s 首尾帧模式缺少尾帧,回退为首帧生视频入参", seg_id)
                            existing_versions = self._list_versions(sid, seg_id)
                            self._report_progress("视频生成", f"启动生成: {seg_id}", 5, data={
                                "asset_complete": {
                                    "type": "clips", "id": seg_id,
                                    "status": "running",
                                    "versions": existing_versions,
                                }
                            })
                            fut = executor.submit(
                                self._generate_one, sid, seg_id, prompt,
                                img_path, video_model, duration,
                                "", video_shot_type, video_ratio, video_resolution
                                video_sound, video_shot_type, video_ratio, video_resolution,
                                video_generation_mode, last_img_path, reference_image_paths
                            )
                            futs[fut] = seg_id
                        for fut in as_completed(futs):
@@ -344,30 +536,53 @@
                # 同步到 session artifacts
                self._update_session_video_data(sid, segments, style_prompt)
                
                return self._build_payload(sid, segments, style_prompt)
                return self._build_payload(sid, segments, video_clips)
        # ═══ 正常流程:全量生成 ═══
        self._report_progress("视频生成", "正在准备数据...", 2)
        preview = self._build_preview(sid, segments, scene_map, style_prompt)
        preview = self._build_preview(sid, segments, scene_map, video_clips)
        self._report_progress("视频生成", "加载视频列表", 5, data={"assets_preview": {"clips": preview}})
        def run():
            tasks = []
            for seg in segments:
            selected_map = {c.get("id"): c.get("selected") for c in video_clips if c.get("id")}
            for seg_index, seg in enumerate(segments):
                seg_id = seg["segment_id"]
                existing = self._list_versions(sid, seg_id)
                if existing: continue
                prompt = self._assemble_prompt(seg, style_prompt)
                img_path = self._get_reference_image(sid, seg_id, scene_map)
                if existing:
                    # 立即报告进度,使 UI 可以显示已跳过的视频
                    selected_path = selected_map.get(seg_id) or existing[-1]
                    self._report_progress("视频生成", f"跳过已生成: {seg_id}", 5, data={
                        "asset_complete": {
                            "type": "clips",
                            "id": seg_id,
                            "status": "done",
                            "selected": selected_path,
                            "versions": existing,
                        }
                    })
                    continue
                prompt = self._assemble_prompt(seg, style_prompt, character_art)
                reference_image_paths = None
                if video_generation_mode == "reference":
                    img_path = None
                    reference_image_paths = self._get_segment_reference_assets(seg, character_art)
                    if not reference_image_paths:
                        logger.warning("VideoDirectorAgent: %s 参考图模式未匹配到第二阶段人物/场景图", seg_id)
                else:
                    img_path = self._get_reference_image(sid, seg_id, scene_map)
                duration = seg.get("total_duration", 10)
                tasks.append((seg_id, prompt, img_path, duration))
                last_img_path = self._get_next_reference_image(sid, seg_index, segments, scene_map)
                if video_generation_mode == "start_end_frame" and not last_img_path:
                    logger.warning("VideoDirectorAgent: %s 首尾帧模式缺少尾帧,回退为首帧生视频入参", seg_id)
                tasks.append((seg_id, prompt, img_path, duration, last_img_path, reference_image_paths))
            if not tasks:
                self._report_progress("视频生成", "所有视频片段已存在", 95)
                return
            done = 0
            with ThreadPoolExecutor(max_workers=concurrency) as executor:
                futs = {}
                for seg_id, prompt, img_path, dur in tasks:
                for seg_id, prompt, img_path, dur, last_img_path, reference_image_paths in tasks:
                    # 提交前立即发送正在运行的状态,让前端 UI 更新
                    self._report_progress("视频生成", f"启动生成: {seg_id}", 5, data={
                        "asset_complete": {
@@ -378,7 +593,8 @@
                    fut = executor.submit(
                        self._generate_one, sid, seg_id, prompt,
                        img_path, video_model, dur,
                        "", video_shot_type, video_ratio, video_resolution
                        video_sound, video_shot_type, video_ratio, video_resolution,
                        video_generation_mode, last_img_path, reference_image_paths
                    )
                    futs[fut] = seg_id
                for fut in as_completed(futs):
@@ -419,4 +635,4 @@
        self._update_session_video_data(sid, segments, style_prompt)
        
        self._report_progress("视频生成", "完成", 100)
        return self._build_payload(sid, segments, style_prompt)
        return self._build_payload(sid, segments, video_clips)