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

# 建立 Kling Motion Control 任務

> 使用相容的 CometAPI 路由，根據角色圖片和參考動作影片建立 Kling Motion Control 任務。

使用此端點，根據角色圖片和一段
參考影片建立 Motion Control 任務。

> [!WARNING]
>
> 本頁說明相容的 Motion Control 路由。Kling Video 3.0
> Motion Control 使用獨立的 API 合約。

## 必要媒體

`image_url` 接受公開 URL 或原始 Base64 字串。

* 請使用 10 MB 以下的 JPG、JPEG 或 PNG 圖片。
* 每個圖片維度須介於 300 至 65,536 像素之間。
* 請使用介於 1:2.5 至 2.5:1 的長寬比。
* 請將 Base64 以不含
  `data:image/...;base64,` 前綴的原始編碼字串傳送。
* 僅顯示一名未受遮擋的人物，且身體取景須與動作
  參考素材相符。

`video_url` 接受公開的 MP4 或 MOV URL。

* 請使用 100 MB 以下的影片。
* 短邊至少須為 340 像素。
* 長邊不得超過 3850 像素。
* 請使用僅有一名可見人物的連續鏡頭。
* 避免剪接、鏡頭變換及過快的動作。

> [!NOTE]
>
> 請遵守時長限制，並檢查巢狀的終態 `task_status`。外層
> HTTP 200 或回應 `code: 0` 僅確認查詢回應，而非
> 生成成功結果。

## 設定 orientation 值

`character_orientation` 為必填欄位，並接受 `image` 或 `video`。

| 值 | 參考影片時長 |
| - | - |
| `image` | 3–10 秒 |
| `video` | 3–30 秒 |

若使用 `element_list`，請將 `character_orientation` 設為 `video`。

## 選擇模型和模式

相容路由接受 `kling-v2-6` 和 `kling-v3`。兩個模型值
皆接受兩個模式值：

| 模型 | `std` | `pro` |
| - | - | - |
| `kling-v2-6` | 支援 | 支援 |
| `kling-v3` | 支援 | 支援 |

若省略 `model_name`，請求會使用 `kling-v2-6`。若省略 `mode`，
請求會使用 `std`。`kling-v3` 值會保留本頁所述的相容請求結構；
不會選取獨立的 Kling 3.0 路徑版本
合約。

相容合約不保證固定的輸出解析度。若您的應用程式需要特定尺寸，請檢查
每個傳回的影片。

`keep_original_sound` 接受 `yes` 或 `no`。若省略此欄位，
請求會使用 `yes`。

## 任務流程

**1. 提交 Motion Control 請求**

傳送來源圖片、參考影片和 orientation 值。選擇一個
模型、模式和聲音值，或使用其文件中記載的預設值。儲存
傳回的 `task_id`。

**2. 輪詢任務**

