使用 Veo 3 生成视频

Veo 3 是 Google 最先进的模型,可根据文字提示生成高保真 8 秒 720p 视频,具有惊人的逼真效果和原生生成的音频。Veo 3 擅长各种视觉和电影风格。选择以下示例,了解如何生成包含对话、电影级真实感或创意动画的视频。

根据图片生成视频

以下代码演示了如何使用 Imagen 生成图片,然后将该图片用作视频的起始帧。

Python

import time
from google import genai

client = genai.Client()

prompt = "Panning wide shot of a calico kitten sleeping in the sunshine"

# Step 1: Generate an image with Imagen
imagen = client.models.generate_images(
    model="imagen-3.0-generate-002",
    prompt=prompt,
)

# Step 2: Generate video with Veo 2 using the image
operation = client.models.generate_videos(
    model="veo-2.0-generate-001",
    prompt=prompt,
    image=imagen.generated_images[0].image,
)

# Poll the operation status until the video is ready
while not operation.done:
    print("Waiting for video generation to complete...")
    time.sleep(10)
    operation = client.operations.get(operation)

# Download the video
video = operation.response.generated_videos[0]
client.files.download(file=video.video)
video.video.save("veo2_with_image_input.mp4")
print("Generated video saved to veo2_with_image_input.mp4")

JavaScript

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

const prompt = "Panning wide shot of a calico kitten sleeping in the sunshine";

// Step 1: Generate an image with Imagen
const imagenResponse = await ai.models.generateImages({
  model: "imagen-3.0-generate-002",
  prompt: prompt,
});

// Step 2: Generate video with Veo 2 using the image
let operation = await ai.models.generateVideos({
  model: "veo-2.0-generate-001", // Use Veo 2
  prompt: prompt,
  image: {
    imageBytes: imagenResponse.generatedImages[0].image.imageBytes,
    mimeType: "image/png",
  },
});

// Poll the operation status until the video is ready
while (!operation.done) {
  console.log("Waiting for video generation to complete...")
  await new Promise((resolve) => setTimeout(resolve, 10000));
  operation = await ai.operations.getVideosOperation({
    operation: operation,
  });
}

// Download the video
ai.files.download({
    file: operation.response.generatedVideos[0].video,
    downloadPath: "veo2_with_image_input.mp4",
});
console.log(`Generated video saved to veo2_with_image_input.mp4`);

Go

package main

import (
    "context"
    "log"
    "os"
    "time"

    "google.golang.org/genai"
)

func main() {
    ctx := context.Background()
    client, err := genai.NewClient(ctx, nil)
    if err != nil {
        log.Fatal(err)
    }

    prompt := "Panning wide shot of a calico kitten sleeping in the sunshine"

    // Step 1: Generate an image with Imagen
    imagenResponse, err := client.Models.GenerateImages(
        ctx,
        "imagen-3.0-generate-002",
        prompt,
        nil, // GenerateImagesConfig
    )
    if err != nil {
        log.Fatal(err)
    }

    // Step 2: Generate video with Veo 2 using the image
    operation, err := client.Models.GenerateVideos(
        ctx,
        "veo-2.0-generate-001",
        prompt,
        imagenResponse.GeneratedImages[0].Image, // Use generated image
        nil, // GenerateVideosConfig
    )
    if err != nil {
        log.Fatal(err)
    }

    // Poll the operation status until the video is ready
    for !operation.Done {
        log.Println("Waiting for video generation to complete...")
        time.Sleep(10 * time.Second)
        operation, _ = client.Operations.GetVideosOperation(ctx, operation, nil)
    }

    // Download the video
    video := operation.Response.GeneratedVideos[0]
    client.Files.Download(ctx, video.Video, nil)
    fname := "veo2_with_image_input.mp4"
    _ = os.WriteFile(fname, video.Video.VideoBytes, 0644)
    log.Printf("Generated video saved to %s\n", fname) 
}

视频生成参数和规范

您可以在 API 请求中设置这些参数,以控制视频生成过程。

