Skip to main content

即梦视频生成示例

以下示例展示如何使用即梦(Jimeng)视频生成模型通过 OpenAI 兼容接口生成高质量的视频内容。 即梦视频生成分为三个步骤:
  1. 创建视频生成任务 - 提交生成请求,获得任务ID
  2. 查询任务状态 - 定期检查任务进度
  3. 下载生成的视频 - 任务完成后下载视频文件

步骤1:创建视频生成任务

curl --request POST \
  --url https://api.tokenops.ai/v1/videos \
  --header 'Authorization: Bearer <API-KEY>' \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "jimeng-video-3.0-pro",
    "prompt": "千军万马奔腾在草原上,阳光明媚,高清画质",
    "req_key": "jimeng_ti2v_v30_pro",
    "seed": -1,
    "frames": 121,
    "aspect_ratio": "16:9"
  }'

响应示例

{
  "id": "10762451179911711518",
  "object": "video", 
  "created_at": 1762776961,
  "status": "in_queue"
}

步骤2:查询任务状态

# 查询任务状态
curl -X GET "https://api.tokenops.ai/v1/videos/10762451179911711518" \
  --header 'Authorization: Bearer <API-KEY>'

状态响应示例

排队中:
{
  "id": "10762451179911711518",
  "object": "video",
  "status": "in_queue"
}
已完成:
{
  "id": "10762451179911711518", 
  "object": "video",
  "status": "completed"
}

步骤3:下载生成的视频

# 下载视频文件
curl -X GET "https://api.tokenops.ai/v1/videos/10762451179911711518/content" \
  --header 'Authorization: Bearer <API-KEY>' \
  --output video.mp4

完整流程示例

import requests
import time

API_KEY = "<API-KEY>"
BASE_URL = "https://api.tokenops.ai/v1"

def complete_jimeng_video_generation(prompt, **kwargs):
    """
    完整的即梦视频生成流程
    """
    # 步骤1: 创建任务
    print("步骤1: 创建视频生成任务...")
    create_result = create_jimeng_video(prompt, **kwargs)
    if not create_result:
        return False
    
    video_id = create_result['id'] 
    print(f"任务ID: {video_id}")
    
    # 步骤2: 等待任务完成
    print("步骤2: 等待任务完成...")
    max_wait_time = 600  # 10分钟
    start_time = time.time()
    
    while time.time() - start_time < max_wait_time:
        status_info = check_video_status(video_id)
        if not status_info:
            break
            
        status = status_info.get('status')
        print(f"当前状态: {status}")
        
        if status == 'completed':
            print("视频生成完成!")
            break
        elif status == 'failed':
            print("视频生成失败")
            return False
        
        time.sleep(15)  # 每15秒检查一次
    else:
        print("等待超时")
        return False
    
    # 步骤3: 下载视频
    print("步骤3: 下载视频...")
    output_path = f"jimeng_video_{video_id}.mp4"
    if download_video(video_id, output_path):
        print(f"视频生成完成: {output_path}")
        return True
    
    return False

# 使用示例
if __name__ == "__main__":
    success = complete_jimeng_video_generation(
        prompt="千军万马奔腾在草原上,阳光明媚,高清画质",
        req_key="jimeng_ti2v_v30_pro",
        seed=-1,
        frames=121,
        aspect_ratio="16:9"
    )
    
    if success:
        print("✅ 视频生成成功!")
    else:
        print("❌ 视频生成失败!")

支持的参数

  • model: 使用的模型名称 (jimeng-video-3.0-pro、 jimeng-video-3.0-720P、 jimeng-video-3.0-1080P)
  • prompt: 视频描述文本(必需,建议400字以内)
  • req_key: 用于指定具体的即梦模型版本(必选)
    • jimeng-video-3.0-pro: jimeng_ti2v_v30_pro
    • jimeng-video-3.0-720P: jimeng_t2v_v30 (文生图), jimeng_i2v_first_v30(首帧), jimeng_i2v_first_tail_v30(首尾帧), jimeng_i2v_recamera_v30(运镜)
    • jimeng-video-3.0-1080P: jimeng_t2v_v30_1080p(文生图), jimeng_i2v_first_v30_1080(首帧), jimeng_i2v_first_tail_v30_1080(首尾帧)
  • seed: 随机种子,默认-1(随机),可指定固定值获得一致结果
  • frames: 生成的总帧数,可选值[121, 241],默认121
  • seconds: 视频时长(秒),支持5秒或10秒,系统会自动转换为对应帧数(5秒=121帧,10秒=241帧)
  • size: 视频尺寸,格式为 宽x高(如 1280x720),系统会自动转换为对应的宽高比
  • aspect_ratio: 生成视频的长宽比,默认”16:9”,支持常见比例(即梦官方参数)
  • image_urls: 参考图片URL数组
  • content: 内容数组,支持文本和图片的组合输入(平台兼容格式)

