示例:图生视频

使用 Atlas Cloud API 从图像创建视频的完整示例

概述

本教程演示一个完整的图生视频工作流:上传源图片、从中生成视频并获取结果。

前提条件

  • 一个 Atlas Cloud 账户及 API Key
  • 一个源图片文件(JPEG、PNG 或 WebP)
  • Python 3.7+ 并安装 requests

完整 Python 示例

import requests
import time
import os

API_KEY = os.environ.get("ATLASCLOUD_API_KEY", "your-api-key")
BASE_URL = "https://api.atlascloud.ai/api/v1"

def upload_image(file_path):
    """上传本地图片并获取临时 URL。"""
    with open(file_path, "rb") as f:
        response = requests.post(
            f"{BASE_URL}/model/uploadMedia",
            headers={"Authorization": f"Bearer {API_KEY}"},
            files={"file": f}
        )
    response.raise_for_status()
    url = response.json().get("url")
    print(f"Uploaded: {url}")
    return url

def generate_video(image_url, prompt, model="kling-v2.0"):
    """从图片提交视频生成任务。"""
    response = requests.post(
        f"{BASE_URL}/model/generateVideo",
        headers={
            "Authorization": f"Bearer {API_KEY}",
            "Content-Type": "application/json"
        },
        json={
            "model": model,
            "prompt": prompt,
            "image_url": image_url
        }
    )
    response.raise_for_status()
    return response.json()["data"]["id"]

def wait_for_result(prediction_id, interval=5, timeout=300):
    """轮询获取生成结果,带超时机制。"""
    elapsed = 0
    while elapsed < timeout:
        response = requests.get(
            f"{BASE_URL}/model/prediction/{prediction_id}",
            headers={"Authorization": f"Bearer {API_KEY}"}
        )
        result = response.json()
        status = result["data"]["status"]

        if status == "completed":
            return result["data"]["outputs"][0]
        elif status == "failed":
            raise Exception(f"Failed: {result['data'].get('error')}")

        print(f"  Status: {status} ({elapsed}s)")
        time.sleep(interval)
        elapsed += interval

    raise TimeoutError("Generation timed out")

# 第 1 步:上传源图片
print("Step 1: Uploading image...")
image_url = upload_image("my_photo.jpg")

# 第 2 步:生成视频
prompt = "The person slowly turns their head and smiles, camera zooms in slightly"
print(f"Step 2: Generating video with prompt: {prompt}")
prediction_id = generate_video(image_url, prompt)
print(f"Task submitted: {prediction_id}")

# 第 3 步:等待结果
print("Step 3: Waiting for video...")
video_url = wait_for_result(prediction_id)
print(f"Video ready: {video_url}")

完整 Node.js 示例

import fs from "fs";

const API_KEY = process.env.ATLASCLOUD_API_KEY || "your-api-key";
const BASE_URL = "https://api.atlascloud.ai/api/v1";

async function uploadImage(filePath) {
  const formData = new FormData();
  formData.append("file", new Blob([fs.readFileSync(filePath)]));

  const response = await fetch(`${BASE_URL}/model/uploadMedia`, {
    method: "POST",
    headers: { Authorization: `Bearer ${API_KEY}` },
    body: formData,
  });

  if (!response.ok) throw new Error(`Upload failed: ${response.status}`);
  const { url } = await response.json();
  console.log(`Uploaded: ${url}`);
  return url;
}

async function generateVideo(imageUrl, prompt, model = "kling-v2.0") {
  const response = await fetch(`${BASE_URL}/model/generateVideo`, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ model, prompt, image_url: imageUrl }),
  });

  if (!response.ok) throw new Error(`Generate failed: ${response.status}`);
  return (await response.json()).data.id;
}

async function waitForResult(predictionId, interval = 5000, timeout = 300000) {
  const start = Date.now();
  while (Date.now() - start < timeout) {
    const response = await fetch(
      `${BASE_URL}/model/prediction/${predictionId}`,
      { headers: { Authorization: `Bearer ${API_KEY}` } }
    );
    const result = await response.json();

    if (result.data.status === "completed") return result.data.outputs[0];
    if (result.data.status === "failed") throw new Error(result.data.error);

    console.log(`  Status: ${result.data.status}`);
    await new Promise((r) => setTimeout(r, interval));
  }
  throw new Error("Timeout");
}

// 运行工作流
console.log("Step 1: Uploading image...");
const imageUrl = await uploadImage("my_photo.jpg");

console.log("Step 2: Generating video...");
const predictionId = await generateVideo(
  imageUrl,
  "The person slowly turns and smiles, gentle camera movement"
);

console.log("Step 3: Waiting for result...");
const videoUrl = await waitForResult(predictionId);
console.log(`Video ready: ${videoUrl}`);

技巧

  • 视频模型:不同模型各有优势——Kling 注重质量,Seedance 擅长运动,Vidu 追求电影感
  • 描述运动:描述期望的运动方式、镜头移动和场景变化
  • 图片质量:更高质量的源图片通常能产生更好的视频效果
  • 生成时间:视频生成通常需要 30 秒到 3 分钟,取决于模型和参数
  • 轮询间隔:视频使用 5 秒间隔(图像使用 2 秒),以减少不必要的 API 调用

下一步