Skip to main content

I2V-based on first and last frames

The Wan I2V model supports generating smoothly transitioned videos based on a first frame image, a last frame image, and a text prompt.

  • Video specifications: Fixed video duration (5 s), with a specified video resolution (480P/720P/1080P).
  • Other capabilities: Also supports intelligent prompt rewriting and adding watermarks.
Quick access:API reference|Prompt guide

QuickStart

Enter a promptEnter a first frame imageEnter a last frame imageOutput video (silent video)
A cute blue little monster with a slightly sad expression stands in the rain. The camera slowly zooms in, freezing on the moment the monster raises its head to gaze up at the sky.
image
image
Before you make a call, first obtain and configure an API Key, and then configure the API Key in environment variables. To make calls through an SDK, install the DashScope SDK.
  • Python SDK
  • Java SDK
  • curl
Make sure the DashScope Python SDK version is 2.0.1 or later before you run the following code.If the version is too low, errors such as "url error, please check url!" may occur. Refer to Install an SDK to update it.
import os
import dashscope
from dashscope import DashScopeError

# The following configuration is for the Singapore region. Replace {WorkspaceId} with your actual workspace ID. The region value varies by region. For the endpoints of each region, see https://www.alibabacloud.com/help/model-studio/image-to-video-by-first-and-last-frame-api-reference
# API keys are region-specific. To get an API key, see https://www.alibabacloud.com/help/model-studio/get-api-key
client = dashscope.Client(
    api_key=os.getenv("DASHSCOPE_API_KEY", "YOUR_API_KEY"),
    workspace="{WorkspaceId}",
    region="ap-southeast-1",
)

print('please wait...')
# generate is a one-step call (submit the task and wait for completion) and returns a completed Job
# first_frame_url / last_frame_url are input-section fields that are not part of the named signature, and are passed through via extra_input
try:
    job = client.videos.generate(model="wan2.2-kf2v-flash",
                                 prompt="一个可爱且表情有些忧伤的蓝色小怪物站在雨中。镜头缓慢拉近,定格在小怪物抬头仰望天空的瞬间。",
                                 extra_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"},
                                 duration=5,  # Fixed at 5. Do not change
                                 prompt_extend=True,
                                 watermark=True)
    print(job)
    print("video_url:", job.video_url)
except DashScopeError as e:
    print(f"HTTP status code: {e.status_code}")
    print(f"Error code: {e.code}")
    print(f"Error message: {e.message}")
    print("See the documentation: https://www.alibabacloud.com/help/model-studio/developer-reference/error-code")
Sample Output
The video_url is valid for 24 hours. Download the video promptly.
{
    "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",
         ...
    },
    ...
}

Scope

  • The models supported vary by region, and resources are independent of each other. For the models supported in each region, see the Model Studio console.
  • When you make a call, make sure that the model, Endpoint URL, and API Key belong to the same region. Cross-region calls will fail.
The sample code in this topic applies to the Singapore region.

Core capabilities

First and last frame video generation

Supported models: All models. Description: Generates a smoothly transitioned video (silent video) based on the first and last frames. Parameter settings:
  • first_frame_url: Required. You must provide the first frame image.
  • last_frame_url: Required. You must provide the last frame image.
  • prompt: Optional but recommended. It controls the changes in the video frames.
Enter a promptEnter a first frame imageEnter a last frame imageOutput video (silent video)
Realistic style, a curious little black cat looking up at the sky, with the camera gradually rising from eye level and finally capturing its curious gaze from a bird's-eye view.
首帧图像
尾帧图像
  • Python SDK
  • Java SDK
  • curl
Make sure the DashScope Python SDK version is 2.0.1 or later. For updates, see Install SDK.
import os
import dashscope
from dashscope import DashScopeError

# The following configuration is for the Singapore region. Replace {WorkspaceId} with your actual workspace ID. The region value varies by region. For the endpoints of each region, see https://www.alibabacloud.com/help/model-studio/image-to-video-by-first-and-last-frame-api-reference
# API keys are region-specific. To get an API key, see https://www.alibabacloud.com/help/model-studio/get-api-key
client = dashscope.Client(
    api_key=os.getenv("DASHSCOPE_API_KEY", "YOUR_API_KEY"),
    workspace="{WorkspaceId}",
    region="ap-southeast-1",
)

def sample_async_call_kf2v():
    # Asynchronous call, which returns a task_id
    # first_frame_url / last_frame_url are input-section fields that are not part of the named signature, and are passed through via extra_input
    try:
        job = client.videos.jobs.create(model="wan2.2-kf2v-flash",
                                        prompt="写实风格,一只黑色小猫好奇地看向天空,镜头从平视逐渐上升,最后俯拍它的好奇的眼神。",
                                        extra_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"},
                                        duration=5,  # Fixed at 5. Do not change
                                        prompt_extend=True,
                                        watermark=True)
        print(job)
        print("task_id: %s" % job.task_id)
    except DashScopeError as e:
        print(f"HTTP status code: {e.status_code}")
        print(f"Error code: {e.code}")
        print(f"Error message: {e.message}")
        print("See the documentation: https://www.alibabacloud.com/help/model-studio/developer-reference/error-code")
        return

    # Wait for the asynchronous task to complete (exponential backoff polling, default total budget of 24 hours)
    try:
        done = client.videos.jobs.wait(job)
        print(done)
        print(done.video_url)
    except DashScopeError as e:
        print(f"HTTP status code: {e.status_code}")
        print(f"Error code: {e.code}")
        print(f"Error message: {e.message}")
        print("See the documentation: https://www.alibabacloud.com/help/model-studio/developer-reference/error-code")

