萬相首尾幀生視頻模型基於首幀映像、尾幀映像和文本提示詞,產生一段平滑過渡的視頻。
相關文檔:使用指南
適用範圍
為確保調用成功,請務必保證模型、Endpoint URL 和 API Key 均屬於同一地區。跨地區調用將會失敗。
選擇模型:確認模型所屬的地區。
選擇 URL:選擇對應的地區 Endpoint URL,支援HTTP URL或 DashScope SDK URL。
配置 API Key:選擇地區並擷取API Key與API Host,再配置API Key到環境變數(準備下線,併入配置 API Key)。
安裝 SDK:如需通過SDK進行調用,請安裝DashScope SDK。
本文的範例程式碼適用於新加坡地區。
HTTP調用
由於圖生視頻任務耗時較長(通常為1-5分鐘),API採用非同步呼叫。整個流程包含 “建立任務 -> 輪詢擷取” 兩個核心步驟,具體如下:
步驟1:建立任務擷取任務ID
新加坡
POST https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/image2video/video-synthesis
北京
POST https://dashscope.aliyuncs.com/api/v1/services/aigc/image2video/video-synthesis
建立成功後,使用介面返回的
task_id查詢結果,task_id 有效期間為 24 小時。請勿重複建立任務,輪詢擷取即可。新手指引請參見Postman。
請求參數 | 首尾幀生視頻根據首幀、尾幀和prompt產生視頻。 使用反向提示詞通過 negative_prompt 指定產生的視頻避免出現“人物”元素。 |
要求標頭(Headers) | |
Content-Type 請求內容類型。此參數必須設定為 | |
Authorization 請求身份認證。介面使用阿里雲百鍊API-Key進行身份認證。樣本值:Bearer sk-xxxx。 | |
X-DashScope-Async 非同步處理配置參數。HTTP請求只支援非同步,必須設定為 重要 缺少此要求標頭將報錯:“current user api does not support synchronous calls”。 | |
請求體(Request Body) | |
model 模型名稱。樣本值:wan2.2-kf2v-flash。 詳情參見模型列表與價格。 | |
input 輸入的基本資料,如提示詞等。 | |
parameters 視頻處理參數。 |
響應參數 | 成功響應請儲存 task_id,用於查詢任務狀態與結果。 異常響應建立任務失敗,請參見錯誤資訊進行解決。 |
output 任務輸出資訊。 | |
request_id 請求唯一標識。可用於請求明細溯源和問題排查。 | |
code 請求失敗的錯誤碼。請求成功時不會返回此參數,詳情請參見錯誤資訊。 | |
message 請求失敗的詳細資料。請求成功時不會返回此參數,詳情請參見錯誤資訊。 |
步驟2:根據任務ID查詢結果
新加坡
GET https://dashscope-intl.aliyuncs.com/api/v1/tasks/{task_id}
北京
GET https://dashscope.aliyuncs.com/api/v1/tasks/{task_id}
輪詢建議:視頻產生過程約需數分鐘,建議採用輪詢機制,並設定合理的查詢間隔(如 15 秒)來擷取結果。
任務狀態流轉:PENDING(排隊中)→ RUNNING(處理中)→ SUCCEEDED(成功)/ FAILED(失敗)。
結果連結:任務成功後返回視頻連結,有效期間為 24 小時。建議在擷取連結後立即下載並轉存至永久儲存(如阿里雲 OSS)。
task_id 有效期間:24小時,逾時後將無法查詢結果,介面將返回任務狀態為
UNKNOWN。
請求參數 | 查詢任務結果請將 各地區的API Key不同。擷取API Key與API Host。 若使用北京地區的模型,需將base_url替換為https://dashscope.aliyuncs.com/api/v1/tasks/86ecf553-d340-4e21-xxxxxxxxx |
要求標頭(Headers) | |
Authorization 請求身份認證。介面使用阿里雲百鍊API-Key進行身份認證。樣本值:Bearer sk-xxxx。 | |
URL路徑參數(Path parameters) | |
task_id 任務ID。 |
響應參數 | 任務執行成功視頻URL僅保留24小時,逾時後會被自動清除,請及時儲存產生的視頻。 任務執行失敗若任務執行失敗,task_status將置為 FAILED,並提供錯誤碼和資訊。請參見錯誤資訊進行解決。 任務查詢到期task_id查詢有效期間為 24 小時,逾時後將無法查詢,返回以下報錯資訊。 |
output 任務輸出資訊。 | |
usage 輸出資訊統計。只對成功的結果計數。 | |
request_id 請求唯一標識。可用於請求明細溯源和問題排查。 |
DashScope SDK調用
SDK 的參數命名與HTTP介面基本一致,參數結構根據語言特性進行封裝。
由於圖生視頻任務耗時較長(通常為1-5分鐘),SDK 在底層封裝了 HTTP 非同步呼叫流程,支援同步、非同步兩種調用方式。
具體耗時受限於排隊任務數和服務執行情況,請在擷取結果時耐心等待。
Python SDK調用
請確保 DashScope Python SDK 版本不低於 1.23.8,再運行以下代碼。
若版本過低,可能會觸發 “url error, please check url!” 等錯誤。請參考安裝SDK進行更新。
根據模型所在地區設定 base_http_api_url:
新加坡
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
北京
dashscope.base_http_api_url = 'https://dashscope.aliyuncs.com/api/v1'
範例程式碼
同步調用
本樣本展示同步調用方式,包括兩種映像輸入方式:公網URL、本地檔案路徑。
請求樣本
import os
from http import HTTPStatus
# dashscope sdk >= 1.23.4
from dashscope import VideoSynthesis
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
# 從環境變數中擷取 DashScope API Key(即阿里雲百鍊平台 API key)
api_key = os.getenv("DASHSCOPE_API_KEY")
# ========== 映像輸入方式(二選一)==========
# 【方式一】使用公網圖片 URL
first_frame_url = "https://wanx.alicdn.com/material/20250318/first_frame.png"
last_frame_url = "https://wanx.alicdn.com/material/20250318/last_frame.png"
# 【方式二】使用本地檔案路徑(file://+檔案路徑)
# 使用絕對路徑
# first_frame_url = "file://" + "/path/to/your/first_frame.png" # Linux/macOS
# last_frame_url = "file://" + "C:/path/to/your/last_frame.png" # Windows
# 或使用相對路徑
# first_frame_url = "file://" + "./first_frame.png" # 以實際路徑為準
# last_frame_url = "file://" + "./last_frame.png" # 以實際路徑為準
def sample_sync_call_kf2v():
print('please wait...')
rsp = VideoSynthesis.call(api_key=api_key,
model="wan2.2-kf2v-flash",
prompt="寫實風格,一隻黑色小貓好奇地看向天空,鏡頭從平視逐漸上升,最後俯拍它的好奇的眼神。",
first_frame_url=first_frame_url,
last_frame_url=last_frame_url,
resolution="720P",
prompt_extend=True)
print(rsp)
if rsp.status_code == HTTPStatus.OK:
print(rsp.output.video_url)
else:
print('Failed, status_code: %s, code: %s, message: %s' %
(rsp.status_code, rsp.code, rsp.message))
if __name__ == '__main__':
sample_sync_call_kf2v()響應樣本
video_url 有效期間24小時,請及時下載視頻。
{
"status_code": 200,
"request_id": "a37fafc3-907c-96f3-95a6-5b2a8268a3fd",
"code": null,
"message": "",
"output": {
"task_id": "4dba0092-da13-42b2-afb1-0f7b8a0f4643",
"task_status": "SUCCEEDED",
"video_url": "https://dashscope-result-wlcb-acdr-1.oss-cn-wulanchabu-acdr-1.aliyuncs.com/xxx.mp4?xxxxx",
"submit_time": "2025-05-23 15:50:12.404",
"scheduled_time": "2025-05-23 15:50:12.443",
"end_time": "2025-05-23 15:54:56.502",
"orig_prompt": "寫實風格,一隻黑色小貓好奇地看向天空,鏡頭從平視逐漸上升,最後俯拍它的好奇的眼神。",
"actual_prompt": "寫實風格,一隻黑色小貓好奇地看向天空,鏡頭從平視逐漸上升,最後俯拍它的好奇的眼神。小貓的黃色眼睛明亮有神,耳朵豎立,鬍鬚清晰可見。背景是簡潔的淺色牆面,突顯小貓的黑色毛髮和專註的表情。近景特寫,強調小貓的眼神變化和姿態。"
},
"usage": {
"video_count": 1,
"video_duration": 5,
"video_ratio": "standard"
}
}非同步呼叫
本樣本展示非同步呼叫方式。該方式會立即返回任務ID,需要自行輪詢或等待任務完成。
請求樣本
import os
from http import HTTPStatus
# dashscope sdk >= 1.23.4
from dashscope import VideoSynthesis
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
# 從環境變數中擷取 DashScope API Key(即阿里雲百鍊平台 API key)
api_key = os.getenv("DASHSCOPE_API_KEY")
# ========== 映像輸入方式(二選一)==========
# 【方式一】使用公網圖片 URL
first_frame_url = "https://wanx.alicdn.com/material/20250318/first_frame.png"
last_frame_url = "https://wanx.alicdn.com/material/20250318/last_frame.png"
# 【方式二】使用本地檔案路徑(file://+檔案路徑)
# 使用絕對路徑
# first_frame_url = "file://" + "/path/to/your/first_frame.png" # Linux/macOS
# last_frame_url = "file://" + "C:/path/to/your/last_frame.png" # Windows
# 或使用相對路徑
# first_frame_url = "file://" + "./first_frame.png" # 以實際路徑為準
# last_frame_url = "file://" + "./last_frame.png" # 以實際路徑為準
def sample_async_call_kf2v():
print('please wait...')
rsp = VideoSynthesis.async_call(api_key=api_key,
model="wan2.2-kf2v-flash",
prompt="寫實風格,一隻黑色小貓好奇地看向天空,鏡頭從平視逐漸上升,最後俯拍它的好奇的眼神。",
first_frame_url=first_frame_url,
last_frame_url=last_frame_url,
resolution="720P",
prompt_extend=True)
print(rsp)
if rsp.status_code == HTTPStatus.OK:
print("task_id: %s" % rsp.output.task_id)
else:
print('Failed, status_code: %s, code: %s, message: %s' %
(rsp.status_code, rsp.code, rsp.message))
# get the task information include the task status.
status = VideoSynthesis.fetch(task=rsp, api_key=api_key)
if status.status_code == HTTPStatus.OK:
print(status.output.task_status) # check the task status
else:
print('Failed, status_code: %s, code: %s, message: %s' %
(status.status_code, status.code, status.message))
# wait the task complete, will call fetch interval, and check it's in finished status.
rsp = VideoSynthesis.wait(task=rsp, api_key=api_key)
print(rsp)
if rsp.status_code == HTTPStatus.OK:
print(rsp.output.video_url)
else:
print('Failed, status_code: %s, code: %s, message: %s' %
(rsp.status_code, rsp.code, rsp.message))
if __name__ == '__main__':
sample_async_call_kf2v()響應樣本
1、建立任務的響應樣本
{
"status_code": 200,
"request_id": "c86ff7ba-8377-917a-90ed-xxxxxx",
"code": "",
"message": "",
"output": {
"task_id": "721164c6-8619-4a35-a6d9-xxxxxx",
"task_status": "PENDING",
"video_url": ""
},
"usage": null
}2、查詢任務結果的響應樣本
video_url 有效期間24小時,請及時下載視頻。
{
"status_code": 200,
"request_id": "efa545b3-f95c-9e3a-a3b6-xxxxxx",
"code": null,
"message": "",
"output": {
"task_id": "721164c6-8619-4a35-a6d9-xxxxxx",
"task_status": "SUCCEEDED",
"video_url": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/xxx.mp4?xxxxx",
"submit_time": "2025-02-12 11:03:30.701",
"scheduled_time": "2025-02-12 11:06:05.378",
"end_time": "2025-02-12 11:12:18.853",
"orig_prompt": "寫實風格,一隻黑色小貓好奇地看向天空,鏡頭從平視逐漸上升,最後俯拍它的好奇的眼神。",
"actual_prompt": "寫實風格,一隻黑色小貓好奇地看向天空,鏡頭從平視逐漸上升,最後俯拍它的好奇的眼神。小貓毛髮烏黑光亮,眼睛大而明亮,瞳孔呈金黃色。它抬頭仰望,耳朵豎立,顯得格外專註。鏡頭上移後,小貓轉頭直視鏡頭,眼神中充滿好奇與警覺。背景簡潔,突出小貓的細節特徵。近景特寫,自然光線柔和。"
},
"usage": {
"video_count": 1,
"video_duration": 5,
"video_ratio": "standard"
}
}Java SDK調用
請確保 DashScope Java SDK 版本不低於 2.20.9,再運行以下代碼。
若版本過低,可能會觸發 “url error, please check url!” 等錯誤。請參考安裝SDK進行更新。
範例程式碼
同步調用
本樣本展示同步調用方式,包括兩種映像輸入方式:公網URL、本地檔案路徑。
請求樣本
// Copyright (c) Alibaba, Inc. and its affiliates.
// dashscope sdk >= 2.20.1
import com.alibaba.dashscope.aigc.videosynthesis.VideoSynthesis;
import com.alibaba.dashscope.aigc.videosynthesis.VideoSynthesisParam;
import com.alibaba.dashscope.aigc.videosynthesis.VideoSynthesisResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
import com.alibaba.dashscope.utils.JsonUtils;
import java.util.HashMap;
import java.util.Map;
public class Kf2vSyncIntl {
static {
Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
}
// 從環境變數中擷取 DashScope API Key(即阿里雲百鍊平台 API Key)
static String apiKey = System.getenv("DASHSCOPE_API_KEY");
/**
* 映像輸入方式(二選一):
*
* 【方式一】公網URL
*/
static String firstFrameUrl = "https://wanx.alicdn.com/material/20250318/first_frame.png";
static String lastFrameUrl = "https://wanx.alicdn.com/material/20250318/last_frame.png";
/**
* 【方式二】本地檔案路徑(file://+絕對路徑 or file:///+絕對路徑)
*/
// static String firstFrameUrl = "file://" + "/your/path/to/first_frame.png"; // Linux/macOS
// static String lastFrameUrl = "file:///" + "C:/path/to/your/img.png"; // Windows
public static void syncCall() {
Map<String, Object> parameters = new HashMap<>();
parameters.put("prompt_extend", true);
parameters.put("resolution", "720P");
VideoSynthesis videoSynthesis = new VideoSynthesis();
VideoSynthesisParam param =
VideoSynthesisParam.builder()
.apiKey(apiKey)
.model("wan2.2-kf2v-flash")
.prompt("寫實風格,一隻黑色小貓好奇地看向天空,鏡頭從平視逐漸上升,最後俯拍它的好奇的眼神。")
.firstFrameUrl(firstFrameUrl)
.lastFrameUrl(lastFrameUrl)
.parameters(parameters)
.build();
VideoSynthesisResult result = null;
try {
System.out.println("---sync call, please wait a moment----");
result = videoSynthesis.call(param);
} catch (ApiException | NoApiKeyException e){
throw new RuntimeException(e.getMessage());
} catch (InputRequiredException e) {
throw new RuntimeException(e);
}
System.out.println(JsonUtils.toJson(result));
}
public static void main(String[] args) {
syncCall();
}
}響應樣本
video_url 有效期間24小時,請及時下載視頻。
{
"request_id": "e6bb4517-c073-9c10-b748-dedb8c11bb41",
"output": {
"task_id": "984784fe-83c1-4fc4-88c7-52c2c1fa92a2",
"task_status": "SUCCEEDED",
"video_url": "https://dashscope-result-wlcb-acdr-1.oss-cn-wulanchabu-acdr-1.aliyuncs.com/xxx.mp4?xxxxx"
},
"usage": {
"video_count": 1,
"video_duration": 5,
"video_ratio": "standard"
}
}非同步呼叫
本樣本展示非同步呼叫方式。該方式會立即返回任務ID,需要自行輪詢或等待任務完成。
請求樣本
// Copyright (c) Alibaba, Inc. and its affiliates.
// dashscope sdk >= 2.20.1
import com.alibaba.dashscope.aigc.videosynthesis.VideoSynthesis;
import com.alibaba.dashscope.aigc.videosynthesis.VideoSynthesisParam;
import com.alibaba.dashscope.aigc.videosynthesis.VideoSynthesisResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
import com.alibaba.dashscope.utils.JsonUtils;
import java.util.HashMap;
import java.util.Map;
public class Kf2vAsync {
static {
Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
}
// 從環境變數中擷取 DashScope API Key(即阿里雲百鍊平台 API Key)
static String apiKey = System.getenv("DASHSCOPE_API_KEY");
/**
* 映像輸入方式(二選一)
*
* 【方式一】公網URL
*/
static String firstFrameUrl = "https://wanx.alicdn.com/material/20250318/first_frame.png";
static String lastFrameUrl = "https://wanx.alicdn.com/material/20250318/last_frame.png";
/**
* 【方式二】本地檔案路徑(file://+絕對路徑 or file:///+絕對路徑)
*/
// static String firstFrameUrl = "file://" + "/your/path/to/first_frame.png"; // Linux/macOS
// static String lastFrameUrl = "file:///" + "C:/path/to/your/img.png"; // Windows
public static void asyncCall(){
// 設定parameters參數
Map<String, Object> parameters = new HashMap<>();
parameters.put("prompt_extend", true);
parameters.put("resolution", "720P");
VideoSynthesis videoSynthesis = new VideoSynthesis();
VideoSynthesisParam param =
VideoSynthesisParam.builder()
.apiKey(apiKey)
.model("wan2.2-kf2v-flash")
.prompt("寫實風格,一隻黑色小貓好奇地看向天空,鏡頭從平視逐漸上升,最後俯拍它的好奇的眼神。")
.firstFrameUrl(firstFrameUrl)
.lastFrameUrl(lastFrameUrl)
.parameters(parameters)
.build();
VideoSynthesisResult result = null;
try {
System.out.println("---async call, please wait a moment----");
result = videoSynthesis.asyncCall(param);
} catch (ApiException | NoApiKeyException e){
throw new RuntimeException(e.getMessage());
} catch (InputRequiredException e) {
throw new RuntimeException(e);
}
System.out.println(JsonUtils.toJson(result));
String taskId = result.getOutput().getTaskId();
System.out.println("taskId=" + taskId);
try {
result = videoSynthesis.wait(taskId, apiKey);
} catch (ApiException | NoApiKeyException e){
throw new RuntimeException(e.getMessage());
}
System.out.println(JsonUtils.toJson(result));
System.out.println(JsonUtils.toJson(result.getOutput()));
}
public static void main(String[] args){
asyncCall();
}
}響應樣本
1、建立任務的響應樣本
{
"request_id": "5dbf9dc5-4f4c-9605-85ea-xxxxxxxx",
"output": {
"task_id": "7277e20e-aa01-4709-xxxxxxxx",
"task_status": "PENDING"
}
}2、查詢任務結果的響應樣本
video_url 有效期間24小時,請及時下載視頻。
{
"request_id": "1625235c-c13e-93ec-aff7-xxxxxxxx",
"output": {
"task_id": "464a5e46-79a6-46fd-9823-xxxxxxxx",
"task_status": "SUCCEEDED",
"video_url": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/xxx.mp4?xxxxxx"
},
"usage": {
"video_count": 1,
"video_duration": 5,
"video_ratio": "standard"
}
}使用限制
錯誤碼
如果模型調用失敗並返回報錯資訊,請參見錯誤資訊進行解決。
常見問題
Q:如何產生特定寬高比(如3:4)的視頻?
A: 輸出視頻的寬高比由輸入首幀映像(first_frame_url)決定,但無法保證精確比例(如嚴格3:4),會存在一定偏差。
為什麼會有偏差:模型會以您輸入映像的比例為基準,結合設定的解析度檔位(resolution)總像素,自動計算出最接近的合法解析度。由於要求視頻的長和寬必須是 16 的倍數,模型會對最終解析度做微調,因此無法保證輸出比例嚴格等於 3:4,但會非常接近。
例如:輸入映像750×1000(寬高比 3:4 = 0.75),並設定 resolution = "720P"(目標總像素約 92 萬),實際輸出816×1104(寬高比 ≈ 0.739,總像素約90萬)。
實踐建議:
輸入控制:盡量使用與目標比例一致的圖片作為首幀輸入。
後期處理:如果您對比例有嚴格要求,建議在視頻產生後,使用編輯工具進行簡單的裁剪或黑邊填充。