AI 视频生成
约 497 字大约 2 分钟
2026-10-04
提交视频生成任务,等待完成后下载结果。
配置
按 AI 适配器指南 配置认证。AI 适配器需要 litellm >= 1.83.0, 启动时会检查依赖版本。生成视频需要提供商和模型支持视频生成。
adapters:
- type: ai
platform: ai
config:
api_key: "" # 留空使用提供商环境变量
video_model: "openai/sora-2"
timeout: 120.0 # 单次请求超时,不是整个任务的等待时间也可以在调用时传 model=。未配置 video_model 且未传模型时抛出 ValueError; 调用时指定的模型不存在,会回退到配置中的 video_model。
生成、查询和下载
video_generation() 返回任务对象,不等待整个视频生成完成。保留其完整 id, 用 video_status() 查询;完成后 video_content() 返回 bytes,由调用者保存或发送。 查询和下载使用原任务 ID 路由到原提供商,不使用默认模型回退。
import asyncio
from pathlib import Path
job = await self.api.ai.video_generation(
"一只猫在阳光下追逐毛线球",
seconds="8",
size="1280x720",
)
# 限制整个等待过程为 10 分钟,超时由调用者处理。
async with asyncio.timeout(600):
while job.status not in {"completed", "failed"}:
await asyncio.sleep(5)
job = await self.api.ai.video_status(job.id)
if job.status == "failed":
raise RuntimeError(f"视频生成失败: {job.error}")
data = await self.api.ai.video_content(job.id)
await asyncio.to_thread(Path("generated.mp4").write_bytes, data)如果模型支持参考图像,可使用文件对象;需保持文件打开直到请求完成:
with open("reference.png", "rb") as reference:
job = await self.api.ai.video_generation(
"让图中的猫转头看向镜头",
input_reference=reference,
seconds="8",
)seconds 使用字符串;时长、尺寸、参考图像及其他参数的支持范围由提供商决定。 api_key、api_base、timeout 等请求参数可通过 **kwargs 覆盖配置,查询和下载时 也可传入。使用自定义认证或端点提交任务后,后续请求应使用相同的认证和端点。
延伸阅读
版权所有
版权归属:Yuan
