万相图生视频模型支持基于首帧图像、尾帧图像与文本提示词,生成平滑过渡的视频。
视频规格:固定视频时长(5秒),指定视频分辨率(480P/720P/1080P)。
其他能力:还支持智能改写prompt、添加水印。
快速开始
输入提示词 | 输入首帧图像 | 输入尾帧图像 | 输出视频(无声视频) |
一个可爱且表情有些忧伤的蓝色小怪物站在雨中。镜头缓慢拉近,定格在小怪物抬头仰望天空的瞬间。 |
|
|
在调用前,先获取API Key,再配置API Key到环境变量。通过SDK进行调用,请安装DashScope SDK。
Python SDK
请确保 DashScope Python SDK 版本不低于 1.25.8,再运行以下代码。
若版本过低,可能会触发 “url error, please check url!” 等错误。请参考安装SDK进行更新。
import os
from http import HTTPStatus
from dashscope import VideoSynthesis
import dashscope
# 以下为新加坡地域URL,各地域的URL不同,获取URL:https://www.alibabacloud.com/help/en/model-studio/image-to-video-by-first-and-last-frame-api-reference
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
# 各地域的API Key不同。获取API Key:https://www.alibabacloud.com/help/en/model-studio/get-api-key
api_key = os.getenv("DASHSCOPE_API_KEY", "YOUR_API_KEY")
print('please wait...')
rsp = VideoSynthesis.call(api_key=api_key,
model="wan2.2-kf2v-flash",
prompt="一个可爱且表情有些忧伤的蓝色小怪物站在雨中。镜头缓慢拉近,定格在小怪物抬头仰望天空的瞬间。",
first_frame_url="https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260126/ixdxvt/wan-kf2v-blue-1.png",
last_frame_url="https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260126/nhtdrc/wan-kf2v-blue-2.png",
duration=5, # 固定为5,不可修改
prompt_extend=True,
watermark=True)
print(rsp)
if rsp.status_code == HTTPStatus.OK:
print("video_url:", rsp.output.video_url)
else:
print('Failed, status_code: %s, code: %s, message: %s' % (rsp.status_code, rsp.code, rsp.message))Java SDK
请确保 DashScope Java SDK 版本不低于 2.22.6,再运行以下代码。
若版本过低,可能会触发 “url error, please check url!” 等错误。请参考安装SDK进行更新。
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.JsonUtils;
import com.alibaba.dashscope.utils.Constants;
public class Image2Video {
static {
// 以下为新加坡地域URL,各地域的URL不同,获取URL:https://www.alibabacloud.com/help/en/model-studio/image-to-video-by-first-and-last-frame-api-reference
Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
}
// 若没有配置环境变量,请用百炼API Key将下行替换为:apiKey="sk-xxx"
// 各地域的API Key不同。获取API Key:https://www.alibabacloud.com/help/en/model-studio/get-api-key
static String apiKey = System.getenv("DASHSCOPE_API_KEY");
public static void image2video() throws ApiException, NoApiKeyException, InputRequiredException {
VideoSynthesis vs = new VideoSynthesis();
VideoSynthesisParam param =
VideoSynthesisParam.builder()
.apiKey(apiKey)
.model("wan2.2-kf2v-flash")
.prompt("一个可爱且表情有些忧伤的蓝色小怪物站在雨中。镜头缓慢拉近,定格在小怪物抬头仰望天空的瞬间。")
.firstFrameUrl("https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260126/ixdxvt/wan-kf2v-blue-1.png")
.lastFrameUrl("https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260126/nhtdrc/wan-kf2v-blue-2.png")
.resolution("720P")
.promptExtend(true)
.watermark(true)
.build();
System.out.println("please wait...");
VideoSynthesisResult result = vs.call(param);
System.out.println(JsonUtils.toJson(result));
}
public static void main(String[] args) {
try {
image2video();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}curl
步骤1:创建任务获取任务ID
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/image2video/video-synthesis' \
-H 'X-DashScope-Async: enable' \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "wan2.2-kf2v-flash",
"input": {
"first_frame_url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260126/ixdxvt/wan-kf2v-blue-1.png",
"last_frame_url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260126/nhtdrc/wan-kf2v-blue-2.png",
"prompt": "一个可爱且表情有些忧伤的蓝色小怪物站在雨中。镜头缓慢拉近,定格在小怪物抬头仰望天空的瞬间。"
},
"parameters": {
"resolution": "720P",
"prompt_extend": true,
"watermark": true
}
}'步骤2:根据任务ID获取结果
将{task_id}完整替换为上一步接口返回的task_id的值。
curl -X GET https://dashscope-intl.aliyuncs.com/api/v1/tasks/{task_id} \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"输出示例
video_url 有效期24小时,请及时下载视频。{
"request_id": "c1209113-8437-424f-a386-xxxxxx",
"output": {
"task_id": "966cebcd-dedc-4962-af88-xxxxxx",
"task_status": "SUCCEEDED",
"video_url": "https://dashscope-result-sh.oss-accelerate.aliyuncs.com/xxx.mp4?Expires=xxx",
...
},
...
}适用范围
各地域支持的模型有所差异,且资源相互独立。调用时,确保模型、Endpoint URL 和 API Key 均属于同一地域,跨地域调用将会失败。
支持的模型:
国际
在国际部署模式下,接入点与数据存储均位于新加坡地域,模型推理计算资源在全球范围内动态调度(不含中国内地)。
模型名称 | 能力支持 | 输入模态 | 输出视频规格 |
wan2.2-kf2v-flash | 无声视频 较2.1模型稳定性与成功率全面提升 | 文本、图像 | 分辨率档位:480P、720P、1080P 视频时长:5s 固定规格:30fps、MP4(H.264编码) |
wan2.1-kf2v-plus | 无声视频 | 文本、图像 | 分辨率档位:720P 视频时长:5s 固定规格:30fps、MP4(H.264编码) |
中国内地
在中国内地部署模式下,接入点与数据存储均位于北京地域,模型推理计算资源仅限于中国内地。
模型名称 | 能力支持 | 输入模态 | 输出视频规格 |
wan2.2-kf2v-flash | 无声视频 较2.1模型稳定性与成功率全面提升 | 文本、图像 | 分辨率档位:480P、720P、1080P 视频时长:5秒 固定规格:30fps、MP4(H.264编码) |
wanx2.1-kf2v-plus | 无声视频 | 文本、图像 | 分辨率档位:720P 视频时长:5s 固定规格:30fps、MP4(H.264编码) |
本文的示例代码适用于新加坡地域。
核心能力
首尾帧生视频
支持模型:所有模型。
功能介绍:根据首帧和尾帧,生成平滑过渡的视频(无声视频)。
参数设置:
first_frame_url:必填,必须传入首帧图像。last_frame_url:必填,必须传入尾帧图像。prompt:可选,推荐填写,控制视频画面的变化。
输入提示词 | 输入首帧图像 | 输入尾帧图像 | 输出视频(无声视频) |
写实风格,一只小黑猫好奇地仰望天空,镜头从平视角度逐渐升高,最后以俯视角度捕捉到它好奇的眼神。 |
|
|
Python SDK
请确保 DashScope Python SDK 版本不低于 1.25.8,可参考安装SDK进行更新。import os
from http import HTTPStatus
from dashscope import VideoSynthesis
import dashscope
# 以下为新加坡地域URL,各地域的URL不同,获取URL:https://www.alibabacloud.com/help/en/model-studio/image-to-video-by-first-and-last-frame-api-reference
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
# 各地域的API Key不同。获取API Key:https://www.alibabacloud.com/help/en/model-studio/get-api-key
api_key = os.getenv("DASHSCOPE_API_KEY", "YOUR_API_KEY")
def sample_async_call_kf2v():
# 异步调用,返回一个task_id
rsp = VideoSynthesis.async_call(api_key=api_key,
model="wan2.2-kf2v-flash",
prompt="写实风格,一只黑色小猫好奇地看向天空,镜头从平视逐渐上升,最后俯拍它的好奇的眼神。",
first_frame_url="https://wanx.alicdn.com/material/20250318/first_frame.png",
last_frame_url="https://wanx.alicdn.com/material/20250318/last_frame.png",
duration=5, # 固定为5,不可修改
prompt_extend=True,
watermark=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))
# 等待异步任务结束
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()Java SDK
请确保 DashScope Java SDK 版本不低于 2.22.6,可参考安装SDK进行更新。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.JsonUtils;
import com.alibaba.dashscope.utils.Constants;
public class Image2Video {
static {
// 以下为新加坡地域URL,各地域的URL不同,获取URL:https://www.alibabacloud.com/help/en/model-studio/image-to-video-by-first-and-last-frame-api-reference
Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
}
// 若没有配置环境变量,请用百炼API Key将下行替换为:apiKey="sk-xxx"
// 各地域的API Key不同。获取API Key:https://www.alibabacloud.com/help/en/model-studio/get-api-key
static String apiKey = System.getenv("DASHSCOPE_API_KEY");
public static void image2video() throws ApiException, NoApiKeyException, InputRequiredException {
VideoSynthesis vs = new VideoSynthesis();
VideoSynthesisParam param =
VideoSynthesisParam.builder()
.apiKey(apiKey)
.model("wan2.2-kf2v-flash")
.prompt("写实风格,一只黑色小猫好奇地看向天空,镜头从平视逐渐上升,最后俯拍它的好奇的眼神。")
.firstFrameUrl("https://wanx.alicdn.com/material/20250318/first_frame.png")
.lastFrameUrl("https://wanx.alicdn.com/material/20250318/last_frame.png")
.resolution("720P")
.promptExtend(true)
.watermark(true)
.build();
// 异步调用
VideoSynthesisResult task = vs.asyncCall(param);
System.out.println(JsonUtils.toJson(task));
System.out.println("please wait...");
//获取结果
VideoSynthesisResult result = vs.wait(task, apiKey);
System.out.println(JsonUtils.toJson(result));
}
public static void main(String[] args) {
try {
image2video();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}curl
步骤1:创建任务获取任务ID
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/image2video/video-synthesis' \
-H 'X-DashScope-Async: enable' \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "wan2.2-kf2v-flash",
"input": {
"first_frame_url": "https://wanx.alicdn.com/material/20250318/first_frame.png",
"last_frame_url": "https://wanx.alicdn.com/material/20250318/last_frame.png",
"prompt": "写实风格,一只黑色小猫好奇地看向天空,镜头从平视逐渐上升,最后俯拍它的好奇的眼神。"
},
"parameters": {
"resolution": "720P",
"prompt_extend": true,
"watermark": true
}
}'步骤2:根据任务ID获取结果
将{task_id}完整替换为上一步接口返回的task_id的值。
curl -X GET https://dashscope-intl.aliyuncs.com/api/v1/tasks/{task_id} \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"如何输入图像
图像数量:1张首帧图像,1张尾帧图像。
输入方式:图像URL、本地文件路径。
示例代码:图像的多种输入方式
Python SDK
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()Java SDK
// 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();
}
}输出视频
视频数量:1个。
视频规格:各模型的输出规格有所差异,具体请参见视频规格。
视频URL有效期:24小时。
视频尺寸:由输入首帧图像和设置的分辨率
resolution决定。模型将尽量保持输入首帧图像的宽高比不变,缩放总像素数接近目标值。因编码规范,输出视频的宽高必须被16整除,模型会自动微调尺寸。
例如:输入图像750×1000(宽高比 3:4 = 0.75),设置 resolution = "720P"(目标总像素约 92 万)。最终输出可能为 816 × 1104(宽高比 ≈ 0.739,总像素 ≈ 90 万),且宽高均为 16 的倍数。
计费与限流
API文档
常见问题
Q:如何生成特定宽高比(如3:4)的视频?
A:当前 API 不支持直接指定视频宽高比。您只能通过 resolution 参数设置视频的分辨率。
resolution 控制的是视频的总像素数量,而非严格固定宽高比。模型会优先保留首帧图像(first_frame_url)的原始宽高比,并在此基础上进行微调,以满足视频编码要求(即宽度和高度均需为 16 的整数倍)。因此,若希望输出接近 3:4 的视频,建议上传宽高比为 3:4 的首帧图像,模型将尽可能保持该比例。
例如:输入图像750×1000(宽高比 3:4 = 0.75),设置 resolution = "720P"(目标总像素约 92 万)。最终输出可能为 816 × 1104(宽高比 ≈ 0.739,总像素 ≈ 90 万),且宽高均为 16 的倍数。
Q:为什么运行SDK代码时出现 “url error, please check url!” 错误?
A:请确保:
DashScope Python SDK 版本不低于
1.25.8。DashScope Java SDK 版本不低于
2.22.6。
若版本过低,可能会触发 “url error, please check url!” 的错误。请参考升级SDK进行更新。



