萬相2.7-視頻編輯API參考

更新時間:
Copy as MD

萬相2.7-視頻編輯模型,支援輸入多模態(文本/映像/視頻),可完成指令編輯和視頻遷移任務。

相關文檔指南文檔

適用範圍

為確保調用成功,請務必保證模型、endpoint URL 和 API Key 均屬於同一地區。跨地區調用將會失敗。

說明

本文的範例程式碼適用於新加坡地區

重要

新加坡地區的舊版網域名稱 https://dashscope-intl.aliyuncs.com 即將下線,請及時遷移到新版網域名稱 https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com

HTTP調用

視頻編輯任務耗時較長(通常為1-5分鐘),API採用非同步呼叫的方式。整個流程包含 “建立任務 -> 輪詢擷取” 兩個核心步驟,具體如下:

步驟1:建立任務擷取任務ID

新加坡

POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/video-generation/video-synthesis

調用時請將WorkspaceId替換為真實的Workspace ID

北京

POST https://dashscope.aliyuncs.com/api/v1/services/aigc/video-generation/video-synthesis

說明
  • 建立成功後,使用介面返回的 task_id 查詢結果,task_id 有效期間為 24 小時。請勿重複建立任務,輪詢擷取即可。

  • 新手指引請參見Postman

請求參數

純指令編輯(修改視頻風格)

curl --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/video-generation/video-synthesis' \
    -H 'X-DashScope-Async: enable' \
    -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
    -H 'Content-Type: application/json' \
    -d '{
    "model": "wan2.7-videoedit",
    "input": {
        "prompt": "將整個畫面轉換為黏土風格",
        "media": [
            {
                "type": "video",
                "url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260402/ldnfdf/wan2.7-videoedit-style-change.mp4"
            }
        ]
    },
    "parameters": {
        "resolution": "720P",
        "prompt_extend": true,
        "watermark": true
    }
}'

指令+參考圖編輯(本機覆寫)

curl --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/video-generation/video-synthesis' \
    -H 'X-DashScope-Async: enable' \
    -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
    -H 'Content-Type: application/json' \
    -d '{
    "model": "wan2.7-videoedit",
    "input": {
        "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": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260402/fwjpqf/wan2.7-videoedit-change-clothes.png"
            }
        ]
    },
    "parameters": {
        "resolution": "720P",
        "prompt_extend": true,
        "watermark": true
    }
}'

Content-Type string (必選)

請求內容類型。此參數必須設定為application/json

Authorization string(必選)

請求身份認證。介面使用阿里雲百鍊API Key進行身份認證。樣本值:Bearer sk-xxxx。

X-DashScope-Async string (必選)

非同步處理配置參數。HTTP請求只支援非同步,必須設定為enable

重要

缺少此要求標頭將報錯:“current user api does not support synchronous calls”。

請求體(Request Body)

model string (必選)

模型名稱。

樣本值:wan2.7-videoedit。

input object (必選)

輸入的基本資料,如提示詞等。

屬性

prompt string (可選)

文本提示詞。用來描述產生視頻中期望包含的元素和視覺特點。

支援中英文,每個漢字/字母佔一個字元,超過部分會自動截斷。

  • wan2.7-videoedit:長度不超過5000個字元。

樣本值:為人物換上酷閃的衣服,再戴參考圖裡的帽子。

negative_prompt string (可選)

反向提示詞,用來描述不希望在視頻畫面中出現的內容,可以對視頻畫面進行限制。

支援中英文,長度不超過500個字元,超過部分會自動截斷。

樣本值:低解析度、錯誤、最差品質、低品質、殘缺、多餘的手指、比例不良等。

media array (必選)

媒體素材列表,用於指定視頻產生所需的參考素材(映像、視頻)。

數組的每個元素為一個媒體對象,包含 typeurl 欄位。

屬性

type string (必選)

媒體素材類型。可選值為:

  • video:必傳。待編輯的視頻。

  • reference_image:可選。參考映像。

素材限制:

  • 視頻有且僅有1個。

  • 參考映像最多傳入4張。

