Zhenran Xu
2026-03-30 0f9a259b487fed51fd781553b3bd279da0bb2a4d
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""
阶段6: 后期制作智能体
拼接用户在阶段5选定的视频片段 → 最终成片
"""
 
import os
import re
import subprocess
import asyncio
import logging
from typing import Any, Optional, Dict
 
from .base_agent import AgentInterface
 
logger = logging.getLogger(__name__)
 
 
class VideoEditorAgent(AgentInterface):
    """后期制作:拼接用户选择的视频片段 → 最终成片"""
 
    def __init__(self):
        super().__init__(name="VideoEditor")
 
    async def process(self, input_data: Any, intervention: Optional[Dict] = None) -> Dict:
        sid = input_data["session_id"]
        selected_clips: dict = input_data.get("selected_clips", {})
 
        if not selected_clips:
            raise Exception("未收到用户选择的视频片段(selected_clips),请先完成阶段5")
 
        self._report_progress("后期制作", "准备视频片段...", 5)
 
        def run():
            video_dir = os.path.join('code/result/video', str(sid))
 
            # 按 shot_id 中的数字排序
            def sort_key(k: str) -> tuple:
                return tuple(int(n) for n in re.findall(r'\d+', k)) or (999,)
 
            clip_paths = []
            for shot_id in sorted(selected_clips.keys(), key=sort_key):
                path = selected_clips[shot_id]
                if os.path.exists(path):
                    clip_paths.append(path)
                else:
                    logger.warning(f"[{sid}] Clip missing: {shot_id} → {path}")
 
            if not clip_paths:
                raise Exception("所有选定的视频片段文件均不存在")
 
            logger.info(f"[{sid}] Concat {len(clip_paths)} clips")
            self._report_progress("后期制作", f"拼接 {len(clip_paths)} 个片段...", 15)
 
            list_path = os.path.join(video_dir, 'file_list.txt')
            with open(list_path, 'w', encoding='utf-8') as f:
                for p in clip_paths:
                    f.write(f"file '{os.path.abspath(p)}'\n")
 
            self._report_progress("后期制作", "ffmpeg 拼接中...", 30)
 
            output = os.path.join(video_dir, f'{sid}_final.mp4')
            cmd = ['ffmpeg', '-f', 'concat', '-safe', '0',
                   '-i', list_path, '-c', 'copy', '-y', output]
            subprocess.run(cmd, check=True, capture_output=True)
            logger.info(f"[{sid}] Concat success: {output}")
            return output
 
        loop = asyncio.get_running_loop()
        final_path = await loop.run_in_executor(None, run)
 
        self._report_progress("后期制作", "成片完成", 100)
 
        return {
            "payload": {"session_id": sid, "final_video": final_path},
            "stage_completed": True,
        }