> ## 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 影片

> 透過 POST /kling/v1/videos/video-extend 使用 task_id 與 video_id 延長 Kling 產生的影片；會作為父帳戶上的子任務執行。

使用此端點將現有的 Kling 影片延長為後續任務。

## 呼叫前準備

* 從已完成的 Kling 影片結果開始
* 傳入父任務結果中的 `video_id`
* 將父 `task_id` 與 `video_id` 一併傳入
* 使用與延長相容的來源影片；不支援的來源影片會回傳 HTTP 400，而不是建立延長任務
* 已完成的 5 秒 Kling v1 或 v1-6 文生影片或圖生影片父任務，是較安全的延長相容性起點
* 僅在需要引導續接內容時才加入 `prompt`、`negative_prompt` 或 `cfg_scale`
* 在與父任務相同的帳戶上下文中執行延長
* 在父任務資源過期前儘快觸發延長

## 任務流程

<Steps>
  <Step title="完成父影片任務">
    建立原始 Kling 影片，並等待其結果可用。
  </Step>

  <Step title="提交延長請求">
    傳送父 `task_id`、`video_id` 與選填 prompt，然後保存回傳的 task id。
  </Step>

  <Step title="輪詢延長任務">
    持續使用 [取得 Kling 任務](./individual-queries)，直到延長後的影片進入終態。
  </Step>
</Steps>

<Tip>
  如需完整參數參考，請參閱 [Kling 官方文件](https://kling.ai/document-api/apiReference/model/videoDuration)。
</Tip>


## OpenAPI

````yaml api/openapi/video/kling/post-video-extension.openapi.json POST /kling/v1/videos/video-extend
openapi: 3.1.0
info:
  title: Video Extension API
  version: 1.0.0
servers:
  - url: https://api.cometapi.com
security:
  - bearerAuth: []
paths:
  /kling/v1/videos/video-extend:
    post:
      summary: Video Extension
      description: Only videos from supported generation types can be extended.
      operationId: video_extension
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - video_id
                - task_id
              properties:
                task_id:
                  type: string
                  description: >-
                    Parent Kling task id for the extension-compatible video
                    being extended.
                video_id:
                  type: string
                  description: >-
                    Video id from a parent task result. The source video must be
                    extension-compatible; unsupported videos return HTTP 400.
                prompt:
                  type: string
                  description: >-
                    Text prompt describing the desired motion for the extended
                    segment. Maximum 500 characters.
                callback_url:
                  type: string
                  description: >-
                    Webhook URL to receive task status updates when the task
                    completes.
                negative_prompt:
                  type: string
                  description: Elements to avoid in the extension.
                cfg_scale:
                  type: number
                  description: 'Prompt adherence strength for the extension. Range: 0-1.'
              default:
                task_id: <parent_task_id>
                video_id: <video_id>
                prompt: Continue with the same calm morning atmosphere
                cfg_scale: 0.5
            examples:
              Default:
                summary: >-
                  Video extension request with an extension-compatible parent
                  task
                value:
                  task_id: <parent_task_id>
                  video_id: <video_id>
                  prompt: Continue with the same calm morning atmosphere
                  cfg_scale: 0.5
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                type: object
                required:
                  - code
                  - message
                  - request_id
                  - data
                properties:
                  code:
                    type: integer
                    description: Error code; specifically define the error code
                  message:
                    type: string
                    description: error message
                  request_id:
                    type: string
                    description: >-
                      Request ID, system-generated, for tracking requests,
                      troubleshooting issues
                  data:
                    type: object
                    required:
                      - task_id
                      - task_status
                      - task_status_msg
                      - task_info
                      - task_result
                      - created_at
                      - updated_at
                    properties:
                      task_id:
                        type: string
                        description: Task ID, system generated
                      task_status:
                        type: string
                        description: >-
                          Task status, enumerated values: submitted, processing,
                          succeed, failed.
                      task_status_msg:
                        type: string
                      task_info:
                        type: object
                        required:
                          - parent_video
                        properties:
                          parent_video:
                            type: object
                            required:
                              - id
                              - url
                              - duration
                            properties:
                              id:
                                type: string
                              url:
                                type: string
                              duration:
                                type: string
                      task_result:
                        type: object
                        required:
                          - videos
                        properties:
                          videos:
                            type: array
                            items:
                              type: object
                              properties:
                                id:
                                  type: string
                                url:
                                  type: string
                                duration:
                                  type: string
                      created_at:
                        type: integer
                        description: Task creation time, Unix timestamp, in ms.
                      updated_at:
                        type: integer
                        description: Task update time, Unix timestamp, in ms.
      x-codeSamples:
        - lang: Shell
          label: Default
          source: |
            curl https://api.cometapi.com/kling/v1/videos/video-extend \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -H "Content-Type: application/json" \
              -d '{
                  "task_id": "<parent_task_id>",
                  "video_id": "<video_id>",
                  "prompt": "Continue with the same calm morning atmosphere",
                  "cfg_scale": 0.5
                }'
        - lang: Python
          label: Default
          source: |
            import os
            import requests

            response = requests.post(
                "https://api.cometapi.com/kling/v1/videos/video-extend",
                headers={"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]},
                json={
                  "task_id": "<parent_task_id>",
                  "video_id": "<video_id>",
                  "prompt": "Continue with the same calm morning atmosphere",
                  "cfg_scale": 0.5
                },
            )

            result = response.json()
            print(result.get("code"), result.get("data", {}).get("task_id"))
        - lang: JavaScript
          label: Default
          source: >
            const response = await
            fetch("https://api.cometapi.com/kling/v1/videos/video-extend", {
              method: "POST",
              headers: {
                Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
                "Content-Type": "application/json",
              },
              body: JSON.stringify({
                "task_id": "<parent_task_id>",
                "video_id": "<video_id>",
                "prompt": "Continue with the same calm morning atmosphere",
                "cfg_scale": 0.5
              }),
            });


            const result = await response.json();

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

````