url string (必選)

媒體素材URL或 Base 64 編碼資料。素材包括視頻和映像。

傳入的視訊(type=video)

待編輯的視頻檔案的 URL。

視頻限制:

  • 格式:mp4、mov。

  • 時間長度:2~10s。

  • 解析度:寬度和高度範圍為[240,4096]像素。

  • 寬高比:1:8~8:1。

  • 檔案大小:不超過100MB。

支援輸入的格式:

  1. 公網URL:

    • 支援 HTTP 和 HTTPS 協議。

    • 樣本值:https://xxx/xxx.mp4。

傳入映像(type=reference_image)

參考映像URL或 Base 64 編碼資料。

映像限制:

  • 格式:JPEG、JPG、PNG(不支援透明通道)、BMP、WEBP。

  • 解析度:寬度和高度範圍為[240, 8000]像素。

  • 寬高比:1:8~8:1。

  • 檔案大小:不超過20MB。

支援輸入的格式:

  1. 公網URL:

    • 支援 HTTP 或 HTTPS 協議。

    • 樣本值:https://xxx/xxx.png。

  2. Base 64 編碼字串:

    • 資料格式:data:{MIME_type};base64,{base64_data}

    • 樣本值:data:image/png;base64,GDU7MtCZzEbTbmRZ......(樣本已截斷,僅做示範)。

    Base64編碼資料格式

    格式: data:{MIME_type};base64,{base64_data} 。

    • {base64_data}:影像檔經過 Base 64 編碼後的字串。

    • {MIME_type}:映像的媒體類型,需與檔案格式對應。

    映像格式

    MIME Type

    JPEG

    image/jpeg

    JPG

    image/jpeg

    PNG

    image/png

    BMP

    image/bmp

    WEBP

    image/webp

parameters object (可選)

視頻處理參數,如設定視頻解析度、設定視頻時間長度、開啟prompt智能改寫、添加浮水印等。

屬性

resolution string (可選)

產生視頻的解析度檔位,用於控制視頻的清晰度(總像素)。

  • wan2.7-videoedit:可選值:720P、1080P。預設值為1080P

ratio string (可選)

產生視頻的寬高比。

生效邏輯:

  • 不傳 ratio 參數:以輸入視頻的寬高比產生近似比例的視頻。

  • 傳入ratio參數:按指定的 ratio 產生視頻。

可選值為:

  • 16:9

  • 9:16

  • 1:1

  • 4:3

  • 3:4

不同寬高比對應的輸出視頻解析度(寬高像素值)見下方表格。

解析度檔位

寬高比

輸出視頻解析度(寬*高)

720P

16:9

1280*720

9:16

720*1280

1:1

960*960

4:3

1104*832

3:4

832*1104

1080P

16:9

1920*1080

9:16

1080*1920

1:1

1440*1440

4:3

1648*1248

3:4

1248*1648

duration integer (可選)

產生視頻的時間長度,單位為秒。

使用建議:此參數僅在需要“截斷視頻”時才需配置。如果希望輸出視頻與輸入視頻時間長度一致,無需設定(或傳入預設值 0)。

使用規則:

  • 預設行為:預設值為 0,代表直接使用輸入視頻的時間長度,不進行截斷。

  • 截斷生效:當傳入指定時間長度時,系統會從原視頻的 0 秒起,截取至 duration 設定的長度。

  • 取值範圍:支援 [2, 10] 之間的整數。

audio_setting string (可選)

視頻聲音設定。

  • auto (預設):模型根據 prompt 內容智能判斷。若提示詞涉及聲音描述,可能重建音頻;否則可能保留輸入素材的原聲。

  • origin:強制保留輸入視頻的原聲,不重建。

prompt_extend boolean (可選)

是否開啟prompt智能改寫。開啟後使用大模型對輸入prompt進行智能改寫。對於較短的prompt產生效果提升明顯,但會增加耗時。

  • true:預設值,開啟智能改寫。

  • false:不開啟智能改寫。

