万相-视频编辑wan2.7模型,支持输入多模态(文本/图像/视频),可完成指令编辑和视频迁移任务。
相关文档:指南文档
适用范围
为确保调用成功,请务必保证模型、endpoint URL 和 API Key 均属于同一地域。跨地域调用将会失败。
选择模型:确认模型所属的地域。
选择 URL:选择对应的地域 Endpoint URL,支持HTTP URL或 DashScope SDK URL。
配置 API Key:获取该地域的API Key,再配置API Key到环境变量。
本文的示例代码适用于北京地域。
HTTP调用
视频编辑任务耗时较长(通常为1-5分钟),API采用异步调用的方式。整个流程包含 “创建任务 -> 轮询获取” 两个核心步骤,具体如下:
步骤1:创建任务获取任务ID
北京
POST https://dashscope.aliyuncs.com/api/v1/services/aigc/video-generation/video-synthesis
新加坡
POST https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/video-generation/video-synthesis
创建成功后,使用接口返回的
task_id查询结果,task_id 有效期为 24 小时。请勿重复创建任务,轮询获取即可。新手指引请参见Postman。
请求参数 | 纯指令编辑(修改视频风格)指令+参考图编辑(局部替换) |
Content-Type 请求内容类型。此参数必须设置为 | |
Authorization 请求身份认证。接口使用阿里云百炼API-Key进行身份认证。示例值:Bearer sk-xxxx。 | |
X-DashScope-Async 异步处理配置参数。HTTP请求只支持异步,必须设置为 重要 缺少此请求头将报错:“current user api does not support synchronous calls”。 | |
请求体(Request Body) | |
model 模型名称。 示例值:wan2.7-videoedit。 | |
input 输入的基本信息,如提示词等。 | |
parameters 视频处理参数,如设置视频分辨率、设置视频时长、开启prompt智能改写、添加水印等。 |
响应参数 | 成功响应请保存 task_id,用于查询任务状态与结果。 异常响应创建任务失败,请参见错误信息进行解决。 |
output 任务输出信息。 | |
request_id 请求唯一标识。可用于请求明细溯源和问题排查。 | |
code 请求失败的错误码。请求成功时不会返回此参数,详情请参见错误信息。 | |
message 请求失败的详细信息。请求成功时不会返回此参数,详情请参见错误信息。 |
步骤2:根据任务ID查询结果
北京
GET https://dashscope.aliyuncs.com/api/v1/tasks/{task_id}
新加坡
GET https://dashscope-intl.aliyuncs.com/api/v1/tasks/{task_id}
请求参数 | 查询任务结果将 |
请求头(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.25.16,再运行以下代码。
若版本过低,可能会触发 "url error, please check url!" 等错误。请参考安装SDK进行更新。
根据模型所在地域设置 base_http_api_url:
北京
dashscope.base_http_api_url = 'https://dashscope.aliyuncs.com/api/v1'
新加坡
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
同步调用
请求示例
import base64
import mimetypes
from http import HTTPStatus
from dashscope import VideoSynthesis
import dashscope
import os
# 以下为北京地域URL,各地域的URL不同
dashscope.base_http_api_url = 'https://dashscope.aliyuncs.com/api/v1'
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx"
# 各地域的API Key不同。获取API Key:https://help.aliyun.com/zh/model-studio/get-api-key
api_key = os.getenv("DASHSCOPE_API_KEY")
# 格式为 data:{MIME_type};base64,{base64_data}
def encode_file(file_path):
mime_type, _ = mimetypes.guess_type(file_path)
if not mime_type or not mime_type.startswith("image/"):
raise ValueError("不支持或无法识别的图像格式")
with open(file_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
return f"data:{mime_type};base64,{encoded_string}"
# 参考图像URL,支持以下三种输入方式
# 【方式一】使用公网图片URL
reference_image_url = "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260402/fwjpqf/wan2.7-videoedit-change-clothes.png"
# 【方式二】使用本地文件(支持绝对路径和相对路径)
# 格式要求:file:// + 文件路径
# 示例(绝对路径):
# reference_image_url = "file://" + "/path/to/image.png" # Linux/macOS
# reference_image_url = "file://" + "C:/path/to/image.png" # Windows
# 示例(相对路径):
# reference_image_url = "file://" + "./image.png" # 相对当前执行文件的路径
# 【方式三】使用Base64编码
# reference_image_url = encode_file("/path/to/image.png")
def sample_sync_call_videoedit():
# call sync api, will return the result
print('please wait...')
rsp = VideoSynthesis.call(
api_key=api_key,
model='wan2.7-videoedit',
prompt='将视频中女孩的衣服替换为图片中的衣服',
media=[
{
"type": "video",
"url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260403/nlspwm/T2VA_22.mp4"
},
{
"type": "reference_image",
"url": reference_image_url
}
],
resolution='720P',
prompt_extend=True,
watermark=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_videoedit()响应示例
video_url 有效期24小时,请及时下载视频。{
"status_code": 200,
"request_id": "d6c3c865-34e9-98a9-a53d-xxxxxx",
"code": null,
"message": "",
"output": {
"task_id": "1de7c853-755a-454a-91bc-xxxxxx",
"task_status": "SUCCEEDED",
"video_url": "https://dashscope-a717.oss-accelerate.aliyuncs.com/xxx.mp4?Expires=xxxx",
"submit_time": "2026-04-10 17:16:30.821",
"scheduled_time": "2026-04-10 17:16:46.379",
"end_time": "2026-04-10 17:24:59.352",
"orig_prompt": "将视频中女孩的衣服替换为图片中的衣服"
},
"usage": {
"video_count": 1,
"video_duration": 0,
"video_ratio": "",
"duration": 10.08,
"input_video_duration": 5.04,
"output_video_duration": 5.04,
"SR": 720
}
}异步调用
请求示例
import base64
import mimetypes
from http import HTTPStatus
from dashscope import VideoSynthesis
import dashscope
import os
# 以下为北京地域URL,各地域的URL不同
dashscope.base_http_api_url = 'https://dashscope.aliyuncs.com/api/v1'
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx"
# 各地域的API Key不同。获取API Key:https://help.aliyun.com/zh/model-studio/get-api-key
api_key = os.getenv("DASHSCOPE_API_KEY")
# 格式为 data:{MIME_type};base64,{base64_data}
def encode_file(file_path):
mime_type, _ = mimetypes.guess_type(file_path)
if not mime_type or not mime_type.startswith("image/"):
raise ValueError("不支持或无法识别的图像格式")
with open(file_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
return f"data:{mime_type};base64,{encoded_string}"
# 参考图像URL,支持以下三种输入方式
# 【方式一】使用公网图片URL
reference_image_url = "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260402/fwjpqf/wan2.7-videoedit-change-clothes.png"
# 【方式二】使用本地文件(支持绝对路径和相对路径)
# 格式要求:file:// + 文件路径
# 示例(绝对路径):
# reference_image_url = "file://" + "/path/to/image.png" # Linux/macOS
# reference_image_url = "file://" + "C:/path/to/image.png" # Windows
# 示例(相对路径):
# reference_image_url = "file://" + "./image.png" # 相对当前执行文件的路径
# 【方式三】使用Base64编码
# reference_image_url = encode_file("/path/to/image.png")
def sample_async_call_videoedit():
# call async api, will return the task information
# you can get task status with the returned task id.
rsp = VideoSynthesis.async_call(
api_key=api_key,
model='wan2.7-videoedit',
prompt='将视频中女孩的衣服替换为图片中的衣服',
media=[
{
"type": "video",
"url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260403/nlspwm/T2VA_22.mp4"
},
{
"type": "reference_image",
"url": reference_image_url
}
],
resolution='720P',
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))
# 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_videoedit()响应示例
1、创建任务的响应示例
{
"status_code": 200,
"request_id": "f16ae7e9-d518-92f8-a02c-xxxxxx",
"code": "",
"message": "",
"output": {
"task_id": "05e68c7e-850c-49e4-b866-xxxxxx",
"task_status": "PENDING",
"video_url": ""
},
"usage": null
}2、查询任务结果的响应示例
video_url 有效期24小时,请及时下载视频。{
"status_code": 200,
"request_id": "d6c3c865-34e9-98a9-a53d-xxxxxx",
"code": null,
"message": "",
"output": {
"task_id": "1de7c853-755a-454a-91bc-xxxxxx",
"task_status": "SUCCEEDED",
"video_url": "https://dashscope-a717.oss-accelerate.aliyuncs.com/xxx.mp4?Expires=xxxx",
"submit_time": "2026-04-10 17:16:30.821",
"scheduled_time": "2026-04-10 17:16:46.379",
"end_time": "2026-04-10 17:24:59.352",
"orig_prompt": "将视频中女孩的衣服替换为图片中的衣服"
},
"usage": {
"video_count": 1,
"video_duration": 0,
"video_ratio": "",
"duration": 10.08,
"input_video_duration": 5.04,
"output_video_duration": 5.04,
"SR": 720
}
}Java SDK调用
请确保 DashScope Java SDK 版本不低于 2.22.14,再运行以下代码。
若版本过低,可能会触发 "url error, please check url!" 等错误。请参考安装SDK进行更新。
根据模型所在地域设置 Constants.baseHttpApiUrl:
北京
Constants.baseHttpApiUrl = "https://dashscope.aliyuncs.com/api/v1";
新加坡
Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
同步调用
请求示例
// Copyright (c) Alibaba, Inc. and its affiliates.
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;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
public class VideoEdit {
static {
// 以下为北京地域url,各地域的url不同
Constants.baseHttpApiUrl = "https://dashscope.aliyuncs.com/api/v1";
}
// 若没有配置环境变量,请用百炼API Key将下行替换为:apiKey="sk-xxx"
// 各地域的API Key不同。获取API Key:https://help.aliyun.com/zh/model-studio/get-api-key
public static String apiKey = System.getenv("DASHSCOPE_API_KEY");
// 参考图像URL,支持以下三种输入方式
// 【方式一】使用公网图片URL
static String referenceImageUrl = "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260402/fwjpqf/wan2.7-videoedit-change-clothes.png";
// 【方式二】使用本地文件(支持绝对路径和相对路径)
// 格式要求:file:// + 文件路径
// 示例(绝对路径):
// static String referenceImageUrl = "file://" + "/path/to/image.png"; // Linux/macOS
// static String referenceImageUrl = "file://" + "C:/path/to/image.png"; // Windows
// 示例(相对路径):
// static String referenceImageUrl = "file://" + "./image.png"; // 相对当前执行文件的路径
// 【方式三】使用Base64编码
// static String referenceImageUrl = encodeFile("/path/to/image.png");
// 格式为 data:{MIME_type};base64,{base64_data}
public static String encodeFile(String filePath) {
Path path = Paths.get(filePath);
if (!Files.exists(path)) {
throw new IllegalArgumentException("文件不存在: " + filePath);
}
String mimeType = null;
try {
mimeType = Files.probeContentType(path);
} catch (IOException e) {
throw new IllegalArgumentException("无法检测文件类型: " + filePath);
}
if (mimeType == null || !mimeType.startsWith("image/")) {
throw new IllegalArgumentException("不支持或无法识别的图像格式");
}
byte[] fileBytes = null;
try {
fileBytes = Files.readAllBytes(path);
} catch (IOException e) {
throw new IllegalArgumentException("无法读取文件内容: " + filePath);
}
String encodedString = Base64.getEncoder().encodeToString(fileBytes);
return "data:" + mimeType + ";base64," + encodedString;
}
public static void videoEdit() throws ApiException, NoApiKeyException, InputRequiredException {
VideoSynthesis vs = new VideoSynthesis();
List<VideoSynthesisParam.Media> media = new ArrayList<VideoSynthesisParam.Media>(){{
add(VideoSynthesisParam.Media.builder()
.url("https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260403/nlspwm/T2VA_22.mp4")
.type("video")
.build());
add(VideoSynthesisParam.Media.builder()
.url(referenceImageUrl)
.type("reference_image")
.build());
}};
Map<String, Object> parameters = new HashMap<>();
parameters.put("resolution", "720P");
parameters.put("prompt_extend", true);
parameters.put("watermark", true);
VideoSynthesisParam param =
VideoSynthesisParam.builder()
.apiKey(apiKey)
.model("wan2.7-videoedit")
.prompt("将视频中女孩的衣服替换为图片中的衣服")
.media(media)
.parameters(parameters)
.build();
System.out.println("please wait...");
VideoSynthesisResult result = vs.call(param);
System.out.println(JsonUtils.toJson(result));
}
public static void main(String[] args) {
try {
videoEdit();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}响应示例
video_url 有效期24小时,请及时下载视频。{
"request_id": "0a15ad3c-cde7-9f7e-b8d2-xxxxxx",
"output": {
"task_id": "0025d1e1-009a-4f53-9c27-xxxxx",
"task_status": "SUCCEEDED",
"video_url": "https://dashscope-a717.oss-accelerate.aliyuncs.com/xxx.mp4?Expires=xxx",
"orig_prompt": "将视频中女孩的衣服替换为图片中的衣服",
"submit_time": "2026-04-10 17:21:01.719",
"scheduled_time": "2026-04-10 17:21:13.182",
"end_time": "2026-04-10 17:31:41.286"
},
"usage": {
"video_count": 1,
"duration": 10.08,
"input_video_duration": 5.04,
"output_video_duration": 5.04,
"SR": "720"
},
"status_code": 200,
"code": "",
"message": ""
}异步调用
请求示例
// Copyright (c) Alibaba, Inc. and its affiliates.
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.aigc.videosynthesis.VideoSynthesisListResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.task.AsyncTaskListParam;
import com.alibaba.dashscope.utils.JsonUtils;
import com.alibaba.dashscope.utils.Constants;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
public class VideoEdit {
static {
// 以下为北京地域url,各地域的url不同
Constants.baseHttpApiUrl = "https://dashscope.aliyuncs.com/api/v1";
}
// 若没有配置环境变量,请用百炼API Key将下行替换为:apiKey="sk-xxx"
// 各地域的API Key不同。获取API Key:https://help.aliyun.com/zh/model-studio/get-api-key
public static String apiKey = System.getenv("DASHSCOPE_API_KEY");
// 参考图像URL,支持以下三种输入方式
// 【方式一】使用公网图片URL
static String referenceImageUrl = "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260402/fwjpqf/wan2.7-videoedit-change-clothes.png";
// 【方式二】使用本地文件(支持绝对路径和相对路径)
// 格式要求:file:// + 文件路径
// 示例(绝对路径):
// static String referenceImageUrl = "file://" + "/path/to/image.png"; // Linux/macOS
// static String referenceImageUrl = "file://" + "C:/path/to/image.png"; // Windows
// 示例(相对路径):
// static String referenceImageUrl = "file://" + "./image.png"; // 相对当前执行文件的路径
// 【方式三】使用Base64编码
// static String referenceImageUrl = encodeFile("/path/to/image.png");
// 格式为 data:{MIME_type};base64,{base64_data}
public static String encodeFile(String filePath) {
Path path = Paths.get(filePath);
if (!Files.exists(path)) {
throw new IllegalArgumentException("文件不存在: " + filePath);
}
String mimeType = null;
try {
mimeType = Files.probeContentType(path);
} catch (IOException e) {
throw new IllegalArgumentException("无法检测文件类型: " + filePath);
}
if (mimeType == null || !mimeType.startsWith("image/")) {
throw new IllegalArgumentException("不支持或无法识别的图像格式");
}
byte[] fileBytes = null;
try {
fileBytes = Files.readAllBytes(path);
} catch (IOException e) {
throw new IllegalArgumentException("无法读取文件内容: " + filePath);
}
String encodedString = Base64.getEncoder().encodeToString(fileBytes);
return "data:" + mimeType + ";base64," + encodedString;
}
public static void videoEdit() throws ApiException, NoApiKeyException, InputRequiredException {
VideoSynthesis vs = new VideoSynthesis();
List<VideoSynthesisParam.Media> media = new ArrayList<VideoSynthesisParam.Media>(){{
add(VideoSynthesisParam.Media.builder()
.url("https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260403/nlspwm/T2VA_22.mp4")
.type("video")
.build());
add(VideoSynthesisParam.Media.builder()
.url(referenceImageUrl)
.type("reference_image")
.build());
}};
Map<String, Object> parameters = new HashMap<>();
parameters.put("resolution", "720P");
parameters.put("prompt_extend", true);
parameters.put("watermark", true);
VideoSynthesisParam param =
VideoSynthesisParam.builder()
.apiKey(apiKey)
.model("wan2.7-videoedit")
.prompt("将视频中女孩的衣服替换为图片中的衣服")
.media(media)
.parameters(parameters)
.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 listTask() throws ApiException, NoApiKeyException {
VideoSynthesis is = new VideoSynthesis();
AsyncTaskListParam param = AsyncTaskListParam.builder().build();
param.setApiKey(apiKey);
VideoSynthesisListResult result = is.list(param);
System.out.println(result);
}
// 获取单个任务结果
public static void fetchTask(String taskId) throws ApiException, NoApiKeyException {
VideoSynthesis is = new VideoSynthesis();
// 如果已设置 DASHSCOPE_API_KEY 为环境变量,apiKey 可为空
VideoSynthesisResult result = is.fetch(taskId, apiKey);
System.out.println(result.getOutput());
System.out.println(result.getUsage());
}
public static void main(String[] args) {
try {
videoEdit();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}响应示例
1、创建任务的响应示例
{
"request_id": "f16ae7e9-d518-92f8-a02c-xxxxxx",
"output": {
"task_id": "05e68c7e-850c-49e4-b866-xxxxxx",
"task_status": "PENDING",
"video_url": ""
},
"usage": null,
"status_code": 200,
"code": "",
"message": ""
}2、查询任务结果的响应示例
video_url 有效期24小时,请及时下载视频。{
"request_id": "0a15ad3c-cde7-9f7e-b8d2-xxxxxx",
"output": {
"task_id": "0025d1e1-009a-4f53-9c27-xxxxx",
"task_status": "SUCCEEDED",
"video_url": "https://dashscope-a717.oss-accelerate.aliyuncs.com/xxx.mp4?Expires=xxx",
"orig_prompt": "将视频中女孩的衣服替换为图片中的衣服",
"submit_time": "2026-04-10 17:21:01.719",
"scheduled_time": "2026-04-10 17:21:13.182",
"end_time": "2026-04-10 17:31:41.286"
},
"usage": {
"video_count": 1,
"duration": 10.08,
"input_video_duration": 5.04,
"output_video_duration": 5.04,
"SR": "720"
},
"status_code": 200,
"code": "",
"message": ""
}错误码
如果模型调用失败并返回报错信息,请参见错误信息进行解决。