图生视频示例

除了文本到视频生成,即梦也支持从图片生成视频。以下示例展示如何使用参考图片生成视频:
curl --request POST \
  --url https://api.tokenops.ai/v1/videos \
  --header 'Authorization: Bearer <API-KEY>' \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "jimeng-video-3.0-pro",
    "prompt": "根据图片内容生成一段动态视频,保持画面风格一致",
    "req_key": "jimeng_ti2v_v30_pro",
    "image_urls": ["https://example.com/reference_image.jpg"],
    "seed": -1,
    "frames": 241,
    "aspect_ratio": "16:9"
  }'

图生视频示例场景

风景图片 + "微风轻抚树叶,云朵缓缓移动,阳光变化"
人物图片 + "人物眨眼微笑,头发轻微摆动"
建筑图片 + "灯光闪烁,窗户透出温暖光芒"
动物图片 + "小动物轻微摆动,眼睛转动"

任务状态说明

  • in_queue: 任务已提交,正在排队等待处理
  • processing: 任务正在处理中
  • completed: 任务已完成,可以下载视频
  • failed: 任务失败

即梦 OmniHuman 数字人

即梦 OmniHuman 是一个数字人视频生成服务,支持通过人像图片和音频生成数字人说话视频。完整的数字人视频生成流程分为三个步骤:
  1. 主体检测(可选)- 检测图片中的主体,返回 mask 图 URL 列表
  2. 主体识别(可选)- 识别图片中是否包含人/类人/拟人主体
  3. 视频生成 - 提交数字人视频生成任务
说明
  • 主体检测和主体识别是可选步骤,用于预检查图片是否适合生成数字人视频
  • 如果图片中有多个人物,可以通过主体检测获取 mask URL,在视频生成时指定说话的主体
  • 视频生成任务复用即梦视频生成接口,任务状态查询和视频下载与普通视频生成相同

步骤1:主体检测(可选)

主体检测是一个同步接口,用于检测图片中的主体并返回对应的 mask 图 URL 列表。最多支持检测 5 个主体。
curl --request POST \
  --url https://api.tokenops.ai/v1/omnihuman/subject/detection \
  --header 'Authorization: Bearer <API-KEY>' \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "OmniHuman_1.5",
    "req_key": "jimeng_realman_avatar_object_detection",
    "image_url": "https://example.com/portrait.jpg"
  }'

主体检测响应示例

{
  "status": 1,
  "mask_urls": [
    "https://example.com/mask1.png",
    "https://example.com/mask2.png"
  ],
  "request_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}

主体检测参数说明

请求参数:
参数类型必选说明
modelString模型名称,如 OmniHuman_1.5
req_keyString服务标识,如 jimeng_realman_avatar_object_detection
image_urlString人像图片 URL 链接
响应参数:
参数类型说明
statusInteger是否包含主体:0-不包含主体,1-包含主体
mask_urlsArray主体对应的 mask 图 URL 列表(按 mask 面积从大到小排序,URL 有效期 1 小时)
request_idString请求 ID

步骤2:主体识别(可选)

主体识别是一个异步接口,用于识别图片中是否包含人、类人、拟人等主体。
curl --request POST \
  --url https://api.tokenops.ai/v1/omnihuman/subject/recognition \
  --header 'Authorization: Bearer <API-KEY>' \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "OmniHuman_1.5",
    "req_key": "jimeng_realman_avatar_picture_create_role_omni_v15",
    "image_url": "https://example.com/portrait.jpg"
  }'

主体识别响应示例

{
  "task_id": "xxxxxxxx",
  "status": 1,
  "request_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}

主体识别参数说明

请求参数:
参数类型必选说明
modelString模型名称,如 OmniHuman_1.5
req_keyString服务标识,如 jimeng_realman_avatar_picture_create_role_omni_v15
image_urlString人像图片 URL 链接
响应参数:
参数类型说明
task_idString任务 ID
statusInteger识别结果:0-不包含人/类人/拟人主体,1-包含人/类人/拟人主体
request_idString请求 ID

步骤3:数字人视频生成

数字人视频生成复用即梦视频生成接口(/v1/videos),通过 generation_type 参数设置为 "omni_human" 来触发数字人视频生成。
curl --request POST \
  --url https://api.tokenops.ai/v1/videos \
  --header 'Authorization: Bearer <API-KEY>' \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "OmniHuman_1.5",
    "req_key": "jimeng_realman_avatar_picture_omni_v15",
    "prompt": "自然说话,面带微笑",
    "generation_type": "omni_human",
    "output_resolution": 1080,
    "content": [
      {
        "type": "image_url",
        "image_url": {
          "url": "https://example.com/portrait.jpg"
        }
      },
      {
        "type": "audio_url",
        "audio_url": {
          "url": "https://example.com/speech.mp3"
        }
      }
    ]
  }'

