> ## 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 một tác vụ đồng bộ môi nâng cao của Kling

> Tạo các tác vụ video đồng bộ môi nâng cao qua CometAPI Kling POST /kling/v1/videos/advanced-lip-sync, gửi âm thanh và cài đặt counterpart để tạo kết quả đồng bộ.

Sử dụng endpoint này để tạo một tác vụ đồng bộ môi nâng cao sau khi bạn đã xác định khuôn mặt trong video nguồn.

## Trước khi gọi

* Bắt đầu với [Lip-Sync](./lip-sync) để lấy `session_id` và các giá trị `face_id` khả dụng
* Tạo một hoặc nhiều mục `face_choose` để ánh xạ từng khuôn mặt với một đoạn âm thanh
* Sử dụng `audio_id` cho âm thanh được tạo từ một tác vụ trước đó, hoặc `sound_file` cho một tệp âm thanh được lưu trữ
* Gửi các trường thời gian theo mili giây. Ví dụ: `sound_start_time: "0"` và `sound_end_time: "2000"` sẽ chọn 2 giây đầu tiên.
* Đảm bảo đoạn âm thanh được cắt có thời lượng ít nhất 2000 ms và không vượt quá thời lượng âm thanh nguồn
* Chỉ đặt `callback_url` hoặc `external_task_id` khi bạn cần gửi webhook hoặc mã theo dõi riêng của mình

## Luồng tác vụ

<Steps>
  <Step title="Trước tiên hãy xác định khuôn mặt">
    Chạy [Lip-Sync](./lip-sync) trên video nguồn và lưu lại `session_id` được trả về cùng với các giá trị `face_id` mục tiêu.
  </Step>

  <Step title="Tạo tác vụ đồng bộ môi nâng cao">
    Gửi `session_id` và mảng `face_choose` thông qua endpoint này.
  </Step>

  <Step title="Theo dõi tác vụ đã tạo">
    Lưu task id được trả về để kiểm tra trạng thái và truy xuất kết quả cuối cùng.
  </Step>
</Steps>

<Tip>
  Để xem tài liệu tham chiếu tham số đầy đủ, hãy xem [tài liệu Kling chính thức](https://kling.ai/document-api/apiReference/model/lipSync).
</Tip>


## OpenAPI

````yaml api/openapi/video/kling/post-counterpart-creating-tasks.openapi.json POST /kling/v1/videos/advanced-lip-sync
openapi: 3.1.0
info:
  title: Counterpart Creating Tasks API
  version: 1.0.0
  description: >-
    Create an advanced Kling lip-sync task after face identification has
    produced a session id and face ids.
servers:
  - url: https://api.cometapi.com
security:
  - bearerAuth: []
paths:
  /kling/v1/videos/advanced-lip-sync:
    post:
      summary: Create an advanced Kling lip-sync task
      description: >-
        Submit the `session_id` returned by face identification plus one or more
        `face_choose` mappings that connect target faces to audio segments.
      operationId: counterpart_creating_tasks
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - session_id
                - face_choose
              properties:
                session_id:
                  type: string
                  description: Session id returned by identify-face.
                face_choose:
                  type: array
                  description: One or more face/audio mappings to synthesize.
                  minItems: 1
                  items:
                    type: object
                    required:
                      - face_id
                    properties:
                      face_id:
                        type: string
                        description: Face id returned by identify-face.
                      audio_id:
                        type: string
                        description: Audio id from a prior audio task.
                      sound_file:
                        type: string
                        description: Public audio URL when you provide your own audio.
                      sound_start_time:
                        type: string
                        description: Start time within the audio source, in milliseconds.
                      sound_end_time:
                        type: string
                        description: >-
                          End time within the audio source, in milliseconds. The
                          clipped segment must be at least 2000 ms and must not
                          exceed the source audio duration.
                      sound_insert_time:
                        type: string
                        description: Insertion time in the target video, in milliseconds.
                      sound_volume:
                        type: number
                        description: Volume for the inserted audio.
                      original_sound_volume:
                        type: number
                        description: Volume for the original video audio.
                callback_url:
                  type: string
                  description: Webhook URL to receive task status updates.
                external_task_id:
                  type: string
                  description: >-
                    Custom task id for your own tracking. Must be unique per
                    account.
              default:
                session_id: <session_id>
                face_choose:
                  - face_id: <face_id>
                    sound_file: https://your-audio-host/speech.wav
                    sound_start_time: '0'
                    sound_end_time: '2000'
                    sound_insert_time: '0'
                    sound_volume: 1
                    original_sound_volume: 0
            examples:
              Default:
                summary: Advanced lip-sync request
                value:
                  session_id: <session_id>
                  face_choose:
                    - face_id: <face_id>
                      sound_file: https://your-audio-host/speech.wav
                      sound_start_time: '0'
                      sound_end_time: '2000'
                      sound_insert_time: '0'
                      sound_volume: 1
                      original_sound_volume: 0
      responses:
        '200':
          description: Task accepted.
          content:
            application/json:
              schema:
                type: object
                required:
                  - code
                  - message
                  - data
                properties:
                  code:
                    type: integer
                  message:
                    type: string
                  data:
                    type: object
                    properties:
                      task_id:
                        type: string
                      task_status:
                        type: string
                      created_at:
                        type: integer
                      updated_at:
                        type: integer
                    additionalProperties: true
      x-codeSamples:
        - lang: Shell
          label: Default
          source: |
            curl https://api.cometapi.com/kling/v1/videos/advanced-lip-sync \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -H "Content-Type: application/json" \
              -d '{
                  "session_id": "<session_id>",
                  "face_choose": [
                    {
                      "face_id": "<face_id>",
                      "sound_file": "https://your-audio-host/speech.wav",
                      "sound_start_time": "0",
                      "sound_end_time": "2000",
                      "sound_insert_time": "0",
                      "sound_volume": 1,
                      "original_sound_volume": 0
                    }
                  ]
                }'
        - lang: Python
          label: Default
          source: |
            import os
            import requests

            response = requests.post(
                "https://api.cometapi.com/kling/v1/videos/advanced-lip-sync",
                headers={"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]},
                json={
                  "session_id": "<session_id>",
                  "face_choose": [
                    {
                      "face_id": "<face_id>",
                      "sound_file": "https://your-audio-host/speech.wav",
                      "sound_start_time": "0",
                      "sound_end_time": "2000",
                      "sound_insert_time": "0",
                      "sound_volume": 1,
                      "original_sound_volume": 0
                    }
                  ]
                },
            )

            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/advanced-lip-sync",
            {
              method: "POST",
              headers: {
                Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
                "Content-Type": "application/json",
              },
              body: JSON.stringify({
                "session_id": "<session_id>",
                "face_choose": [
                  {
                    "face_id": "<face_id>",
                    "sound_file": "https://your-audio-host/speech.wav",
                    "sound_start_time": "0",
                    "sound_end_time": "2000",
                    "sound_insert_time": "0",
                    "sound_volume": 1,
                    "original_sound_volume": 0
                  }
                ]
              }),
            });


            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.

````