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"
}
}鉴权
千问云 API Key。详见获取 API Key。
Header 参数
必须设置为 enable。HTTP 请求仅支持异步处理。若省略此请求头,将返回 "当前用户 API 不支持同步调用" 的错误。
请求体
application/json模型名称。
输入数据,包括首帧图像、提示词和可选音频。
显示子属性
显示子属性
首帧图像的 URL 或 Base64 字符串。
图像约束:
- 格式:JPEG、JPG、PNG(不支持透明通道)、BMP、WEBP。
- 分辨率:宽高均需在 360 至 2,000 像素之间。
- 文件大小:不超过 10 MB。
支持的输入格式:
- 公开 URL(支持 HTTP/HTTPS)。
- Base64 编码图像:
data:{MIME_type};base64,{base64_data}。
用于描述生成视频内容和视觉特征的文字提示词。支持中英文。不同模型的长度限制:
- wan2.6 和 wan2.5 模型:最多 1,500 个字符。
- wan2.2 和 wan2.1 模型:最多 800 个字符。
超出限制的文本将自动截断。提示词撰写技巧请参见图生视频提示词指南。
负向提示词,描述不希望出现在视频中的内容。支持中英文,最多 500 个字符,超出部分自动截断。
音频文件的 URL。模型在生成视频时将与该音频同步。仅 wan2.6 和 wan2.5 模型支持。
音频约束:
- 格式:wav、mp3。
- 时长:3 至 30 秒。
- 文件大小:不超过 15 MB。
- 若音频时长超过视频
duration,将自动截断;若短于视频时长,剩余部分无声。
视频生成参数。
显示子属性
显示子属性
生成视频的分辨率规格。模型会将输出缩放至相近的总像素数,宽高比与输入图像尽量保持一致。分辨率直接影响费用(1080P > 720P > 480P)。
各模型默认值及可选项:
- wan2.6-i2v-flash:720P、1080P(默认:1080P)
- wan2.6-i2v:720P、1080P(默认:1080P)
- wan2.5-i2v-preview:480P、720P、1080P(默认:1080P)
- wan2.2-i2v-flash:480P、720P、1080P(默认:720P)
- wan2.2-i2v-plus:480P、1080P(默认:1080P)
- wan2.1-i2v-turbo:480P、720P(默认:720P)
- wan2.1-i2v-plus:720P(固定)
生成视频的时长(秒)。时长越长费用越高(按秒计费)。
各模型有效值:
- wan2.6-i2v-flash:2–15 的整数(默认:5)
- wan2.6-i2v:2–15 的整数(默认:5)
- wan2.5-i2v-preview:5、10(默认:5)
- wan2.2-i2v-flash:固定为 5(不可配置)
- wan2.2-i2v-plus:固定为 5(不可配置)
- wan2.1-i2v-turbo:3、4、5(默认:5)
- wan2.1-i2v-plus:固定为 5(不可配置)
是否启用提示词改写。启用后,LLM 将对输入提示词进行改写,可提升短提示词的生成质量,但会增加处理时间。
视频采用单一连续镜头还是多镜头切换。仅 wan2.6 模型支持,且仅在 prompt_extend 为 true 时生效。指定后将覆盖提示词中与镜头相关的描述。
是否生成有声视频。仅 wan2.6-i2v-flash 支持。优先级:audio > audio_url。若 audio=false,即使提供了 audio_url,输出视频也为无声。音频设置会影响定价。
是否在视频右下角添加 "AI 生成" 水印。
随机数种子,用于控制生成结果的可复现性。取值范围:[0, 2147483647]。若不设置,则随机使用种子。相同种子不保证生成完全相同的结果。

