> ## 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 tạo video

> Chọn các tuyến video của CometAPI cho quy trình Seedance, HappyHorse, Sora 2, Veo 3, Wan, xAI, Vidu, Omni, Kling và Runway.

Sử dụng tài liệu mô hình video của CometAPI bằng cách chọn quy trình nhà cung cấp phù hợp với loại tác vụ của bạn. Hầu hết các endpoint video tạo tác vụ bất đồng bộ, vì vậy hãy lưu task ID và dùng polling để lấy kết quả. Chỉ thêm callback khi trang dành riêng cho từng mô hình có tài liệu về hỗ trợ callback.

## Chọn một video API

<CardGroup cols={2}>
  <Card title="Tạo video Seedance" icon="sparkles" href="/api/video/seedance/create">
    Tạo các tác vụ video Seedance.
  </Card>

  <Card title="Tạo video HappyHorse" icon="sparkles" href="/api/video/happyhorse/create">
    Tạo job text-to-video HappyHorse.
  </Card>

  <Card title="Tạo video Sora 2" icon="film" href="/api/video/sora-2/create">
    Tạo job video Sora 2.
  </Card>

  <Card title="Truy xuất video Sora 2" icon="refresh" href="/api/video/sora-2/retrieve">
    Truy vấn các job video Sora.
  </Card>

  <Card title="Tạo video Veo 3" icon="film" href="/api/video/veo3/create">
    Tạo job video Veo.
  </Card>

  <Card title="Tạo video Wan" icon="sparkles" href="/api/video/wan/create">
    Tạo job text-to-video Wan.
  </Card>

  <Card title="Tạo video xAI" icon="film" href="/api/video/xai/video-generation">
    Tạo các job video xAI.
  </Card>

  <Card title="Tạo video Vidu" icon="sparkles" href="/api/video/vidu/create">
    Tạo job text-to-video Vidu.
  </Card>

  <Card title="Tạo video Omni (Beta)" icon="sparkles" href="/api/video/omni/create">
    Tạo các job video Omni bản beta.
  </Card>

  <Card title="Tạo tác vụ Kling text-to-video" icon="film" href="/api/video/kling/text-to-video">
    Tạo video Kling từ prompt văn bản.
  </Card>

  <Card title="Tạo tác vụ Runway image-to-video" icon="film" href="/api/video/runway/official-format/runway-images-raw-video">
    Tạo video Runway từ hình ảnh.
  </Card>
</CardGroup>

## Tạo và polling một video task

Sử dụng model ID có hỗ trợ video từ [trang Models](/vi/overview/models) hoặc [danh mục model](https://www.cometapi.com/models/). Các ví dụ bên dưới tạo một video task bằng `POST /v1/videos`, sau đó polling task ID được trả về cho đến khi task đạt tới trạng thái kết thúc.

<Note>
  Các ví dụ này sử dụng giá trị giữ chỗ `your-video-model-id`. Hãy thay thế nó bằng một model ID video khả dụng từ [trang Models](/vi/overview/models) hoặc [danh mục model](https://www.cometapi.com/models/) trước khi bạn chạy request.
</Note>

<Tip>
  Mở [Tạo một video Seedance](/api/video/seedance/create) và [Truy xuất một video Seedance](/api/video/seedance/query) để sử dụng các API playground và schema endpoint.
</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>

## Ví dụ response

Một response tạo thành công có thể trông như sau. Hãy lưu task ID trước khi polling:

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

Một response polling thành công có thể trông như sau. Các response đã hoàn tất có thể bao gồm `video_url`; một số định dạng provider sử dụng các trường kết quả đặc thù theo model hoặc route nội dung video khi route đó được ghi nhận trong tài liệu:

```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"
}
```

## Bản ghi model ví dụ

<Info>
  Phản hồi danh mục model ví dụ này hiển thị cấu trúc bao `/api/models` và hình dạng của một bản ghi model video. Đây không phải là danh sách model đầy đủ.
</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
      }
    }
  ]
}
```

## Lỗi thường gặp

<AccordionGroup>
  <Accordion title="Missing task ID">
    Lưu ID từ phản hồi tạo trước khi trả về từ bộ xử lý job của bạn.
  </Accordion>

  <Accordion title="Polling too fast">
    Thêm độ trễ và backoff giữa các lần kiểm tra trạng thái.
  </Accordion>

  <Accordion title="Unsupported duration or size">
    Sử dụng các trường thời lượng và độ phân giải được ghi trong tài liệu cho endpoint video đã chọn.
  </Accordion>

  <Accordion title="Missing video_url">
    Xử lý `video_url` như một trường tùy chọn và quay về dùng các trường kết quả dành riêng cho model hoặc route content khi có sẵn.
  </Accordion>

  <Accordion title="Callback not received">
    Dùng polling làm nguồn thông tin chính xác và xác minh rằng URL callback của bạn chấp nhận các yêu cầu POST.
  </Accordion>
</AccordionGroup>

## Mã lỗi và chiến lược retry

<AccordionGroup>
  <Accordion title="400">
    Không retry cho đến khi các trường prompt, file, thời lượng hoặc kích thước được sửa.
  </Accordion>

  <Accordion title="401">
    Không retry cho đến khi API key có mặt và hợp lệ.
  </Accordion>

  <Accordion title="404">
    Kiểm tra task ID, base URL, path và model ID trước khi retry.
  </Accordion>

  <Accordion title="413">
    Giảm kích thước tải lên trước khi retry.
  </Accordion>

  <Accordion title="429">
    Retry với exponential backoff và giảm mức đồng thời của việc tạo hoặc polling.
  </Accordion>

  <Accordion title="500 or 503">
    Retry việc tạo task với backoff; tiếp tục polling các task hiện có trừ khi task gặp lỗi kết thúc.
  </Accordion>
</AccordionGroup>

<Tip>
  Để xem các mẫu triển khai, hãy xem [Mã lỗi và chiến lược retry](/vi/guides/error-codes-and-retry-strategy), [Giới hạn tốc độ và mức đồng thời](/vi/guides/rate-limits-and-concurrency), và [Webhook và polling cho việc tạo video](/vi/guides/webhook-and-polling-for-video-generation).
</Tip>

## Giá và danh mục model

<CardGroup cols={3}>
  <Card title="Models page" icon="list" href="/overview/models">
    Đọc cách CometAPI hiển thị model ID trong tài liệu.
  </Card>

  <Card title="Model directory" icon="puzzle-piece" href="https://www.cometapi.com/models/">
    Duyệt tình trạng sẵn có và khả năng của model.
  </Card>

  <Card title="Pricing" icon="tag" href="https://www.cometapi.com/pricing/">
    Kiểm tra giá trước khi bạn gọi một model.
  </Card>
</CardGroup>