使用 [取得 Kling 任務](https://apidoc.cometapi.com/zh-Hant/api/video/kling/individual-queries.md) 搭配傳回的 `task_id`。
持續查詢，直到狀態為 `succeed` 或 `failed`。

**3. 儲存結果**

請盡快下載並儲存結果。Kling 的相容 API
文件指出，生成的影片會在 30 天後清除。請勿
假設傳回的 URL 在完整 30 天期間內都可存取。

## 選用欄位

| 欄位 | 結構 | 限制 |
| - | - | - |
| `prompt` | 字串，最多 2500 個字元 | 相容請求結構中的選用文字欄位。 |
| `callback_url` | URI 字串或空字串 | 未設定回呼 URI 時，請省略此欄位或使用空字串。 |
| `external_task_id` | 對您的帳戶唯一的字串 | 將任務與您的應用程式建立關聯。它無法取代用於 CometAPI 查詢的傳回 `task_id`。 |
| `element_list` | 最多包含一個 `{"element_id": 123}` 物件的陣列 | 僅將此欄位與 `character_orientation: "video"`. |
| `watermark_info` | `{"enabled": boolean}` | 相容的浮水印結構僅包含 `enabled` Boolean。 |

## 回呼結構

Legacy 回呼結構描述具有下列結構：

```json theme={null}
{
  "task_id": "<task_id>",
  "task_status": "succeed",
  "task_status_msg": "",
  "created_at": 1785398400000,
  "updated_at": 1785398460000,
  "final_unit_deduction": "<value>",
  "final_balance_deduction": {
    "quota": "<value>",
    "list_price": "<value>"
  },
  "task_info": {
    "external_task_id": "<your_unique_id>"
  },
  "task_result": {
    "videos": [
      {
        "id": "<video_id>",
        "url": "https://media.example.com/<file_id>.mp4",
        "duration": "6.4"
      }
    ]
  }
}
```

回呼狀態可以是 `submitted`、`processing`、`succeed` 或 `failed`。
終態結果欄位僅會在終態回傳時出現。

## 結果欄位

| 欄位 | 類型 | 說明 |
| - | - | - |
| `task_result.videos[].id` | 字串 | 生成的影片 ID。 |
| `task_result.videos[].url` | URI 字串 | 生成影片的傳送 URL。 |
| `task_result.videos[].watermark_url` | URI 字串 | 任務傳回浮水印影片時的影片傳送 URL。 |
| `task_result.videos[].duration` | 字串 | 生成影片的時長（秒）。 |
| `final_unit_deduction` | 字串 | 隨任務傳回的最終單位扣除值。 |
| `final_balance_deduction` | `{"quota": string, "list_price": string}` | 隨任務傳回的最終餘額扣除值。 |

任務狀態為 `submitted`、`processing`、`succeed` 或 `failed`。

> [!TIP]
>
> 請參閱 [Kling Motion Control API 參考資料](https://kling.ai/document-api/api/video/motion-control/legacy)
> 以及 [Kling 回呼通訊協定](https://kling.ai/document-api/api/get-started/callbacks)
> 以瞭解 API 詳細資料。


## OpenAPI

````yaml api/openapi/video/kling/post-motion-control.openapi.json POST /kling/v1/videos/motion-control
openapi: 3.1.0
info:
  title: Kling Motion Control API
  version: 1.0.0
  description: >-
    Create a Kling Motion Control task from a character image and a reference
    motion video.
servers:
  - url: https://api.cometapi.com
security:
  - bearerAuth: []
paths:
  /kling/v1/videos/motion-control:
    post:
      summary: Create a Kling Motion Control task
      description: >-
        Submit a character image and a reference motion video. The response
        contains a task ID that you can use for status queries.
      operationId: create_kling_motion_control
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - image_url
                - video_url
                - character_orientation
              properties:
                model_name:
                  type: string
                  description: >-
                    Model ID for this compatible Motion Control request. Omit
                    this field to use `kling-v2-6`. The `kling-v3` value keeps
                    this compatible request shape; it does not select the
                    separate Kling 3.0 path-version contract.
                  enum:
                    - kling-v2-6
                    - kling-v3
                  default: kling-v2-6
                image_url:
                  type: string
                  description: >-
                    Character image as a public URL or a raw Base64 string. Send
                    raw Base64 without a `data:image/...;base64,` prefix;
                    data-URI input is outside the compatible contract. Supported
                    formats are JPG, JPEG, and PNG. The image must be 10 MB or
                    smaller. Its width and height must each be from 300 through
                    65,536 pixels, and its aspect ratio must be between 1:2.5
                    and 2.5:1.
                video_url:
                  type: string
                  format: uri
                  description: >-
                    Public reference motion video URL. Use an MP4 or MOV file
                    that is 100 MB or smaller. The short edge must be at least
                    340 pixels, and the long edge must not exceed 3850 pixels.
                    The video must be at least 3 seconds long. The maximum
                    duration depends on `character_orientation`.
                prompt:
                  type: string
                  maxLength: 2500
                  description: >-
                    Optional text field in the compatible request structure.
                    Maximum 2500 characters.
                keep_original_sound:
                  type: string
                  description: >-
                    Compatible string enum. Accepted values are `yes` and `no`.
                    Omitted requests use `yes`.
                  enum:
                    - 'yes'
                    - 'no'
                  default: 'yes'
                character_orientation:
                  type: string
                  description: >-
                    Required compatible string enum. With `image`, the reference
                    video can be 3 to 10 seconds long. With `video`, the
                    reference video can be 3 to 30 seconds long.
                  enum:
                    - image
                    - video
                mode:
                  type: string
                  description: >-
                    Both compatible models accept `std` and `pro`. Omitted
                    requests use `std`.
                  enum:
                    - std
                    - pro
                  default: std
                callback_url:
                  description: >-
                    Optional callback field in the compatible structure. Provide
                    a URI, or omit the field or send an empty string when no
                    callback URI is configured.
                  oneOf:
                    - type: string
                      format: uri
                    - type: string
                      const: ''
                external_task_id:
                  type: string
                  description: >-
                    Optional ID for correlation in your application. The value
                    must be unique for your account. Store the returned
                    `task_id` for CometAPI status queries.
                element_list:
                  type: array
                  maxItems: 1
                  description: >-
                    Optional compatible Element structure. Provide at most one
                    object, and combine this field only with
                    `character_orientation: video`.
                  items:
                    type: object
                    required:
                      - element_id
                    properties:
                      element_id:
                        type: integer
                        format: int64
                        description: Kling Element ID in the compatible structure.
                    additionalProperties: false
                watermark_info:
                  type: object
                  description: Optional compatible watermark structure.
                  required:
                    - enabled
                  properties:
                    enabled:
                      type: boolean
                      description: Boolean flag in the compatible watermark structure.
                  additionalProperties: false
              example:
                model_name: kling-v3
                image_url: https://your-image-host.example.com/character.png
                video_url: https://your-video-host.example.com/reference-motion.mp4
                prompt: Studio scene.
                keep_original_sound: 'no'
                character_orientation: video
                mode: std
            examples:
              Image URL:
                summary: Use a public character image URL
                value:
                  model_name: kling-v3
                  image_url: https://your-image-host.example.com/character.png
                  video_url: https://your-video-host.example.com/reference-motion.mp4
                  prompt: Studio scene.
                  keep_original_sound: 'no'
                  character_orientation: video
                  mode: std
              Image base64:
                summary: Use a raw Base64 character image
                value:
                  model_name: kling-v3
                  image_url: <your-image-base64>
                  video_url: https://your-video-host.example.com/reference-motion.mp4
                  prompt: Studio scene.
                  keep_original_sound: 'no'
                  character_orientation: video
                  mode: pro
      responses:
        '200':
          description: Task accepted.
          content:
            application/json:
              schema:
                type: object
                required:
                  - code
                  - message
                  - data
                properties:
                  code:
                    type: integer
                    description: >-
                      Response code. A value of 0 indicates that the request was
                      accepted.
                  message:
                    type: string
                    description: Response message.
                  data:
                    type: object
                    required:
                      - task_id
                      - task_status
                      - task_info
                      - created_at
                      - updated_at
                    properties:
                      task_id:
                        type: string
                        description: System-generated task ID for status queries.
                      task_status:
                        type: string
                        description: Task state after submission.
                        const: submitted
                      task_status_msg:
                        type: string
                        description: Status detail when returned.
                      task_info:
                        type: object
                        description: Additional task metadata. The object can be empty.
                        additionalProperties: true
                      created_at:
                        type: integer
                        format: int64
                        description: Task creation timestamp in milliseconds.
                      updated_at:
                        type: integer
                        format: int64
                        description: Last task update timestamp in milliseconds.
                example:
                  code: 0
                  message: SUCCEED
                  data:
                    task_id: <task_id>
                    task_status: submitted
                    task_info: {}
                    created_at: 1785398400000
                    updated_at: 1785398400000
      x-codeSamples:
        - lang: Shell
          label: Image URL
          source: |
            curl https://api.cometapi.com/kling/v1/videos/motion-control \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -H "Content-Type: application/json" \
              -d '{
                "model_name": "kling-v3",
                "image_url": "https://your-image-host.example.com/character.png",
                "video_url": "https://your-video-host.example.com/reference-motion.mp4",
                "prompt": "Studio scene.",
                "keep_original_sound": "no",
                "character_orientation": "video",
                "mode": "std"
              }'
        - lang: Python
          label: Image URL
          source: |
            import os
            import requests

            response = requests.post(
                "https://api.cometapi.com/kling/v1/videos/motion-control",
                headers={"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]},
                json={
                    "model_name": "kling-v3",
                    "image_url": "https://your-image-host.example.com/character.png",
                    "video_url": "https://your-video-host.example.com/reference-motion.mp4",
                    "prompt": "Studio scene.",
                    "keep_original_sound": "no",
                    "character_orientation": "video",
                    "mode": "std",
                },
            )

            result = response.json()
            print(result.get("code"), result.get("data", {}).get("task_id"))
        - lang: JavaScript
          label: Image URL
          source: |
            const response = await fetch(
              "https://api.cometapi.com/kling/v1/videos/motion-control",
              {
                method: "POST",
                headers: {
                  Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
                  "Content-Type": "application/json",
                },
                body: JSON.stringify({
                  model_name: "kling-v3",
                  image_url: "https://your-image-host.example.com/character.png",
                  video_url: "https://your-video-host.example.com/reference-motion.mp4",
                  prompt: "Studio scene.",
                  keep_original_sound: "no",
                  character_orientation: "video",
                  mode: "std",
                }),
              },
            );

            const result = await response.json();
            console.log(result.code, result.data?.task_id);
        - lang: Shell
          label: Image base64
          source: |
            curl https://api.cometapi.com/kling/v1/videos/motion-control \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -H "Content-Type: application/json" \
              --data-binary @- <<'JSON'
            {
              "model_name": "kling-v3",
              "image_url": "<base64-encoded-png>",
              "video_url": "https://your-video-host.example.com/reference-motion.mp4",
              "prompt": "Studio scene.",
              "keep_original_sound": "no",
              "character_orientation": "video",
              "mode": "pro"
            }
            JSON
        - lang: Python
          label: Image base64
          source: |
            import base64
            import os
            import requests

            with open("character.png", "rb") as image_file:
                image_base64 = base64.b64encode(image_file.read()).decode("ascii")

            response = requests.post(
                "https://api.cometapi.com/kling/v1/videos/motion-control",
                headers={"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]},
                json={
                    "model_name": "kling-v3",
                    "image_url": image_base64,
                    "video_url": "https://your-video-host.example.com/reference-motion.mp4",
                    "prompt": "Studio scene.",
                    "keep_original_sound": "no",
                    "character_orientation": "video",
                    "mode": "pro",
                },
            )

            result = response.json()
            print(result.get("code"), result.get("data", {}).get("task_id"))
        - lang: JavaScript
          label: Image base64
          source: >
            import { readFile } from "node:fs/promises";


            const imageBase64 = (await
            readFile("character.png")).toString("base64");

            const response = await fetch(
              "https://api.cometapi.com/kling/v1/videos/motion-control",
              {
                method: "POST",
                headers: {
                  Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
                  "Content-Type": "application/json",
                },
                body: JSON.stringify({
                  model_name: "kling-v3",
                  image_url: imageBase64,
                  video_url: "https://your-video-host.example.com/reference-motion.mp4",
                  prompt: "Studio scene.",
                  keep_original_sound: "no",
                  character_orientation: "video",
                  mode: "pro",
                }),
              },
            );


            const result = response.json();

            console.log(result.code, result.data?.task_id);
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Bearer authentication. Use your CometAPI API key.

````

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