参数 说明 Veo 3(预览版) Veo 2(稳定版)
prompt 视频的文字说明。支持音频提示。 string string
negativePrompt 描述视频中应避免的内容的文字。 string string
image 要添加动画的初始图片。 不支持 Image 对象
aspectRatio 视频的宽高比。 "16:9" "16:9""9:16"
personGeneration 控制人物的生成。 "allow_all" "allow_all""allow_adult""dont_allow"

您可以在请求中设置参数,自定义视频生成。例如,您可以指定 negativePrompt 来引导模型。

Python

import time
from google import genai
from google.genai import types

client = genai.Client()

operation = client.models.generate_videos(
    model="veo-3.0-generate-preview",
    prompt="A cinematic shot of a majestic lion in the savannah.",
    config=types.GenerateVideosConfig(negative_prompt="cartoon, drawing, low quality"),
)

# Poll the operation status until the video is ready
while not operation.done:
    print("Waiting for video generation to complete...")
    time.sleep(10)
    operation = client.operations.get(operation)

# Download the generated video
generated_video = operation.response.generated_videos[0]
client.files.download(file=generated_video.video)
generated_video.video.save("parameters_example.mp4")
print("Generated video saved to parameters_example.mp4")

JavaScript

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

let operation = await ai.models.generateVideos({
  model: "veo-3.0-generate-preview",
  prompt: "A cinematic shot of a majestic lion in the savannah.",
  config: {
    aspectRatio: "16:9",
    negativePrompt: "cartoon, drawing, low quality"
  },
});

// Poll the operation status until the video is ready
while (!operation.done) {
  console.log("Waiting for video generation to complete...")
  await new Promise((resolve) => setTimeout(resolve, 10000));
  operation = await ai.operations.getVideosOperation({
    operation: operation,
  });
}

// Download the generated video
ai.files.download({
    file: operation.response.generatedVideos[0].video,
    downloadPath: "parameters_example.mp4",
});
console.log(`Generated video saved to parameters_example.mp4`);

Go

package main

import (
    "context"
    "log"
    "os"
    "time"

    "google.golang.org/genai"
)

func main() {
    ctx := context.Background()
    client, err := genai.NewClient(ctx, nil)
    if err != nil {
        log.Fatal(err)
    }

    videoConfig := &genai.GenerateVideosConfig{
        AspectRatio: "16:9",
        NegativePrompt: "cartoon, drawing, low quality",
    }

    operation, _ := client.Models.GenerateVideos(
        ctx,
        "veo-3.0-generate-preview",
        "A cinematic shot of a majestic lion in the savannah.",
        nil,
        videoConfig,
    )

    // Poll the operation status until the video is ready
    for !operation.Done {
        log.Println("Waiting for video generation to complete...")
        time.Sleep(10 * time.Second)
        operation, _ = client.Operations.GetVideosOperation(ctx, operation, nil)
    }

    // Download the generated video
    video := operation.Response.GeneratedVideos[0]
    client.Files.Download(ctx, video.Video, nil)
    fname := "parameters_example.mp4"
    _ = os.WriteFile(fname, video.Video.VideoBytes, 0644)
    log.Printf("Generated video saved to %s\n", fname)
}

REST

# Note: This script uses jq to parse the JSON response. 
# GEMINI API Base URL
BASE_URL="https://generativelanguage.googleapis.com/v1beta"

# Send request to generate video and capture the operation name into a variable.
operation_name=$(curl -s "${BASE_URL}/models/veo-3.0-generate-preview:predictLongRunning" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -X "POST" \
  -d '{
    "instances": [{
        "prompt": "A cinematic shot of a majestic lion in the savannah."
      }
    ],
    "parameters": {
      "aspectRatio": "16:9",
      "negativePrompt": "cartoon, drawing, low quality"
    }
  }' | jq -r .name)

# Poll the operation status until the video is ready
while true; do
  # Get the full JSON status and store it in a variable.
  status_response=$(curl -s -H "x-goog-api-key: $GEMINI_API_KEY" "${BASE_URL}/${operation_name}")

  # Check the "done" field from the JSON stored in the variable.
  is_done=$(echo "${status_response}" | jq .done)

  if [ "${is_done}" = "true" ]; then
    # Extract the download URI from the final response.
    video_uri=$(echo "${status_response}" | jq -r '.response.generateVideoResponse.generatedSamples[0].video.uri')
    echo "Downloading video from: ${video_uri}"

    # Download the video using the URI and API key and follow redirects.
    curl -L -o parameters_example.mp4 -H "x-goog-api-key: $GEMINI_API_KEY" "${video_uri}"
    break
  fi
  # Wait for 5 seconds before checking again.
  sleep 10