数字人视频生成响应示例

{
  "id": "10762451179911711518",
  "object": "video",
  "created_at": 1762776961,
  "status": "in_queue"
}

数字人视频生成参数说明

参数类型必选说明
modelString数字人模型名称,如 OmniHuman_1.5
req_keyString服务标识,如 jimeng_realman_avatar_picture_omni_v15
generation_typeString生成类型,数字人场景必须设置为 "omni_human"
contentArray内容数组,必须包含 image_urlaudio_url
promptString提示词,建议长度 ≤ 300 字符
mask_urlsArraymask 图 URL 列表,用于指定说话的主体(当图片中有多个人物时使用)
output_resolutionInteger输出视频分辨率:720 或 1080,默认 1080
pe_fast_modeBoolean是否启用快速模式,默认 false
seedInteger随机种子,默认 -1(随机)
关于 mask 图传递方式: 除了使用 mask_urls 参数,也可以在 content 数组中添加带有 role: "mask_image" 的图片元素来传递 mask 图:
{
  "type": "image_url",
  "role": "mask_image",
  "image_url": {
    "url": "https://example.com/mask.png"
  }
}

Content 数组说明

content 数组必须包含以下两种类型的元素: 图片元素(必选):
{
  "type": "image_url",
  "image_url": {
    "url": "https://example.com/portrait.jpg"
  }
}
音频元素(必选):
{
  "type": "audio_url",
  "audio_url": {
    "url": "https://example.com/speech.mp3"
  }
}
注意事项
  • 音频时长必须小于 60 秒
  • 图片建议使用清晰的人像正面照
  • 如果图片中有多个人物,建议先调用主体检测接口获取 mask URL,然后在 mask_urls 参数中指定说话的主体

查询任务状态和下载视频

数字人视频生成任务创建后,可以使用与普通视频生成相同的接口来查询任务状态和下载视频:
  • 查询状态GET /v1/videos/{video_id}
  • 下载视频GET /v1/videos/{video_id}/content
详细用法请参考上方 步骤2:查询任务状态步骤3:下载生成的视频 部分。

完整流程示例

以下是一个完整的数字人视频生成流程示例,包括主体检测、视频生成、状态轮询和下载:
import requests
import time

API_KEY = "<API-KEY>"
BASE_URL = "https://api.tokenops.ai/v1"

def complete_omnihuman_video_generation(image_url, audio_url, prompt=""):
    """
    完整的 OmniHuman 数字人视频生成流程
    """
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }

    # 步骤1: 主体检测(可选,用于获取 mask)
    print("步骤1: 主体检测...")
    detect_resp = requests.post(
        f"{BASE_URL}/omnihuman/subject/detection",
        headers=headers,
        json={"model": "OmniHuman_1.5", "req_key": "jimeng_realman_avatar_object_detection", "image_url": image_url}
    )

    mask_urls = []
    if detect_resp.status_code == 200:
        detect_result = detect_resp.json()
        print(f"主体检测完成: status={detect_result.get('status')}")
        mask_urls = detect_result.get('mask_urls', [])
        if mask_urls:
            print(f"检测到 {len(mask_urls)} 个主体")

    # 步骤2: 创建视频生成任务
    print("\n步骤2: 创建视频生成任务...")
    video_data = {
        "model": "OmniHuman_1.5",
        "req_key": "jimeng_realman_avatar_picture_omni_v15",
        "prompt": prompt,
        "generation_type": "omni_human",
        "output_resolution": 1080,
        "content": [
            {"type": "image_url", "image_url": {"url": image_url}},
            {"type": "audio_url", "audio_url": {"url": audio_url}}
        ]
    }

    # 如果有多个主体,使用第一个 mask
    if len(mask_urls) > 1:
        video_data["mask_urls"] = [mask_urls[0]]
        print(f"使用 mask: {mask_urls[0]}")

    create_resp = requests.post(
        f"{BASE_URL}/videos",
        headers=headers,
        json=video_data
    )

    if create_resp.status_code != 200:
        print(f"创建任务失败: {create_resp.text}")
        return False

    video_id = create_resp.json().get('id')
    print(f"任务ID: {video_id}")

    # 步骤3: 轮询任务状态
    print("\n步骤3: 等待任务完成...")
    max_wait_time = 600  # 10分钟
    start_time = time.time()

    while time.time() - start_time < max_wait_time:
        status_resp = requests.get(
            f"{BASE_URL}/videos/{video_id}",
            headers={"Authorization": f"Bearer {API_KEY}"}
        )

        if status_resp.status_code == 200:
            status_result = status_resp.json()
            status = status_result.get('status')
            print(f"当前状态: {status}")

            if status == 'completed':
                print("视频生成完成!")
                break
            elif status == 'failed':
                print(f"视频生成失败: {status_result.get('error')}")
                return False

        time.sleep(15)
    else:
        print("等待超时")
        return False

    # 步骤4: 下载视频
    print("\n步骤4: 下载视频...")
    download_resp = requests.get(
        f"{BASE_URL}/videos/{video_id}/content",
        headers={"Authorization": f"Bearer {API_KEY}"}
    )

    if download_resp.status_code == 200:
        output_path = f"omnihuman_video_{video_id}.mp4"
        with open(output_path, "wb") as f:
            f.write(download_resp.content)
        print(f"视频已保存: {output_path}")
        return True
    else:
        print(f"下载失败: {download_resp.status_code}")
        return False

