Xinfan Chen
2026-05-12 612efddfc4736dedd7af97f191bbf3686a28f2ce
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"""
统一视频生成客户端
根据 model 名称自动路由到对应后端:
  - wan*      → DashscopeVideoClient (DashScope VideoSynthesis)
  - kling*    → KlingVideoClient (可灵 AI)
"""
 
import os
import logging
from typing import Optional
from config import Config
 
try:
    from models.video_dashscope import DashscopeVideoClient
    from models.video_kling import KlingVideoClient
    from models.video_seedance import SeedanceVideoClient
except ImportError:
    from video_dashscope import DashscopeVideoClient
    from video_kling import KlingVideoClient
    from video_seedance import SeedanceVideoClient
 
logger = logging.getLogger(__name__)
 
 
class VideoClient:
    """
    统一视频生成客户端
    参照 ImageClient 模式,按模型名路由到不同后端
    """
 
    def __init__(
        self,
        dashscope_api_key: Optional[str] = None,
        dashscope_base_url: Optional[str] = None,
        kling_access_key: Optional[str] = None,
        kling_secret_key: Optional[str] = None,
        kling_base_url: Optional[str] = None,
        ark_api_key: Optional[str] = None,
        ark_base_url: Optional[str] = None,
    ):
        # 万象客户端
        self.Dashscope_client = DashscopeVideoClient(
            api_key=dashscope_api_key or Config.DASHSCOPE_API_KEY,
            base_url=dashscope_base_url or Config.DASHSCOPE_BASE_URL,
        )
 
        # 可灵客户端
        self.kling_client = KlingVideoClient(
            access_key=kling_access_key or Config.KLING_ACCESS_KEY,
            secret_key=kling_secret_key or Config.KLING_SECRET_KEY,
            base_url=kling_base_url or Config.KLING_BASE_URL,
        )
 
        # Seedance 客户端
        self.seedance_client = SeedanceVideoClient(
            api_key=ark_api_key or Config.ARK_API_KEY,
            base_url=ark_base_url or Config.ARK_BASE_URL,
        )
 
    def generate_video(
        self,
        prompt: str,
        image_path: Optional[str],
        save_path: str,
        model: str = "wan2.7-i2v",
        duration: int = 5,
        shot_type: str = "multi",
        sound: str = "",
        video_ratio: str = "16:9",
        resolution: Optional[str] = None,
        last_image_path: Optional[str] = None,
        first_clip_path: Optional[str] = None,
        reference_image_path: Optional[str] = None,
        reference_image_paths: Optional[list[str]] = None,
        reference_video_paths: Optional[list[str]] = None,
        reference_audio_path: Optional[str] = None,
        audio_path: Optional[str] = None,
        negative_prompt: Optional[str] = None,
        prompt_extend: Optional[bool] = None,
        watermark: Optional[bool] = None,
        seed: Optional[int] = None,
        mode: str = "pro",
        cfg_scale: float = 0.5,
        generate_audio: Optional[bool] = None,
        audio: Optional[bool] = None,
    ) -> str:
        """
        生成视频
 
        Args:
            prompt: 视频描述提示词
            image_path: 输入图片本地路径;DashScope wan2.7 视频续写可为空并使用 first_clip_path
            save_path: 输出视频保存路径
            model: 模型名,决定使用哪个后端
            duration: 视频时长(秒)
            shot_type: 镜头类型 "single" / "multi"
 
        Returns:
            video_url: 远端视频 URL
 
        Raises:
            FileNotFoundError: 输入图片不存在
            RuntimeError: 生成或下载失败
        """
        if not model:
            model = "wan2.7-i2v"
 
        if Config.PRINT_MODEL_INPUT:
            lines = [
                "---- VIDEO GENERATION REQUEST ----",
                f"Prompt: {prompt}",
                "Image: [Base64图片]" if image_path and str(image_path).startswith("data:") else f"Image: {image_path}",
                f"Model: {model}",
                f"Duration: {duration}s",
                f"Shot Type: {shot_type}",
                f"Video Ratio: {video_ratio}",
            ]
            if resolution:
                lines.append(f"Resolution: {resolution}")
            if last_image_path:
                lines.append(f"Last Image: {last_image_path}")
            if first_clip_path:
                lines.append(f"First Clip: {first_clip_path}")
            if reference_image_path:
                lines.append(f"Reference Image: {reference_image_path}")
            if reference_image_paths:
                lines.append(f"Reference Images: {reference_image_paths}")
            if reference_video_paths:
                lines.append(f"Reference Videos: {reference_video_paths}")
            if reference_audio_path:
                lines.append(f"Reference Audio: {reference_audio_path}")
            if audio_path:
                lines.append(f"Audio: {audio_path}")
            if negative_prompt:
                lines.append(f"Negative Prompt: {negative_prompt}")
            if sound:
                lines.append(f"Sound: {sound}")
            lines.extend([
                f"Save: {save_path}",
                "-" * 30,
            ])
            logger.info("\n%s", "\n".join(lines))
 
        model_lower = model.lower()
 
        if "kling" in model_lower:
            return self._generate_kling(
                prompt,
                image_path,
                save_path,
                model,
                duration,
                sound,
                mode,
                cfg_scale,
                negative_prompt or "",
            )
        elif "seedance" in model_lower:
            return self._generate_seedance(
                prompt,
                image_path,
                save_path,
                model,
                duration,
                video_ratio,
                resolution,
                seed,
                watermark,
                generate_audio,
            )
        elif "wan" in model_lower or "happyhorse" in model_lower:
            return self._generate_wan(
                prompt,
                image_path,
                save_path,
                model,
                duration,
                shot_type,
                video_ratio,
                last_image_path,
                first_clip_path,
                reference_image_path,
                reference_image_paths,
                reference_video_paths,
                reference_audio_path,
                audio_path,
                negative_prompt,
                resolution,
                prompt_extend,
                watermark if watermark is not None else False,
                seed,
                audio,
            )
        else:
            raise ValueError(f"未知的视频生成模型: {model}")
 
    def _generate_wan(
        self,
        prompt: str,
        image_path: Optional[str],
        save_path: str,
        model: str,
        duration: int,
        shot_type: str,
        video_ratio: str,
        last_image_path: Optional[str],
        first_clip_path: Optional[str],
        reference_image_path: Optional[str],
        reference_image_paths: Optional[list[str]],
        reference_video_paths: Optional[list[str]],
        reference_audio_path: Optional[str],
        audio_path: Optional[str],
        negative_prompt: Optional[str],
        resolution: Optional[str],
        prompt_extend: Optional[bool],
        watermark: bool,
        seed: Optional[int],
        audio: Optional[bool],
    ) -> str:
        """通过万象模型生成视频"""
        logger.info("VideoClient routed to Wan: model=%s", model)
        return self.Dashscope_client.generate_video(
            prompt=prompt,
            image_path=image_path,
            save_path=save_path,
            model=model,
            duration=duration,
            shot_type=shot_type,
            video_ratio=video_ratio,
            last_image_path=last_image_path,
            first_clip_path=first_clip_path,
            reference_image_path=reference_image_path,
            reference_image_paths=reference_image_paths,
            reference_video_paths=reference_video_paths,
            reference_audio_path=reference_audio_path,
            audio_path=audio_path,
            negative_prompt=negative_prompt,
            resolution=resolution,
            prompt_extend=prompt_extend,
            watermark=watermark,
            seed=seed,
            audio=audio,
        )
 
    def _generate_kling(
        self,
        prompt: str,
        image_path: Optional[str],
        save_path: str,
        model: str,
        duration: int = 5,
        sound: str = "",
        mode: str = "pro",
        cfg_scale: float = 0.5,
        negative_prompt: str = "",
    ) -> str:
        """通过可灵模型生成视频"""
        logger.info("VideoClient routed to Kling: model=%s", model)
        return self.kling_client.generate_video(
            prompt=prompt,
            image_path=image_path,
            save_path=save_path,
            model=model,
            duration=duration,
            sound=sound,
            mode=mode,
            cfg_scale=cfg_scale,
            negative_prompt=negative_prompt,
        )
 
    def _generate_seedance(
        self,
        prompt: str,
        image_path: Optional[str],
        save_path: str,
        model: str,
        duration: int = 5,
        video_ratio: str = "16:9",
        resolution: Optional[str] = None,
        seed: Optional[int] = None,
        watermark: Optional[bool] = None,
        generate_audio: Optional[bool] = None,
    ) -> str:
        """通过 Seedance 模型生成视频"""
        logger.info("VideoClient routed to Seedance: model=%s", model)
        return self.seedance_client.generate_video(
            prompt=prompt,
            image_path=image_path,
            save_path=save_path,
            model=model,
            duration=duration,
            ratio=video_ratio,
            resolution=resolution or "720p",
            seed=seed,
            watermark=watermark,
            generate_audio=generate_audio,
        )