done

处理异步操作

生成视频是一项计算密集型任务。当您发送请求时,API 会启动一个长时间运行的作业,并立即返回一个 operation 对象。然后,您必须进行轮询,直到视频准备就绪(以 done 状态为 true 表示)。

此流程的核心是一个轮询循环,用于定期检查作业的状态。

Python

import time
from google import genai
from google.genai import types

client = genai.Client()

# After starting the job, you get an operation object
operation = client.models.generate_videos(
    model="veo-3.0-generate-preview",
    prompt="A cinematic shot of a majestic lion in the savannah.",
)

# Alternatively, you can use the operation.name to get the operation
operation = types.GenerateVideosOperation(name=operation.name)

# This loop checks the job status every 10 seconds
while not operation.done:
    time.sleep(10)
    # Refresh the operation object to get the latest status
    operation = client.operations.get(operation)

# Once done, the result is in operation.response
# ... process and download your video ...

JavaScript

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

// After starting the job, you get an operation object
let operation = await ai.models.generateVideos({
  model: "veo-3.0-generate-preview",
  prompt: "A cinematic shot of a majestic lion in the savannah.",
});

// Alternatively, you can use the operation.name to get the operation
// operation = types.GenerateVideosOperation(name=operation.name)

// This loop checks the job status every 10 seconds
while (!operation.done) {
    await new Promise((resolve) => setTimeout(resolve, 1000));
    // Refresh the operation object to get the latest status
    operation = await ai.operations.getVideosOperation({ operation });
}

// Once done, the result is in operation.response
// ... process and download your video ...

模型功能

功能 说明 Veo 3(预览版) Veo 2(稳定版)
音频 原生生成包含音频的视频。 ✔️ 始终开启 ❌ 仅限静音
输入模态 用于生成的输入类型。 文生视频 文本转视频、图片转视频
解决方法 视频的输出分辨率。 720p 720p
帧速率 视频的输出帧速率。 24 帧/秒 24 帧/秒
视频时长 生成的视频的时长。 8 秒 5-8 秒
每次请求的视频数量 每次请求生成的视频数量。 1 1 或 2
状态和详细信息 型号供应情况和更多详细信息。 预览 稳定版

如需详细了解 Veo 的使用限制,请参阅模型价格速率限制页面。

Veo 提示指南

本部分包含一些示例视频,展示了您可以使用 Veo 创作的视频,并说明了如何修改提示以生成不同的结果。

安全过滤器

Veo 会在 Gemini 中应用安全过滤器,以帮助确保生成的视频和上传的照片不包含冒犯性内容。 违反我们条款和准则的提示会被屏蔽。

音频提示(Veo 3)

借助 Veo 3,您可以为音效、环境噪音和对话提供提示。该模型会捕捉这些提示的细微差别,以生成同步的音轨。

  • 对话:使用引号表示具体的对话内容。(例如:“这一定是关键,”他低声说道。)
  • 音效 (SFX):明确描述声音。(示例:轮胎发出刺耳的尖叫声,发动机发出轰鸣声。)
  • 环境噪声:描述环境的声景。(示例:背景中回荡着微弱而诡异的嗡嗡声。)

这些视频展示了如何通过提供越来越详细的提示来让 Veo 3 生成音频。

提示 生成的输出
更多细节(对话和环境)
特写镜头:两个人盯着墙上的一幅神秘图画,火把的光在闪烁。“这一定是钥匙,”他一边低声说道,一边描摹着图案。“不过,这是什么意思呢?”她困惑地问道,同时歪了歪头。潮湿的石头、精美的雕刻、隐藏的符号。背景中传来一阵微弱而诡异的嗡嗡声。
洞穴中的寻宝者。
细节较少(对话)
露营(定格动画):露营者:“我现在与大自然融为一体了!”熊:“大自然希望有一些个人空间”。
洞穴中的寻宝者。

