外观
异步任务说明
视频生成等耗时较长的任务无法在一次 HTTP 请求内完成,采用异步任务模式:
text
提交任务 ──► 返回任务 ID ──► 轮询任务状态 ──► 完成后下载结果
POST id / task_id GET(每 5~10 秒) GET .../content流程
提交任务
调用 创建视频任务(POST /v1/videos),接口会立即返回任务对象,其中的 id 就是任务 ID,初始状态为 queued。
轮询状态
每隔 5~10 秒调用一次 查询视频任务(GET /v1/videos/{video_id}),直到 status 变为终态:
| status | 含义 |
|---|---|
queued | 排队中 |
in_progress | 生成中,可参考 progress(0–100) |
completed | 已完成,可以下载 |
failed | 失败,原因见 error.message |
下载结果
任务完成后调用 下载视频内容(GET /v1/videos/{video_id}/content)获取 MP4 文件,建议尽快保存到自己的存储中。
完整示例
python
import time
import requests
BASE = "https://vip.zen-ai.top"
HEADERS = {"Authorization": "Bearer sk-xxxxxxxx"}
# 1. 提交任务
task = requests.post(f"{BASE}/v1/videos", headers=HEADERS, json={
"model": "sora-2",
"prompt": "一只橘猫在窗台上晒太阳,镜头缓慢推近",
"seconds": "4",
"size": "1280x720",
}).json()
video_id = task["id"]
print("任务 ID:", video_id)
# 2. 轮询状态
while True:
info = requests.get(f"{BASE}/v1/videos/{video_id}", headers=HEADERS).json()
status = info.get("status")
print(status, info.get("progress"))
if status in ("completed", "failed"):
break
time.sleep(8)
# 3. 下载结果
if status == "completed":
video = requests.get(f"{BASE}/v1/videos/{video_id}/content", headers=HEADERS)
with open("output.mp4", "wb") as f:
f.write(video.content)
print("已保存 output.mp4")
else:
print("生成失败:", info.get("error"))js
const BASE = "https://vip.zen-ai.top";
const headers = { Authorization: `Bearer ${process.env.VIP_API_KEY}`, "Content-Type": "application/json" };
// 1. 提交任务
const task = await fetch(`${BASE}/v1/videos`, {
method: "POST",
headers,
body: JSON.stringify({ model: "sora-2", prompt: "一只橘猫在窗台上晒太阳,镜头缓慢推近", seconds: "4", size: "1280x720" }),
}).then((r) => r.json());
// 2. 轮询状态
let info;
do {
await new Promise((r) => setTimeout(r, 8000));
info = await fetch(`${BASE}/v1/videos/${task.id}`, { headers }).then((r) => r.json());
console.log(info.status, info.progress);
} while (!["completed", "failed"].includes(info.status));
// 3. 下载结果
if (info.status === "completed") {
const res = await fetch(`${BASE}/v1/videos/${task.id}/content`, { headers });
const { writeFile } = await import("node:fs/promises");
await writeFile("output.mp4", Buffer.from(await res.arrayBuffer()));
}