跳转到主要内容
首帧

Wan — 创建任务

提交图生视频任务

POST
/services/aigc/video-generation/video-synthesis
import base64
import os
from http import HTTPStatus
from dashscope import VideoSynthesis
import mimetypes
import dashscope

dashscope.base_http_api_url = 'https://dashscope.aliyuncs.com/api/v1'


# 如果未配置环境变量,请将下一行替换为您的 API Key:api_key="sk-xxx"
api_key = os.getenv("DASHSCOPE_API_KEY")

# --- 辅助函数:用于 Base64 编码 ---
# 格式: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("Unsupported or unrecognized image format")
  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}"

"""
图片输入方式:
从以下三种方式中选择一种,

1. 使用公开 URL - 适用于可公开访问的图片
2. 使用本地文件 - 适用于本地开发和测试
3. 使用 Base64 编码 - 适用于私有图片或需要加密传输的场景
"""

# [方式 1] 使用可公开访问的图片 URL
# 示例:使用公开图片 URL
img_url = "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250925/wpimhv/rap.png"

# [方式 2] 使用本地文件(支持绝对路径和相对路径)
# 格式要求:file:// + 文件路径
# 示例(绝对路径):
# img_url = "file://" + "/path/to/your/img.png"    # Linux/macOS
# img_url = "file://" + "/C:/path/to/your/img.png"  # Windows
# 示例(相对路径):
# img_url = "file://" + "./img.png"                # 相对于当前可执行文件的路径

# [方式 3] 使用 Base64 编码的图片
# img_url = encode_file("./img.png")

# 设置音频 URL
audio_url = "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250925/ozwpvi/rap.mp3"

def sample_call_i2v():
  # 同步调用,直接返回结果
  print('请稍候...')
  rsp = VideoSynthesis.call(api_key=api_key,
                              model='wan2.6-i2v-flash',
                              prompt='A scene of urban fantasy art. A dynamic graffiti art character. A boy made of spray paint comes to life from a concrete wall. He raps an English song at high speed while striking a classic, energetic rapper pose. The scene is set under an urban railway bridge at night. The lighting comes from a single street lamp, creating a cinematic atmosphere full of high energy and amazing detail. The audio of the video consists entirely of his rap, with no other dialogue or noise.',
                              img_url=img_url,
                              audio_url=audio_url,
                              resolution="720P",
                              duration=10,
                              prompt_extend=True,
                              watermark=False,
                              negative_prompt="",
                              seed=12345)
  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))


if __name__ == '__main__':
  sample_call_i2v()
{
  "request_id": "4909100c-7b5a-9f92-bfe5-xxxxxx",
  "output": {
    "task_id": "0385dc79-5ff8-4d82-bcb6-xxxxxx",
    "task_status": "PENDING"
  }
}
根据首帧图片和文本提示词生成视频。

鉴权

string
header
必填

千问云 API Key。详见获取 API Key

Header 参数

enum<string>
必填

必须设置为 enable。HTTP 请求仅支持异步处理。若省略此请求头,将返回 "当前用户 API 不支持同步调用" 的错误。

enable

请求体

application/json
enum<string>
必填

模型名称。

wan2.6-i2v-flash,wan2.6-i2v,wan2.5-i2v-preview,wan2.2-i2v-flash,wan2.2-i2v-plus,wan2.1-i2v-turbo,wan2.1-i2v-plus
wan2.6-i2v-flash
object
必填

输入数据,包括首帧图像、提示词和可选音频。

object

视频生成参数。

响应

200-application/json
string

唯一请求标识符,用于追踪和排查问题。

4909100c-7b5a-9f92-bfe5-xxxxxx
object