不妨亲自尝试一下这些提示,听听音频效果! 试用 Veo 3

提示撰写的基础知识

良好的提示应具有描述性且清晰明了。如要充分利用 Veo,请先确定核心创意,然后通过添加关键字和修饰符来完善创意,并在提示中加入视频专用术语。

您的提示应包含以下元素:

  • 正文:您希望在视频中呈现的对象、人物、动物或场景,例如城市景观自然车辆小狗
  • 动作:正文正在执行的动作(例如,走路跑步转头)。
  • 风格:使用特定的电影风格关键字(例如科幻恐怖片黑色电影)或动画风格(例如卡通)来指定创意方向。
  • 相机定位和运动:[可选] 使用航拍平视俯拍轨道拍摄仰拍等术语控制相机的位置和运动。
  • 构图:[可选] 镜头取景方式,例如广角镜头特写镜头单人镜头双人镜头
  • 对焦和镜头效果:[可选] 使用浅景深深景深柔焦微距镜头广角镜头等术语来实现特定的视觉效果。
  • 氛围:[可选] 颜色和光线对场景的贡献,例如蓝色调夜间暖色调

有关撰写提示的更多技巧

  • 使用描述性语言:使用形容词和副词为 Veo 描绘清晰的画面。
  • 增强面部细节:指定面部细节作为照片的焦点,例如在提示中使用“portrait”(人像)一词。

如需了解更全面的提示策略,请参阅提示设计简介

提示和输出示例

本部分提供了多个提示,重点介绍了描述性细节如何提升每个视频的最终效果。

冰柱

本视频演示了如何在提示中使用提示撰写基础知识中的元素。

提示 生成的输出
特写镜头(构图):冰冻岩壁(背景)上融化的冰柱(正文),冷蓝色调(氛围),放大(镜头运动),保持水滴(动作)的特写细节。 滴水的冰柱,蓝色背景。

一位男士正在打电话

这些视频演示了如何通过添加越来越具体的细节来修改提示,让 Veo 按照您的喜好优化输出内容。

提示 生成的输出
细节较少
镜头从远处推近,展现一位身着绿色风衣、神情绝望的男人。他正在用一部转盘式壁挂电话拨打电话,电话上亮着绿色霓虹灯。看起来像电影场景。
男士正在打电话。
更多细节
一个电影特写镜头跟随着一位身着破旧绿色风衣、神情绝望的男人,他正在拨打安装在粗糙砖墙上的转盘式电话,周围笼罩着绿色霓虹灯的诡异光芒。镜头缓缓推进,显示出他下巴的紧张感,以及他努力拨打电话时脸上刻着的绝望。浅景深效果将焦点对准了他紧锁的眉头和黑色的拨号电话,模糊的背景则呈现出一片霓虹色彩和模糊的阴影,营造出一种紧迫感和孤立感。
一位男士正在打电话

雪豹

提示 生成的输出
简单提示
一只毛发像雪豹一样可爱的生物在冬季森林中行走,3D 卡通风格渲染。
雪豹无精打采。
详细提示
制作一个简短的 3D 动画场景,采用欢快的卡通风格。一只可爱的生物,有着雪豹般的皮毛、富有表现力的大眼睛和圆润友好的身形,在奇幻的冬季森林中欢快地跳跃。场景中应有圆润的雪树、缓缓飘落的雪花,以及透过树枝的温暖阳光。生物活泼的动作和灿烂的笑容应传达出纯粹的喜悦。采用欢快温馨的基调,搭配明亮欢快的色彩和活泼的动画。
雪豹跑得更快了。

按写作要素划分的示例

以下示例展示了如何根据每个基本要素优化提示。

主题和上下文

指定主要焦点(正文)和背景或环境(上下文)。

提示 生成的输出
一栋白色混凝土公寓楼的建筑效果图,具有流畅的有机形状,与茂盛的绿色植物和未来派元素无缝融合 占位符。
一颗卫星在太空中漂浮,背景是月球和一些星星。 漂浮在大气层中的卫星。