watermark boolean (可選)

是否添加浮水印標識,浮水印位於視頻右下角,文案固定為“AI產生”。

  • false:預設值,不添加浮水印。

  • true:添加浮水印。

樣本值:false。

seed integer (可選)

隨機數種子,取值範圍為[0, 2147483647]

未指定時,系統自動產生隨機種子。若需提升產生結果的可複現性,建議固定seed值。

請注意,由於模型產生具有機率性,即使使用相同 seed,也不能保證每次產生結果完全一致。

響應參數

成功響應

請儲存 task_id,用於查詢任務狀態與結果。

{
    "output": {
        "task_status": "PENDING",
        "task_id": "0385dc79-5ff8-4d82-bcb6-xxxxxx"
    },
    "request_id": "4909100c-7b5a-9f92-bfe5-xxxxxx"
}

異常響應

建立任務失敗,請參見錯誤碼進行解決。

{
    "code": "InvalidApiKey",
    "message": "No API-key provided.",
    "request_id": "7438d53d-6eb8-4596-8835-xxxxxx"
}

output object

任務輸出資訊。

屬性

task_id string

任務ID。查詢有效期間24小時。

task_status string

任務狀態。

枚舉值

  • PENDING:任務排隊中

  • RUNNING:任務處理中

  • SUCCEEDED:任務執行成功

  • FAILED:任務執行失敗

  • CANCELED:任務已取消

  • UNKNOWN:任務不存在或狀態未知

request_id string

請求唯一標識。可用於請求明細溯源和問題排查。

code string

請求失敗的錯誤碼。請求成功時不會返回此參數,詳情請參見錯誤碼

message string

請求失敗的詳細資料。請求成功時不會返回此參數,詳情請參見錯誤碼

步驟2:根據任務ID查詢結果

新加坡

GET https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/tasks/{task_id}

調用時請將WorkspaceId替換為真實的Workspace ID

北京

GET https://dashscope.aliyuncs.com/api/v1/tasks/{task_id}

說明
  • 輪詢建議:視頻產生過程約需數分鐘,建議採用輪詢機制,並設定合理的查詢間隔(如 15 秒)來擷取結果。

  • 任務狀態流轉:PENDING(排隊中)→ RUNNING(處理中)→ SUCCEEDED(成功)/ FAILED(失敗)。

  • 結果連結:任務成功後返回視頻連結,有效期間為 24 小時。建議在擷取連結後立即下載並轉存至永久儲存(如阿里雲 OSS)。

  • task_id 有效期間24小時,逾時後將無法查詢結果,介面將返回任務狀態為UNKNOWN

請求參數

查詢任務結果

{task_id}完整替換為上一步介面返回的task_id的值。task_id查詢有效期間為24小時。

curl -X GET https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/tasks/{task_id} \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"
要求標頭(Headers)

Authorization string(必選)

請求身份認證。介面使用阿里雲百鍊API Key進行身份認證。樣本值:Bearer sk-xxxx。

URL路徑參數(Path parameters)

task_id string(必選)

任務ID。

響應參數

任務執行成功

視頻URL僅保留24小時,逾時後會被自動清除,請及時儲存產生的視頻。

{
    "request_id": "f16ae7e9-d518-92f8-a02c-xxxxxx",
    "output": {
        "task_id": "05e68c7e-850c-49e4-b866-xxxxxx",
        "task_status": "SUCCEEDED",
        "submit_time": "2026-04-03 00:08:03.576",
        "scheduled_time": "2026-04-03 00:08:13.408",
        "end_time": "2026-04-03 00:11:57.286",
        "orig_prompt": "將視頻中女孩的衣服替換為圖片中的衣服",
        "video_url": "https://dashscope-a717.oss-accelerate.aliyuncs.com/xxx.mp4?xxxx"
    },
    "usage": {
        "duration": 10.04,
        "input_video_duration": 5.02,
        "output_video_duration": 5.02,
        "video_count": 1,
        "SR": 720
    }
}

任務執行失敗

