Skip to content

视频任务状态查询 ​

视频生成是异步任务,提交后需通过任务 ID 轮询状态,直到任务完成或失败。

查询任务状态 ​

GET /v1/videos/{task_id}

路径参数 ​

参数类型必填说明
task_idstring是创建视频时返回的任务 ID

请求示例 ​

bash
curl -X GET "https://your-api-base/v1/videos/abcd1234efgh" \
  -H "Authorization: Bearer sk-xxxxxx"
python
import requests
import time

API_BASE = "https://your-api-base"
API_KEY = "sk-xxxxxx"

def wait_for_video(task_id, interval=10, timeout=600):
    """轮询视频任务状态,直到完成或失败"""
    start = time.time()
    while time.time() - start < timeout:
        resp = requests.get(
            f"{API_BASE}/v1/videos/{task_id}",
            headers={"Authorization": f"Bearer {API_KEY}"}
        )
        data = resp.json()
        status = data.get("status")

        print(f"[{int(time.time() - start)}s] 状态: {status}, 进度: {data.get('progress', 0)}%")

        if status == "completed":
            print(f"视频地址: {data.get('url')}")
            return data
        elif status == "failed":
            print(f"生成失败: {data.get('error', {}).get('message')}")
            return data

        time.sleep(interval)

    print("超时,任务未完成")
    return None

# 使用示例:先创建视频任务,再轮询
# 1. 创建任务
create_resp = requests.post(
    f"{API_BASE}/v1/videos",
    headers={"Authorization": f"Bearer {API_KEY}"},
    json={
        "model": "sora",
        "prompt": "A cat walking on the beach at sunset",
        "duration": 5,
        "width": 1280,
        "height": 720
    }
)
task_id = create_resp.json()["task_id"]
print(f"任务已创建: {task_id}")

# 2. 轮询状态
result = wait_for_video(task_id)

响应 ​

排队中 ​

json
{
  "id": "video-abc123",
  "task_id": "abcd1234efgh",
  "object": "video",
  "model": "sora",
  "status": "queued",
  "progress": 0,
  "created_at": 1677858242
}

生成中 ​

json
{
  "id": "video-abc123",
  "task_id": "abcd1234efgh",
  "object": "video",
  "model": "sora",
  "status": "in_progress",
  "progress": 45,
  "created_at": 1677858242
}

已完成 ​

json
{
  "id": "video-abc123",
  "task_id": "abcd1234efgh",
  "object": "video",
  "model": "sora",
  "status": "completed",
  "progress": 100,
  "created_at": 1677858242,
  "completed_at": 1677858300,
  "expires_at": 1677944642,
  "url": "https://example.com/video.mp4",
  "format": "mp4",
  "metadata": {
    "duration": 5,
    "fps": 30,
    "width": 1280,
    "height": 720,
    "seed": 20231234
  }
}

生成失败 ​

json
{
  "id": "video-abc123",
  "task_id": "abcd1234efgh",
  "object": "video",
  "model": "sora",
  "status": "failed",
  "progress": 0,
  "created_at": 1677858242,
  "error": {
    "code": 500,
    "message": "Video generation failed"
  }
}

状态说明 ​

状态说明后续操作
queued排队中,等待处理继续轮询
in_progress正在生成继续轮询
completed已完成通过 url 字段下载视频
failed生成失败查看 error 字段,重新提交任务

下载视频 ​

任务完成后,可以通过以下方式获取视频:

方式一:从状态响应的 url 字段直接下载

bash
curl -o video.mp4 "https://example.com/video.mp4"

方式二:通过 API 端点获取视频内容

GET /v1/videos/{task_id}/content

bash
curl -X GET "https://your-api-base/v1/videos/abcd1234efgh/content" \
  -H "Authorization: Bearer sk-xxxxxx" \
  -o video.mp4

注意

视频 URL 有过期时间(expires_at),请在有效期内下载。过期后需重新查询任务状态获取新地址。

备用端点 ​

端点方法说明
/v1/video/generations/{task_id}GET获取视频生成任务状态