操作

指定受试者正在做什么(例如,走路、跑步或转头)。

提示 生成的输出
广角镜头:一位女性在海滩上行走,在日落时分面朝地平线,看起来很满足和放松。 日落美景令人惊叹。

样式

添加关键字,引导生成器朝着特定美学风格(例如超现实主义、复古、未来主义、黑色电影)生成图片。

提示 生成的输出
黑色电影风格,一男一女走在街上,神秘、电影感、黑白。 黑色电影风格绝对美轮美奂。

相机运动和构图

指定相机移动方式(第一人称视角拍摄、航拍、跟拍无人机视角)以及拍摄画面的构图方式(广角镜头、特写镜头、低角度)。

提示 生成的输出
第一人称视角镜头:一辆复古汽车在雨中行驶,加拿大夜景,电影风格。 日落美景令人惊叹。
眼睛的超近特写,眼睛中映出城市。 日落美景令人惊叹。

气氛

色彩和光线会影响情绪。您可以尝试使用“柔和的橙色暖色调”“自然光”“日出”或“冷蓝色调”等字词。

提示 生成的输出
在阳光明媚的公园里,一个女孩抱着可爱的金毛猎犬小狗的特写镜头。 一只小狗在一位年轻女孩的怀里。
电影般的特写镜头:一位悲伤的女性在雨中乘坐公交车,画面采用冷色调蓝色,营造出悲伤的氛围。 一位女士坐在公交车上,看起来很伤心。

使用参考图片生成视频

您可以使用 Veo 的图像转视频功能让图片栩栩如生。

提示 生成的输出
输入图片(由 Imagen 生成)
一只拿着巧克力棒的兔子。
兔子正在逃跑。
输出视频(由 Veo 2 生成)
兔子跑开了。
兔子正在逃跑。

否定提示

负面提示用于指定您希望视频中包含的元素。

  • ❌ 请勿使用“没有”或“不”等指令性语言。(例如 “没有墙壁”)。
  • ✅ 请描述您不想看到的内容。(例如 “墙、框架”)。
提示 生成的输出
不使用负提示
生成一段简短的风格化动画,内容是一棵巨大的孤零零的橡树,树叶在强风中剧烈摇摆... [截断]
使用文字的树。
使用负面提示
[相同提示]

负面提示:城市背景、人造结构、黑暗、暴风雨或威胁性氛围。
不含否定词的树。

宽高比

Veo 可让您指定视频的宽高比。

提示 生成的输出
宽屏 (16:9)
创作一段视频,内容为:一名男子驾驶一辆红色敞篷车在 20 世纪 70 年代的棕榈泉行驶,无人机跟拍视角,暖暖的阳光,长长的阴影。
一名男子驾驶一辆红色敞篷车在棕榈泉行驶,风格为 20 世纪 70 年代。
纵向 (9:16 - 仅限 Veo 2)
制作一段视频,突出展示茂密热带雨林中壮观的夏威夷瀑布的流畅动态。重点呈现逼真的水流、细致的树叶和自然光线,以传达宁静的氛围。捕捉湍急的水流、雾气弥漫的氛围以及透过茂密树冠的斑驳阳光。使用流畅的电影级镜头移动来展示瀑布及其周围环境。力求营造宁静而真实的氛围,让观看者仿佛置身于夏威夷热带雨林的宁静美景之中。
夏威夷雨林中壮丽的瀑布。

限制

  • 请求延迟时间:最短:11 秒;最长:6 分钟(高峰时段)。
  • 区域限制personGeneration: "allow_all"(Veo 3 中的默认设置)和“图片转视频”personGeneration(Veo 2)功能在欧盟、英国、瑞士、中东和北非地区不可用。
  • 视频保留期限:生成的视频会在服务器上存储 2 天,之后会被移除。如需保存本地副本,您必须在视频生成后的 2 天内下载视频。
  • 添加水印:Veo 创建的视频会使用 SynthID(我们的 AI 生成内容水印添加和识别工具)添加水印。
  • 安全性:生成的视频会通过安全过滤和记忆检查流程,有助于降低隐私权、版权和偏见风险。

后续步骤