您可以使用 Veo on Vertex AI,通过提供蒙版和图片对象将对象插入视频中,然后向模型提供提示,其中包含对所需输出的说明。
如需详细了解如何编写有效的文本提示来生成视频,请参阅 Veo 提示指南。
准备工作
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Enable the Vertex AI API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Enable the Vertex AI API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
为您的环境设置身份验证。
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
Python
如需在本地开发环境中使用本页面上的 Python 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。
安装 Google Cloud CLI。
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
Google Cloud
REST
如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭证。
安装 Google Cloud CLI。
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证。
将对象插入视频中
控制台
在 Google Cloud 控制台中,依次前往 Vertex AI Studio > Generate Media 页面。
点击 Veo。
可选:在设置窗格中,配置以下设置:
模型:选择 Veo 2 预览版。
宽高比:选择 16:9 或 9:16。
结果数量:调整滑块或输入介于 1 和 4 之间的值。
视频时长:从菜单中选择所需的视频时长。
输出目录:点击浏览以创建或选择一个 Cloud Storage 存储桶来存储输出文件。
可选:在安全部分,选择以下人物生成设置之一:
允许(仅限成人):默认值。仅生成成人人物或人脸。不生成青少年或儿童人物或人脸。
不允许:不生成人物或人脸。
可选:在高级选项部分中,输入用于随机生成视频的种子值。
点击上传图片或视频。
选择要上传的本地视频,然后点击选择。
执行下列其中一项操作:
上传您自己的蒙版:
在电脑上创建蒙版。
点击上传蒙版。在显示的对话框中,选择要上传的蒙版。
定义蒙版:在修改工具栏中,使用蒙版工具(masked_transitions反转工具)指定要向其添加内容的一个或多个区域。
方框、 画笔或
在撰写提示框中,撰写描述要添加到视频中的内容的提示,然后点击
生成。
Python
安装
pip install --upgrade google-genai
如需了解详情,请参阅 SDK 参考文档。
设置环境变量以将 Gen AI SDK 与 Vertex AI 搭配使用:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_VERTEXAI=True
REST
设置您的环境后,您可以使用 REST 测试文本提示。以下示例会向发布方模型端点发送请求。
如需详细了解 Veo API,请参阅 Veo on Vertex AI API 参考文档。
使用以下命令发送视频生成请求。此请求会启动长时间运行的操作,并将输出存储到您指定的 Cloud Storage 存储桶。
在使用任何请求数据之前,请先进行以下替换:
-
PROJECT_ID
:您的 Google Cloud 项目 ID。 -
TEXT_PROMPT
:用于指导视频生成的文本提示。 -
MASK_STORAGE_URI
遮罩对象的 Cloud Storage 存储桶 URI 路径。 -
MASK_MIME_TYPE
图片遮罩的 MIME 类型。仅支持下列类型之一:image/png
image/jpeg
image/webp
-
VIDEO_INPUT_STORAGE_URI
Cloud Storage 存储桶 URI 路径视频输入对象。 -
VIDEO_MIME_TYPE
视频对象的 MIME 类型。仅支持下列类型之一:video/mov
video/mpeg
video/mp4
video/mpg
video/avi
video/wmv
video/mpegps
video/flv
-
OUTPUT_STORAGE_URI
:可选:用于存储输出视频的 Cloud Storage 存储桶。如果未提供,则回答中会返回以 Base64 字节编码的视频。例如:gs://video-bucket/output/
。 -
RESPONSE_COUNT
:您要生成的视频文件数量。接受的整数值:1-4。 -
其他可选参数
根据您的应用场景,使用以下可选变量。在
"parameters": {}
对象中添加以下部分或全部参数。"parameters": { "aspectRatio": "ASPECT_RATIO", "negativePrompt": "NEGATIVE_PROMPT", "personGeneration": "PERSON_SAFETY_SETTING", // "resolution": RESOLUTION, // Veo 3 models only "sampleCount": RESPONSE_COUNT, "seed": SEED_NUMBER }
-
ASPECT_RATIO
:(可选)一个字符串值,用于描述所生成视频的宽高比。您可使用以下值:"16:9"
表示横向"9:16"
表示纵向
默认值为
"16:9"
-
NEGATIVE_PROMPT
:(可选)一个字符串值,用于描述您想要阻止模型生成的内容。 -
PERSON_SAFETY_SETTING
:(可选)一个字符串值,用于控制生成人物或人脸的安全设置。您可使用以下值:-
"allow_adult"
:仅允许生成成人形象和人脸。 -
"disallow"
:不生成人物或人脸。
默认值为
"allow_adult"
。 -
-
RESOLUTION
:(可选)用于控制生成的视频分辨率的字符串值。仅受 Veo 3 模型支持。您可使用以下值:"720p"
"1080p"
默认值为
"720p"
。 -
RESPONSE_COUNT
:可选。一个整数值,用于描述要生成的视频数量。可接受的值范围为1
-4
。 -
SEED_NUMBER
:可选。一个 uint32 值,模型使用该值生成确定性视频。通过在请求中指定种子编号而无需更改其他参数,可引导模型生成相同的视频。可接受的值范围为0
-4294967295
。
-
HTTP 方法和网址:
POST https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/veo-2.0-generate-preview:predictLongRunning
请求 JSON 正文:
{ "instances": [ { "prompt": "TEXT_PROMPT", // The following fields can be repeated for up to three total // images. "mask": { "gcsURI": "MASK_STORAGE_URI", "mimeType": "MASK_MIME_TYPE", "maskMode": "insert" }, "video": { "gcsURI": "VIDEO_INPUT_STORAGE_URI", "mimeType": "VIDEO_MIME_TYPE" } } ], "parameters": { "storageUri": "OUTPUT_STORAGE_URI", "sampleCount": RESPONSE_COUNT, } }
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为
request.json
的文件中,然后执行以下命令:curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/veo-2.0-generate-preview:predictLongRunning"PowerShell
将请求正文保存在名为
request.json
的文件中,然后执行以下命令:$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/veo-2.0-generate-preview:predictLongRunning" | Select-Object -Expand Content{ "name": "projects/PROJECT_ID/locations/us-central1/publishers/google/models/veo-2.0-generate-001/operations/a1b07c8e-7b5a-4aba-bb34-3e1ccb8afcc8" }
-
可选:检查长时间运行的视频生成操作的状态。
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:您的 Google Cloud 项目 ID。
- MODEL_ID:要使用的模型 ID。
- OPERATION_ID:在原始生成视频请求中返回的唯一操作 ID。
HTTP 方法和网址:
POST https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/MODEL_ID:fetchPredictOperation
请求 JSON 正文:
{ "operationName": "projects/PROJECT_ID/locations/us-central1/publishers/google/models/MODEL_ID/operations/OPERATION_ID" }
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为
request.json
的文件中,然后执行以下命令:curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/MODEL_ID:fetchPredictOperation"PowerShell
将请求正文保存在名为
request.json
的文件中,然后执行以下命令:$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/MODEL_ID:fetchPredictOperation" | Select-Object -Expand Content
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-10-19。