# 使用示例
if __name__ == "__main__":
    success = complete_omnihuman_video_generation(
        image_url="https://example.com/portrait.jpg",
        audio_url="https://example.com/speech.mp3",
        prompt="自然说话,面带微笑"
    )

    if success:
        print("\n✅ 数字人视频生成成功!")
    else:
        print("\n❌ 数字人视频生成失败!")

平台兼容视频接口

除了使用传统的 prompt + image_urls 格式,我们的平台还支持使用 JSON 格式的 content 字段来传递 prompt 和参考图片,这种方式更加灵活,适合程序化调用。

Content 字段结构

content 是一个数组,每个元素包含以下字段:
字段类型说明
typeString内容类型:text(文本)、image_url(图片URL)、image_base64(Base64图片)
textString文本内容(当 type=text 时)
image_urlObject图片信息(当 type=image_url 或 image_base64 时),包含 url 字段

纯文本生成视频

curl -X POST "https://api.tokenops.ai/v1/videos" \
  -H "Authorization: Bearer <API-KEY>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "jimeng-video-3.0-pro",
    "size": "1280x720",
    "seconds": "5",
    "req_key": "jimeng_ti2v_v30_pro",
    "content": [
      {
        "type": "text",
        "text": "千军万马奔腾在草原上,阳光明媚,高清画质"
      }
    ]
  }'

使用图片 URL 作为参考帧

curl -X POST "https://api.tokenops.ai/v1/videos" \
  -H "Authorization: Bearer <API-KEY>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "jimeng-video-3.0-pro",
    "size": "1280x720",
    "seconds": "5",
    "req_key": "jimeng_ti2v_v30_pro",
    "content": [
      {
        "type": "text",
        "text": "根据这张风景图生成一段视频,展现微风轻抚、云朵缓缓移动的动态效果"
      },
      {
        "type": "image_url",
        "image_url": {
          "url": "https://example.com/landscape.jpg"
        }
      }
    ]
  }'

使用 Base64 编码的图片

curl -X POST "https://api.tokenops.ai/v1/videos" \
  -H "Authorization: Bearer <API-KEY>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "jimeng-video-3.0-pro",
    "size": "1280x720",
    "seconds": "5",
    "req_key": "jimeng_ti2v_v30_pro",
    "content": [
      {
        "type": "text",
        "text": "让画面中的小猫跳跃玩耍"
      },
      {
        "type": "image_base64",
        "image_url": {
          "url": "data:image/jpeg;base64,/9j/4AAQSkZJRg..."
        }
      }
    ]
  }'

Content 字段请求示例

{
  "model": "jimeng-video-3.0-pro",
  "size": "1280x720",
  "seconds": "5",
  "req_key": "jimeng_ti2v_v30_pro",
  "content": [
    {
      "type": "text",
      "text": "根据图片生成一段动态视频"
    },
    {
      "type": "image_url",
      "image_url": {
        "url": "https://example.com/image.jpg"
      }
    }
  ]
}
提示
  • 使用 content 字段时,系统会自动从中提取文本作为 prompt,提取图片作为参考帧
  • seconds 参数支持 “5” 或 “10”,系统会自动转换为对应的帧数(5秒=121帧,10秒=241帧)
  • size 参数会自动转换为对应的宽高比,例如 “1280x720” 会转换为 “16:9”
  • 如果同时提供了 prompt 字段和 content 中的文本,content 中的文本会优先使用