> ## Documentation Index
> Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 视频生成 API

> 根据你的工作流需求，选择适用于 Seedance、HappyHorse、Sora 2、Veo 3、Wan、xAI、Vidu、Omni、Kling 和 Runway 的 CometAPI 视频路由。

通过选择与你的任务类型匹配的提供商工作流，使用 CometAPI 视频模型文档。大多数视频端点都会创建异步任务，因此请保存任务 ID，并使用轮询来获取结果。仅当模型专属页面文档说明支持回调时，再添加回调。

## 选择视频 API

<CardGroup cols={2}>
  <Card title="创建 Seedance 视频" icon="sparkles" href="/api/video/seedance/create">
    创建 Seedance 视频任务。
  </Card>

  <Card title="创建 HappyHorse 视频" icon="sparkles" href="/api/video/happyhorse/create">
    创建 HappyHorse 文生视频任务。
  </Card>

  <Card title="创建 Sora 2 视频" icon="film" href="/api/video/sora-2/create">
    创建 Sora 2 视频任务。
  </Card>

  <Card title="获取 Sora 2 视频" icon="refresh" href="/api/video/sora-2/retrieve">
    查询 Sora 视频任务。
  </Card>

  <Card title="创建 Veo 3 视频" icon="film" href="/api/video/veo3/create">
    创建 Veo 视频任务。
  </Card>

  <Card title="创建 Wan 视频" icon="sparkles" href="/api/video/wan/create">
    创建 Wan 文生视频任务。
  </Card>

  <Card title="创建 xAI 视频" icon="film" href="/api/video/xai/video-generation">
    生成 xAI 视频任务。
  </Card>

  <Card title="创建 Vidu 视频" icon="sparkles" href="/api/video/vidu/create">
    创建 Vidu 文生视频任务。
  </Card>

  <Card title="创建 Omni 视频（Beta）" icon="sparkles" href="/api/video/omni/create">
    创建测试版 Omni 视频任务。
  </Card>

  <Card title="创建 Kling 文生视频任务" icon="film" href="/api/video/kling/text-to-video">
    根据文本 Prompt 生成 Kling 视频。
  </Card>

  <Card title="创建 Runway 图生视频任务" icon="film" href="/api/video/runway/official-format/runway-images-raw-video">
    根据图像生成 Runway 视频。
  </Card>
</CardGroup>

## 创建并轮询视频任务

