> ## 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.

# Tạo tác vụ chuyển hình ảnh thành video Kling theo mô hình

> Tạo video Kling từ hình ảnh nguồn bằng tuyến dành riêng cho mô hình, sau đó truy xuất video bằng ID tác vụ.

Gửi hình ảnh nguồn đến `POST /image-to-video/{model}` để tạo tác vụ video. Bạn có thể thêm Prompt để mô tả chuyển động. Phản hồi chứa `id` mà bạn dùng để truy xuất kết quả.

## Chọn mô hình

Đặt ID mô hình trong URL. Không thêm trường model vào phần nội dung JSON.

| ID mô hình | Độ phân giải | Thời lượng | Âm thanh |
| - | - | - | - |
| `kling-4.0` | `720p`, `1080p` | Số nguyên từ 3 đến 30 giây | Bỏ qua `audio` |
| `kling-3.0-turbo` | `720p`, `1080p` | Số nguyên từ 3 đến 15 giây | Bỏ qua `audio` |
| `kling-3.0` | `720p`, `1080p`, `4k` | Số nguyên từ 3 đến 15 giây | `native` hoặc `off` |
| `kling-2.6` | `720p`, `1080p` | 5 hoặc 10 giây | `native` hoặc `off`; sử dụng `1080p` với `native` |
| `kling-2.5-turbo` | `720p`, `1080p` | 5 hoặc 10 giây | Bỏ qua `audio` |

`kling-4.0` chấp nhận `first_frame`, `middle_frame` và `last_frame`; hãy thêm `first_frame` khi sử dụng `last_frame`. `kling-3.0-turbo` chỉ chấp nhận `first_frame`. `kling-2.5-turbo`,
`kling-3.0`, và `kling-2.6` cũng hỗ trợ `last_frame`; hãy thêm
`first_frame` cùng với giá trị đó. Đối với `kling-2.5-turbo` và `kling-2.6`,
tổ hợp khung hình đầu và cuối yêu cầu `1080p`.

## Tham số yêu cầu

Đặt từng đầu vào trong `contents`. Một mục `prompt` sử dụng `text`; một mục khung hình sử dụng `url`. Đối với hình ảnh, cung cấp URL có thể truy cập công khai hoặc dữ liệu JPG hoặc PNG được mã hóa Base64.

| Trường | Kiểu | Bắt buộc | Mô tả |
| - | - | - | - |
| Đường dẫn `model` | chuỗi | Có | ID mô hình trong URL. Chọn một ID từ bảng trên; không thêm ID đó vào nội dung yêu cầu. |
| `contents` | mảng | Có | Các mục đầu vào. Bao gồm một hình ảnh `first_frame`; thêm một mục `prompt` hoặc một hình ảnh `last_frame` được mô hình hỗ trợ khi cần. |
| `contents[].type` | chuỗi | Có | `prompt`, `first_frame`, hoặc `last_frame`. Bao gồm `first_frame` khi sử dụng `last_frame`; mô hình được chọn quyết định liệu có thể sử dụng `last_frame` hay không. |
| `contents[].text` | chuỗi | Đối với mục `prompt` | Văn bản mô tả chuyển động. |
| `contents[].url` | chuỗi | Đối với mục khung hình | URL hình ảnh công khai hoặc dữ liệu JPG hoặc PNG được mã hóa Base64. |
| `settings.resolution` | chuỗi | Không | Độ phân giải đầu ra theo hàng của mô hình được chọn trong bảng trên. Giá trị mặc định là `720p`. |
| `settings.duration` | số nguyên | Không | Độ dài video tính bằng giây theo hàng của mô hình được chọn trong bảng trên. Giá trị mặc định là `10` đối với `kling-4.0` và `5` đối với các mô hình khác. Gửi một số nguyên, không phải chuỗi. |
| `settings.multi_shot` | boolean | Không | Chỉ đối với `kling-3.0`, giá trị mặc định chính thức là `true`. Đặt `true` cho nhiều cảnh quay hoặc `false` cho một cảnh quay, và đặt giá trị tường minh khi sử dụng trường này. |
| `settings.audio` | chuỗi | Không | Đối với `kling-3.0` hoặc `kling-2.6`, đặt `native` để tạo âm thanh hoặc `off` để không tạo âm thanh. Đặt giá trị tường minh khi sử dụng trường này. |
| `options.callback_url` | chuỗi | Không | URL công khai nhận các bản cập nhật tác vụ. |
| `options.external_task_id` | chuỗi | Không | Mã định danh của bạn để đối chiếu tác vụ với ứng dụng. |
| `options.watermark_info` | đối tượng | Không | Cấu hình hình mờ cho đầu ra. |

`kling-4.0` không cung cấp tùy chọn `settings.audio`. Video của mô hình này có chứa một bản âm thanh; hãy bỏ qua trường này thay vì gửi một giá trị mà mô hình sẽ bỏ qua.

