使用 Veo 根据图片生成视频

使用 Veo(一种用于生成视频的生成式 AI 模型)根据图片创作视频。

深入探索

如需查看包含此代码示例的详细文档,请参阅以下内容:

代码示例

Java

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Java 设置说明执行操作。 如需了解详情,请参阅 Vertex AI Java API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭证。 如需了解详情,请参阅为本地开发环境设置身份验证


import com.google.genai.Client;
import com.google.genai.types.GenerateVideosConfig;
import com.google.genai.types.GenerateVideosOperation;
import com.google.genai.types.GenerateVideosResponse;
import com.google.genai.types.GeneratedVideo;
import com.google.genai.types.GetOperationConfig;
import com.google.genai.types.Image;
import com.google.genai.types.Video;
import java.util.concurrent.TimeUnit;

public class VideoGenWithImg {

  public static void main(String[] args) throws InterruptedException {
    // TODO(developer): Replace these variables before running the sample.
    String modelId = "veo-3.0-generate-preview";
    String outputGcsUri = "gs://your-bucket/your-prefix";
    generateContent(modelId, outputGcsUri);
  }

  // Generates a video with an image and a text prompt.
  public static String generateContent(String modelId, String outputGcsUri)
      throws InterruptedException {
    // Client Initialization. Once created, it can be reused for multiple requests.
    try (Client client = Client.builder().location("global").vertexAI(true).build()) {

      GenerateVideosOperation operation =
          client.models.generateVideos(
              modelId,
              "Extreme close-up of a cluster of vibrant wildflowers"
                  + " swaying gently in a sun-drenched meadow.",
              Image.builder()
                  .gcsUri("gs://cloud-samples-data/generative-ai/image/flowers.png")
                  .mimeType("image/png")
                  .build(),
              GenerateVideosConfig.builder()
                  .aspectRatio("16:9")
                  .outputGcsUri(outputGcsUri)
                  .build());

      while (!operation.done().orElse(false)) {
        TimeUnit.SECONDS.sleep(15);
        operation =
            client.operations.getVideosOperation(operation, GetOperationConfig.builder().build());
      }

      String generatedVideoUri =
          operation
              .response()
              .flatMap(GenerateVideosResponse::generatedVideos)
              .flatMap(videos -> videos.stream().findFirst())
              .flatMap(GeneratedVideo::video)
              .flatMap(Video::uri)
              .orElseThrow(
                  () ->
                      new IllegalStateException(
                          "Could not get the URI from the generated video"));

      System.out.println("Generated video URI: " + generatedVideoUri);
      // Example response:
      // Generated video URI: gs://your-bucket/your-prefix/generated-video-123.mp4
      return generatedVideoUri;
    }
  }
}

Python

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Python 设置说明执行操作。 如需了解详情,请参阅 Vertex AI Python API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭证。 如需了解详情,请参阅为本地开发环境设置身份验证

import time
from google import genai
from google.genai.types import GenerateVideosConfig, Image

client = genai.Client()

# TODO(developer): Update and un-comment below line
# output_gcs_uri = "gs://your-bucket/your-prefix"

operation = client.models.generate_videos(
    model="veo-3.1-generate-001",
    prompt="Extreme close-up of a cluster of vibrant wildflowers swaying gently in a sun-drenched meadow.",
    image=Image(
        gcs_uri="gs://cloud-samples-data/generative-ai/image/flowers.png",
        mime_type="image/png",
    ),
    config=GenerateVideosConfig(
        aspect_ratio="16:9",
        output_gcs_uri=output_gcs_uri,
    ),
)

while not operation.done:
    time.sleep(15)
    operation = client.operations.get(operation)
    print(operation)

if operation.response:
    print(operation.result.generated_videos[0].video.uri)

# Example response:
# gs://your-bucket/your-prefix

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅Google Cloud 示例浏览器