if __name__ == '__main__':
    sample_async_call_kf2v()

How to input images

  • Image count: one first frame image and one last frame image.
  • Input methods: image URL, local file path.

Method 1: Image URL (HTTP API, SDK) Recommended

Different programming languages (Python/Java) have slightly different requirements for file paths. Follow the rules below strictly.Python SDK: supports absolute paths and relative paths. The file path rules are as follows:

Operating system

File path to pass in

Example (absolute path)

Example (relative path)

Linux / macOS

file:/{absolute or relative path of the file}

file:///home/images/test.png

file://./images/test.png

Windows

file://D:/images/test.png

file://./images/test.png

Java SDK: only absolute paths are supported. The file path rules are as follows:

Operating system

File path to pass in

Example (absolute path)

Linux / macOS

file:/{absolute path of the file}

file:///home/images/test.png

Windows

file://{absolute path of the file}

file:///D:/images/test.png

Sample code: multiple ways to input images

Python SDK
import os
import dashscope
from dashscope import DashScopeError

# The following configuration is for the Singapore region. Replace {WorkspaceId} with your actual workspace ID. The region value varies by region.
client = dashscope.Client(
    # If the environment variable is not configured, replace the following line with your Model Studio API key: api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    workspace="{WorkspaceId}",
    region="ap-southeast-1",
)

# ========== Image input methods (choose one) ==========
# [Method 1] Use a public image 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"

# [Method 2] Use a local file path (file:// + file path)
# Use an absolute path
# first_frame_url = "file://" + "/path/to/your/first_frame.png"  # Linux/macOS
# last_frame_url = "file://" + "C:/path/to/your/last_frame.png"  # Windows
# Or use a relative path
# first_frame_url = "file://" + "./first_frame.png"              # Use the actual path
# last_frame_url = "file://" + "./last_frame.png"                # Use the actual path

def sample_sync_call_kf2v():
    print('please wait...')
    # generate is a one-step call (submit the task and wait for completion) and returns a completed Job
    # first_frame_url / last_frame_url are input-section fields that are not part of the named signature, and are passed through via extra_input
    try:
        job = client.videos.generate(model="wan2.2-kf2v-flash",
                                     prompt="写实风格,一只黑色小猫好奇地看向天空,镜头从平视逐渐上升,最后俯拍它的好奇的眼神。",
                                     extra_input={'first_frame_url': first_frame_url,
                                                  'last_frame_url': last_frame_url},
                                     resolution="720P",
                                     prompt_extend=True)
        print(job)
        print(job.video_url)
    except DashScopeError as e:
        print(f"HTTP status code: {e.status_code}")
        print(f"Error code: {e.code}")
        print(f"Error message: {e.message}")
        print("See the documentation: https://www.alibabacloud.com/help/model-studio/developer-reference/error-code")

if __name__ == '__main__':
    sample_sync_call_kf2v()

Output videos

  • Video count: one.
  • Video specifications: Output specifications vary by model. For details, see Video specifications.
  • Video URL validity: 24 hours.
  • Video dimensions: determined by the input first frame image and the resolution you set.
    • The model tries to keep the aspect ratio of the input first frame image unchanged while scaling the total pixel count close to the target value. Due to encoding standards, the width and height of the output video must be divisible by 16, and the model automatically fine-tunes the dimensions.
    • For example: an input image of 750×1000 (aspect ratio 3:4 = 0.75) with resolution = "720P" (target total pixels of about 920,000) may produce a final output of 816 × 1104 (aspect ratio ≈ 0.739, total pixels ≈ 900,000), where both the width and height are multiples of 16.

Billing and rate limits

  • For the model free quota and billing unit prices, see Model pricing.
  • For model rate limits, see Wan series.
  • Billing details:
    • Input is not billed; output is billed. Output is billed by the seconds of video successfully generated.
    • Failed model calls or processing errors do not incur any charges and do not consume the new user free quota.
    • I2V also supports the savings plan.

API reference

I2V based on first and last frames API reference

FAQ

Q: How do I generate a video with a specific aspect ratio (such as 3:4)?

A: Currently, the API does not support directly specifying the video aspect ratio. You can only set the video resolution through the resolution parameter. The resolution parameter controls the total number of pixels in the video, rather than strictly fixing the aspect ratio. The model prioritizes preserving the original aspect ratio of the first frame image (first_frame_url) and makes minor adjustments on that basis to meet video encoding requirements (that is, both the width and height must be multiples of 16). Therefore, if you want the output to be close to a 3:4 video, we recommend uploading a first frame image with a 3:4 aspect ratio, and the model will maintain that ratio as closely as possible.
For example: with an input image of 750×1000 (aspect ratio 3:4 = 0.75) and resolution set to "720P" (target total pixels of approximately 920,000), the final output may be 816 × 1104 (aspect ratio ≈ 0.739, total pixels ≈ 900,000), with both the width and height being multiples of 16.

Q: Why does the "url error, please check url!" error occur when running SDK code?

A: Please ensure that:
  • The DashScope Python SDK version is 2.0.1 or later.
  • The DashScope Java SDK version is 2.22.6 or later.
If the version is too low, the "url error, please check url!" error may be triggered. Please refer to Upgrade SDK to update it.