Yêu cầu sau tạo chuyển động cho một ảnh nguồn thành video dài năm giây:

```json theme={null}
{
  "contents": [
    {
      "type": "prompt",
      "text": "The camera slowly moves closer to the scene."
    },
    {
      "type": "first_frame",
      "url": "https://raw.githubusercontent.com/ultralytics/yolov5/402e17ddf820996f51a191cbb798376e1144f069/data/images/zidane.jpg"
    }
  ],
  "settings": {
    "resolution": "720p",
    "duration": 5
  }
}
```

Thay URL hình ảnh bằng URL công khai hoặc dữ liệu JPG/PNG được mã hóa Base64 của ảnh nguồn khi bạn sử dụng ảnh khác. Gửi nội dung này đến `https://api.cometapi.com/image-to-video/kling-3.0-turbo` cùng với khóa API CometAPI của bạn. Bảng mã OpenAPI cung cấp các yêu cầu Shell, Python và JavaScript có cùng nội dung.

## Truy xuất video

Phản hồi từ yêu cầu tạo có dạng sau:

```json theme={null}
{
  "code": 0,
  "message": "SUCCEED",
  "data": {
    "id": "example-task-id",
    "status": "submitted"
  }
}
```

Lưu `data.id`. Sử dụng [Lấy tác vụ Kling](https://apidoc.cometapi.com/vi/api/video/kling/model-routes/tasks.md) với ID đó cho đến khi tác vụ đạt trạng thái kết thúc. Lấy URL video từ mảng `outputs` của tác vụ.


## OpenAPI

````yaml api/openapi/video/kling/model-routes/post-image-to-video.openapi.json POST /image-to-video/{model}
openapi: 3.1.0
info:
  title: Kling image-to-video model route
  version: 1.0.0
  description: Create a Kling video task from a source image, with an optional prompt.
servers:
  - url: https://api.cometapi.com
security:
  - bearerAuth: []
paths:
  /image-to-video/{model}:
    post:
      summary: Create an image-to-video task
      description: >-
        Submit a source image with optional motion text, then receive a task ID
        for retrieving the generated video.
      operationId: createKlingModelImageToVideoTask
      parameters:
        - name: model
          in: path
          required: true
          description: >-
            Kling model ID. `kling-4.0` accepts 3-30 second durations and
            `middle_frame` inputs. `kling-3.0` supports 4k resolution. The 2.x
            models accept 5- or 10-second duration.
          schema:
            type: string
            enum:
              - kling-4.0
              - kling-3.0-turbo
              - kling-3.0
              - kling-2.6
              - kling-2.5-turbo
            default: kling-3.0-turbo
          example: kling-3.0-turbo
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ImageToVideoRequest'
            example:
              contents:
                - type: prompt
                  text: The camera slowly moves closer to the scene.
                - type: first_frame
                  url: >-
                    https://raw.githubusercontent.com/ultralytics/yolov5/402e17ddf820996f51a191cbb798376e1144f069/data/images/zidane.jpg
              settings:
                resolution: 720p
                duration: 5
      responses:
        '200':
          description: >-
            The task was created. Use `data.id` to retrieve its status and video
            output.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CreateTaskResponse'
              example:
                code: 0
                message: SUCCEED
                data:
                  id: example-task-id
                  status: submitted
      x-codeSamples:
        - lang: Shell
          label: Create task
          source: |
            curl https://api.cometapi.com/image-to-video/kling-3.0-turbo \
              -X POST \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -H "Content-Type: application/json" \
              --data-raw '{
              "contents": [
                {
                  "type": "prompt",
                  "text": "The camera slowly moves closer to the scene."
                },
                {
                  "type": "first_frame",
                  "url": "https://raw.githubusercontent.com/ultralytics/yolov5/402e17ddf820996f51a191cbb798376e1144f069/data/images/zidane.jpg"
                }
              ],
              "settings": {
                "resolution": "720p",
                "duration": 5
              }
            }'
        - lang: Python
          label: Create task
          source: |
            import os
            import requests

            response = requests.post(
                "https://api.cometapi.com/image-to-video/kling-3.0-turbo",
                headers={"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]},
                json={
                    "contents": [
                        {
                            "type": "prompt",
                            "text": "The camera slowly moves closer to the scene.",
                        },
                        {
                            "type": "first_frame",
                            "url": "https://raw.githubusercontent.com/ultralytics/yolov5/402e17ddf820996f51a191cbb798376e1144f069/data/images/zidane.jpg",
                        },
                    ],
                    "settings": {
                        "resolution": "720p",
                        "duration": 5
                    },
                },
            )
            response.raise_for_status()
            print(response.json()["data"]["id"])
        - lang: JavaScript
          label: Create task
          source: |
            const response = await fetch(
              "https://api.cometapi.com/image-to-video/kling-3.0-turbo",
              {
                method: "POST",
                headers: {
                  Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
                  "Content-Type": "application/json",
                },
                body: JSON.stringify({
                  contents: [
                    { type: "prompt", text: "The camera slowly moves closer to the scene." },
                    {
                      type: "first_frame",
                      url: "https://raw.githubusercontent.com/ultralytics/yolov5/402e17ddf820996f51a191cbb798376e1144f069/data/images/zidane.jpg",
                    },
                  ],
                  settings: {
                    resolution: "720p",
                    duration: 5,
                  },
                }),
              },
            );
            if (!response.ok) throw new Error(`HTTP ${response.status}`);
            console.log((await response.json()).data.id);
components:
  schemas:
    ImageToVideoRequest:
      type: object
      required:
        - contents
      properties:
        contents:
          type: array
          minItems: 1
          description: >-
            Image and text inputs exposed by the CometAPI route. Include a
            `first_frame` image. Add a `prompt` entry or a model-supported
            `last_frame` image when needed.
          contains:
            type: object
            required:
              - type
            properties:
              type:
                const: first_frame
                description: >-
                  Identifies the source image required for image-to-video
                  generation.
          minContains: 1
          items:
            type: object
            required:
              - type
            properties:
              type:
                type: string
                enum:
                  - prompt
                  - first_frame
                  - last_frame
                description: >-
                  Input role. Use `prompt` for motion text, `first_frame` for
                  the source image, or `last_frame` for the target ending image.
                  `kling-3.0-turbo` accepts `first_frame` only;
                  `kling-2.5-turbo`, `kling-3.0`, and `kling-2.6` also support
                  `last_frame`. For `kling-2.5-turbo` and `kling-2.6`, a
                  first-and-last-frame request requires `1080p`.
              text:
                type: string
                description: Motion description for a `prompt` entry.
              url:
                type: string
                description: >-
                  Public image URL or Base64-encoded JPG or PNG data for a frame
                  entry.
            allOf:
              - if:
                  properties:
                    type:
                      const: prompt
                      description: Identifies an optional motion prompt.
                then:
                  required:
                    - text
                else:
                  required:
                    - url
            additionalProperties: false
            description: Frame and prompt content fields exposed by the CometAPI route.
        settings:
          $ref: '#/components/schemas/VideoSettings'
        options:
          $ref: '#/components/schemas/TaskOptions'
      additionalProperties: false
      description: >-
        Image-to-video request fields exposed by the CometAPI route. The model
        ID is a path parameter, not a body field.
    CreateTaskResponse:
      type: object
      required:
        - code
        - message
        - data
      properties:
        code:
          oneOf:
            - type: integer
            - type: string
          description: Request result code. `0` or `success` indicates acceptance.
        message:
          type: string
          description: Result message.
        msg:
          type: string
          description: Additional result message when included in the response.
        data:
          type: object
          description: Task identifier and submission status.
          required:
            - id
            - status
          properties:
            id:
              type: string
              description: Task ID to use in `GET /tasks?task_ids={id}`.
            status:
              type: string
              description: Task status at submission.
    VideoSettings:
      type: object
      description: >-
        Video generation settings exposed by the CometAPI route. The model ID in
        the URL determines which values are accepted.
      properties:
        resolution:
          type: string
          enum:
            - 720p
            - 1080p
            - 4k
          default: 720p
          description: >-
            Output resolution. `4k` is available with `kling-3.0`; the other
            listed models accept `720p` or `1080p`.
        duration:
          type: integer
          minimum: 3
          maximum: 30
          description: >-
            Video length in seconds. Use an integer from 3 to 30 for
            `kling-4.0`, 3 to 15 for the 3.0 models, or 5 or 10 for the 2.x
            models. If omitted, the request uses 10 seconds for `kling-4.0` and
            5 seconds for the other models. Send a JSON number, not a string. A
            `kling-2.5-turbo` or `kling-2.6` request with both `first_frame` and
            `last_frame` requires `1080p`.
        multi_shot:
          type: boolean
          description: >-
            For `kling-3.0` only, the official default is `true`. Set `true` for
            multiple shots or `false` for one shot, and set the value explicitly
            when you use this field.
        audio:
          type: string
          enum:
            - native
            - 'off'
          description: >-
            For `kling-3.0` or `kling-2.6`, use `native` for generated audio or
            `off` to disable it. Set the value explicitly when you use this
            field. With `kling-2.6`, `native` requires `1080p`. Omit this field
            for the Turbo models.
      additionalProperties: false
    TaskOptions:
      type: object
      description: Optional task fields exposed by the CometAPI route.
      properties:
        callback_url:
          type: string
          format: uri
          description: Public URL that receives task updates.
        external_task_id:
          type: string
          description: >-
            Your identifier for matching this task to a record in your
            application.
        watermark_info:
          type: object
          description: Watermark configuration for the generated video.
          additionalProperties: true
      additionalProperties: false
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Authenticate with your CometAPI API key.

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.