若任務執行失敗,task_status將置為 FAILED,並提供錯誤碼和資訊。請參見錯誤碼進行解決。

{
    "request_id": "e5d70b02-ebd3-98ce-9fe8-759d7d7b107d",
    "output": {
        "task_id": "86ecf553-d340-4e21-af6e-a0c6a421c010",
        "task_status": "FAILED",
        "code": "InvalidParameter",
        "message": "The size is not match xxxxxx"
    }
}

任務查詢到期

task_id查詢有效期間為 24 小時,逾時後將無法查詢,返回以下報錯資訊。

{
    "request_id": "a4de7c32-7057-9f82-8581-xxxxxx",
    "output": {
        "task_id": "502a00b1-19d9-4839-a82f-xxxxxx",
        "task_status": "UNKNOWN"
    }
}

output object

任務輸出資訊。

屬性

task_id string

任務ID。查詢有效期間24小時。

task_status string

任務狀態。

枚舉值

  • PENDING:任務排隊中

  • RUNNING:任務處理中

  • SUCCEEDED:任務執行成功

  • FAILED:任務執行失敗

  • CANCELED:任務已取消

  • UNKNOWN:任務不存在或狀態未知

輪詢過程中的狀態流轉:

  • PENDING(排隊中) → RUNNING(處理中)→ SUCCEEDED(成功)/ FAILED(失敗)。

  • 初次查詢狀態通常為 PENDING(排隊中)或 RUNNING(處理中)。

  • 當狀態變為 SUCCEEDED 時,響應中將包含產生的視頻URL。

  • 若狀態為 FAILED,請檢查錯誤資訊並重試。

  • 若狀態為 CANCELED,表示任務已取消,如需繼續請重新提交任務。

  • 若狀態為 UNKNOWN,表示任務不存在或狀態未知,可能在 task_id 不存在或超過 24 小時有效期間後出現。

submit_time string

任務提交時間。時區為UTC+8,格式為 YYYY-MM-DD HH:mm:ss.SSS。

scheduled_time string

任務執行時間。時區為UTC+8,格式為 YYYY-MM-DD HH:mm:ss.SSS。

end_time string

任務完成時間。時區為UTC+8,格式為 YYYY-MM-DD HH:mm:ss.SSS。

video_url string

視頻URL。僅在 task_status 為 SUCCEEDED 時返回。

連結有效期間24小時,可通過此URL下載視頻。視頻格式為MP4(H.264 編碼)。

orig_prompt string

原始輸入的prompt,對應請求參數prompt

code string

請求失敗的錯誤碼。請求成功時不會返回此參數,詳情請參見錯誤碼

message string

請求失敗的詳細資料。請求成功時不會返回此參數,詳情請參見錯誤碼

usage object

輸出資訊統計,只對成功的結果計數。

屬性

input_video_duration float

輸入視頻的時間長度,單位秒。

output_video_duration float

輸出視頻的時間長度,單位秒。

duration float

總的視頻時間長度,用於計費。

計費公式:duration=input_video_duration+output_video_duration

SR integer

輸出視頻的解析度檔位。樣本值:720。

video_count integer

產生視頻的數量。固定為1。

request_id string

請求唯一標識。可用於請求明細溯源和問題排查。

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://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'

調用時請將WorkspaceId替換為真實的Workspace ID

北京

dashscope.base_http_api_url = 'https://dashscope.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://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'

# 若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx"
# 各地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/en/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://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'

# 若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx"
# 各地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/en/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://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1";

調用時請將WorkspaceId替換為真實的Workspace ID

北京

Constants.baseHttpApiUrl = "https://dashscope.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://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1";
    }

    // 若沒有配置環境變數,請用百鍊API Key將下行替換為:apiKey="sk-xxx"
    // 各地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/en/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://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1";
    }

    // 若沒有配置環境變數,請用百鍊API Key將下行替換為:apiKey="sk-xxx"
    // 各地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/en/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": ""
}

錯誤碼

如果模型調用失敗並返回報錯資訊,請參見錯誤碼進行解決。