Kling AI 文字轉影片 是做什麼的?
概述
使用 /v1/videos/text2video 端點,根據文字提示生成影片。支援模型 v1 至 v2.6、標準/專業模式、攝影機控制、反向提示詞,以及原生音訊(v2.6 以上)。
端點: POST https://api.klingai.com/v1/videos/text2video
請求參數
| 參數 | 類型 | 必要 | 說明 |
|---|---|---|---|
model_name | 字串 | 是 | 模型版本(請參閱模型目錄) |
prompt | 字串 | 是 | 影片描述,最多 2500 個字元 |
negative_prompt | 字串 | 否 | 要排除的內容 |
duration | 字串 | 是 | "5" 或 "10" 秒 |
aspect_ratio | 字串 | 否 | "16:9"(預設)、"9:16"、"1:1" 等 |
mode | 字串 | 否 | "standard"(預設)或 "professional" |
cfg_scale | 浮點數 | 否 | 提示詞遵循度(0.0-1.0,預設 0.5) |
camera_control | 物件 | 否 | 攝影機移動配置 |
callback_url | 字串 | 否 | 完成通知的 Webhook URL |
完整範例 — Python
import jwt, time, os, requests
BASE = "https://api.klingai.com/v1"
def get_headers():
ak, sk = os.environ["KLING_ACCESS_KEY"], os.environ["KLING_SECRET_KEY"]
token = jwt.encode(
{"iss": ak, "exp": int(time.time()) + 1800, "nbf": int(time.time()) - 5},
sk, algorithm="HS256", headers={"alg": "HS256", "typ": "JWT"}
)
return {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
# 建立文字轉影片任務
response = requests.post(f"{BASE}/videos/text2video", headers=get_headers(), json={
"model_name": "kling-v2-6",
"prompt": "Aerial drone shot of a coral reef at golden hour, "
"tropical fish swimming through crystal clear water, "
"sun rays penetrating the surface, cinematic 4K",
"negative_prompt": "blurry, low quality, distorted, watermark",
"duration": "5",
"aspect_ratio": "16:9",
"mode": "professional",
"cfg_scale": 0.5,
})
task = response.json()
task_id = task["data"]["task_id"]
# 輪詢完成狀態
while True:
time.sleep(15)
result = requests.get(
f"{BASE}/videos/text2video/{task_id}", headers=get_headers()
).json()
status = result["data"]["task_status"]
if status == "succeed":
video = result["data"]["task_result"]["videos"][0]
print(f"影片 URL: {video['url']}")
print(f"時長: {video['duration']}s")
break
elif status == "failed":
raise RuntimeError(result["data"]["task_status_msg"])
# 其他狀態:submitted/processing — 持續輪詢
搭配攝影機控制
# 攝影機移動類型:pan, tilt, zoom, roll
response = requests.post(f"{BASE}/videos/text2video", headers=get_headers(), json={
"model_name": "kling-v2-6",
"prompt": "A medieval castle on a cliff at sunrise, fog in the valley",
"duration": "5",
"mode": "standard",
"camera_control": {
"type": "simple",
"config": {
"horizontal": 5, # 向右平移(負值向左),範圍 -10 至 10
"vertical": 0, # 俯仰(負值向下,正值向上)
"zoom": 3, # 縮放(正值放大,負值縮小)
"roll": 0, # 旋轉
"pan": 0, # 左右橫移
"tilt": -2, # 上下橫移
}
},
})
規則: 對於 type: "simple",config 中只能有一個非零欄位。
搭配原生音訊(僅 v2.6)
response = requests.post(f"{BASE}/videos/text2video", headers=get_headers(), json={
"model_name": "kling-v2-6",
"prompt": "A jazz band performing in a dimly lit club, saxophone solo, "
"audience clapping, warm amber lighting",
"duration": "10",
"mode": "professional",
"motion_has_audio": True, # 生成同步音訊
})
提示詞工程技巧
| 技巧 | 範例 |
|---|---|
| 場景 + 動作 + 風格 | "A samurai walking through cherry blossoms, cinematic slow motion" |
| 光線提示 | "golden hour", "neon-lit", "overcast diffused light" |
| 鏡頭語言 | "close-up", "wide establishing shot", "tracking shot" |
| 反向提示詞 | "blurry, watermark, text overlay, distorted faces" |
| 材質/質感 | "brushed steel", "hand-painted watercolor", "photorealistic" |
費用參考
| 時長 | 標準模式 | 專業模式 |
|---|---|---|
| 5 秒 | 10 點數 | 35 點數 |
| 10 秒 | 20 點數 | 70 點數 |
錯誤處理
| 錯誤 | 原因 | 解決方法 |
|---|---|---|
400 無效提示詞 | 為空或超過 2500 字元 | 檢查提示詞長度 |
400 無效模型 | 不支援的 model_name | 使用目錄中的有效模型 ID |
402 點數不足 | 點數不足 | 充值 |
task_status: failed | 違反內容政策或複雜度過高 | 簡化提示詞,移除受限內容 |
先決條件
- 審核通過的簡報、權利已清除或合成的參考素材、授權的工作區與預算、內容政策審查,以及指派的擁有者負責發布與刪除。
操作說明
- 根據審核通過的簡報建立沙盒草稿;請勿在提示詞或上傳中包含私人個體、受保護素材或未經證實的聲明。
- 在提交前確認請求的時長、風格、目的地、點數預算、內容政策狀態,以及僅限草稿設定。
- 審查一個帶浮水印的預覽渲染,確認政策、權利與品質;若發現政策或歸屬問題,請停止操作並刪除草稿,不要發布。
- 僅在獲得擁有者批准後才發布,並保留一份去敏的製作收據;在核准的保留期限到後移除暫存資源。
輸出
產生一份渲染收據,包含簡報 ID、已審核的來源分類、模型/模式、時長、點數估算、政策與權利審查結果、草稿目的地、核准者、保留/刪除參考資訊,以及任務 ID。排除提示詞文字、身分資訊與憑證。
範例
brief=synthetic-product-demo; source=rights-cleared; mode=standard; duration=5s; policy=pass; destination=draft-only; approval=pending; cleanup=24h 為一個安全的預覽請求。