使用 [Models 页面](/zh-Hans/overview/models) 或 [模型目录](https://www.cometapi.com/models/)中的支持视频的 model ID。下面的示例先通过 `POST /v1/videos` 创建一个视频任务，然后持续轮询返回的任务 ID，直到任务进入终态。

<Note>
  这些示例使用占位符 `your-video-model-id`。在运行请求之前，请将其替换为 [Models 页面](/zh-Hans/overview/models) 或 [模型目录](https://www.cometapi.com/models/)中可用的视频 model ID。
</Note>

<Tip>
  打开 [创建 Seedance 视频](/api/video/seedance/create) 和 [获取 Seedance 视频](/api/video/seedance/query)，以使用 API playground 和端点 schema。
</Tip>

<CodeGroup>
  ```python Python theme={null}
  import os
  import time
  import requests

  headers = {"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]}

  create_response = requests.post(
      "https://api.cometapi.com/v1/videos",
      headers=headers,
      data={
          "model": "your-video-model-id",
          "prompt": "A calm camera move across a desk with a paper airplane",
      },
      timeout=30,
  )
  create_response.raise_for_status()
  task = create_response.json()
  task_id = task["id"]

  terminal_statuses = {"completed", "failed", "error"}

  while True:
      poll_response = requests.get(
          f"https://api.cometapi.com/v1/videos/{task_id}",
          headers=headers,
          timeout=30,
      )
      poll_response.raise_for_status()
      result = poll_response.json()
      print(result["status"], result.get("progress"))

      if result["status"] in terminal_statuses:
          print(result.get("video_url"))
          break

      time.sleep(10)
  ```

  ```javascript Node.js theme={null}
  const form = new FormData();
  form.append("model", "your-video-model-id");
  form.append("prompt", "A calm camera move across a desk with a paper airplane");

  const createResponse = await fetch("https://api.cometapi.com/v1/videos", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
    },
    body: form,
  });

  if (!createResponse.ok) {
    throw new Error(await createResponse.text());
  }

  const task = await createResponse.json();
  const terminalStatuses = new Set(["completed", "failed", "error"]);

  while (true) {
    const pollResponse = await fetch(
      `https://api.cometapi.com/v1/videos/${task.id}`,
      {
        headers: {
          Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
        },
      },
    );

    if (!pollResponse.ok) {
      throw new Error(await pollResponse.text());
    }

    const result = await pollResponse.json();
    console.log(result.status, result.progress);

    if (terminalStatuses.has(result.status)) {
      console.log(result.video_url);
      break;
    }

    await new Promise((resolve) => setTimeout(resolve, 10_000));
  }
  ```

  ```bash cURL theme={null}
  curl https://api.cometapi.com/v1/videos \
    -H "Authorization: Bearer $COMETAPI_KEY" \
    -F "model=your-video-model-id" \
    -F "prompt=A calm camera move across a desk with a paper airplane"

  curl https://api.cometapi.com/v1/videos/task_example \
    -H "Authorization: Bearer $COMETAPI_KEY"
  ```
</CodeGroup>

## 响应示例

成功的创建响应可能如下所示。请在轮询之前先保存任务 ID：

```json theme={null}
{
  "id": "task_example",
  "task_id": "task_example",
  "object": "video",
  "model": "your-video-model-id",
  "status": "queued",
  "progress": 0,
  "created_at": 1779872000
}
```

成功的轮询响应可能如下所示。已完成的响应可能包含 `video_url`；某些提供商格式会使用模型特定的结果字段，或在已记录该路由时使用视频内容路由：

```json theme={null}
{
  "id": "task_example",
  "object": "video",
  "model": "your-video-model-id",
  "status": "completed",
  "progress": 100,
  "completed_at": 1779872300,
  "video_url": "https://example.com/generated-video.mp4"
}
```

## 示例模型记录

<Info>
  此示例模型目录响应展示了 `/api/models` 的响应封装以及一个视频模型记录的结构。它不是完整的模型列表。
</Info>

```bash cURL theme={null}
curl https://api.cometapi.com/api/models
```

```json theme={null}
{
  "success": true,
  "page": 1,
  "page_size": 20,
  "total": 302,
  "data": [
    {
      "created": 1767529753,
      "id": "your-video-model-id",
      "code": "your-video-model-id",
      "provider": "ExampleProvider",
      "provider_code": "example",
      "name": "Example video model",
      "model_type": "video",
      "features": [
        "text-to-video"
      ],
      "endpoints": "{\n  \"seedance\": {\n    \"path\": \"/v1/videos\",\n    \"method\": \"POST\"\n  }\n}",
      "pricing": {
        "currency": "USD / M Tokens",
        "input": null,
        "output": null,
        "per_request": null,
        "per_second": 0.024
      }
    }
  ]
}
```

## 常见错误

<AccordionGroup>
  <Accordion title="Missing task ID">
    在你的作业处理器返回之前，先保存创建响应中的 ID。
  </Accordion>

  <Accordion title="Polling too fast">
    在状态检查之间增加延迟和退避。
  </Accordion>

  <Accordion title="Unsupported duration or size">
    使用为所选视频端点记录的 duration 和 resolution 字段。
  </Accordion>

  <Accordion title="Missing video_url">
    将 `video_url` 视为可选，并在可用时回退到模型特定的结果字段或 content 路由。
  </Accordion>

  <Accordion title="Callback not received">
    使用轮询作为事实来源，并验证你的回调 URL 接受 POST 请求。
  </Accordion>
</AccordionGroup>

## 错误码与重试策略

<AccordionGroup>
  <Accordion title="400">
    在修正 prompt、文件、duration 或 size 字段之前，不要重试。
  </Accordion>

  <Accordion title="401">
    在 API key 存在且有效之前，不要重试。
  </Accordion>

  <Accordion title="404">
    重试前请检查 task ID、base URL、路径和 model ID。
  </Accordion>

  <Accordion title="413">
    重试前请减小上传大小。
  </Accordion>

  <Accordion title="429">
    使用指数退避重试，并降低创建或轮询的并发度。
  </Accordion>

  <Accordion title="500 or 503">
    使用退避重试任务创建；除非任务进入终态错误，否则继续轮询现有任务。
  </Accordion>
</AccordionGroup>

<Tip>
  有关实现模式，请参阅 [错误码与重试策略](/zh-Hans/guides/error-codes-and-retry-strategy)、[速率限制与并发](/zh-Hans/guides/rate-limits-and-concurrency) 和 [用于视频生成的 Webhook 与轮询](/zh-Hans/guides/webhook-and-polling-for-video-generation)。
</Tip>

## 定价与模型目录

<CardGroup cols={3}>
  <Card title="Models page" icon="list" href="/overview/models">
    了解 CometAPI 如何在文档中公开 model ID。
  </Card>

  <Card title="Model directory" icon="puzzle-piece" href="https://www.cometapi.com/models/">
    浏览可用模型及其能力。
  </Card>

  <Card title="Pricing" icon="tag" href="https://www.cometapi.com/pricing/">
    在调用模型前查看定价。
  </Card>
</CardGroup>
