延长 Veo 视频

您可以使用 Veo on Vertex AI 扩展之前使用 Veo 生成的视频。您可以使用 Google Cloud 控制台或 Vertex AI API 延长视频。

如需了解如何编写有效的文本提示来生成视频,请参阅 Veo 提示指南

准备工作

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Enable the Vertex AI API.

    Enable the API

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Enable the Vertex AI API.

    Enable the API

  6. 为您的环境设置身份验证。

    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.

    REST

    如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭证。

      After installing the Google Cloud CLI, initialize it by running the following command:

      gcloud init

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证

    延长视频

    以下示例展示了如何扩展 Veo 视频:

    控制台

    1. 在 Google Cloud 控制台中,前往 Vertex AI > Media Studio 页面。

      Media Studio

    2. 点击视频,打开视频媒体工作室页面。

    3. 设置窗格中,配置以下设置:

      • 模型:选择 Veo 2 veo-2.0-generate-001

      • 宽高比:选择 16:99:16

      • 结果数量:调整滑块或输入介于 14 之间的值。

      • 视频时长:选择时长,介于 5 秒 8 秒之间。

      • 输出目录:点击浏览以创建或选择一个 Cloud Storage 存储桶来存储输出文件。

    4. 编写提示框中,输入描述要生成的视频的文字提示。

    5. 点击 生成

    6. 将光标悬停在要延长的视频上,然后依次点击 class="material-icons">auto_fix_high AI 操作 > 延长视频

    7. 编写提示框中,输入描述要生成的视频的文字提示。

    8. 点击 生成

    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

    import time
    from google import genai
    from google.genai.types import GenerateVideosConfig, Video
    
    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-2.0-generate-001",
        prompt="a butterfly flies in and lands on the flower",
        video=Video(
            uri="gs://cloud-samples-data/generative-ai/video/flower.mp4",
        ),
        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

    REST

    设置您的环境后,您可以使用 REST 测试文本提示。以下示例会向发布方模型端点发送请求。

    如需详细了解 Veo API,请参阅 Veo on Vertex AI API

    1. 使用以下命令发送视频生成请求。此请求会启动长时间运行的操作,并将输出存储到您指定的 Cloud Storage 存储桶。

      在使用任何请求数据之前,请先进行以下替换:

      • PROJECT_ID:您的 Google Cloud 项目 ID
      • TEXT_PROMPT:用于指导视频生成的文本提示。
      • PATH_TO_VIDEO:您要扩展的 Veo 视频的 Cloud Storage 路径。
      • OUTPUT_STORAGE_URI:可选:用于存储输出视频的 Cloud Storage 存储桶。如果未提供,则回答中会返回视频字节。例如:gs://video-bucket/output/
      • RESPONSE_COUNT:您要生成的视频文件数量。 接受的整数值:1-4。
      • DURATION:您要生成的视频文件的时长。 接受的整数值为 5-8。
      • 其他可选参数

        根据您的应用场景,使用以下可选变量。在 "parameters": {} 对象中添加以下部分或全部参数。

        "parameters": {
          "aspectRatio": "ASPECT_RATIO",
          "negativePrompt": "NEGATIVE_PROMPT",
          "personGeneration": "PERSON_SAFETY_SETTING",
          "sampleCount": RESPONSE_COUNT,
          "seed": SEED_NUMBER
        }
        • ASPECT_RATIO:字符串。可选。定义所生成视频的宽高比。值:16:9(默认,横向)或 9:16(纵向)。
        • NEGATIVE_PROMPT:字符串。可选。用于描述您想要阻止模型生成的内容的文本字符串。
        • PERSON_SAFETY_SETTING:字符串。可选。用于控制是否允许人物或人脸生成的安全设置。值:
          • allow_adult(默认值):仅允许生成成年人。
          • disallow:禁止在图片中包含人物或人脸。
        • RESPONSE_COUNT:int。可选。请求的输出图片数量。值: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-001:predictLongRunning

      请求 JSON 正文:

      {
        "instances": [
          {
            "prompt": "TEXT_PROMPT",
             "video": {
               "gcsUri": "PATH_TO_VIDEO",
               "mimeType": "video/mp4"
             }
          }
        ],
        "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-001: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-001:predictLongRunning" | Select-Object -Expand Content
      此请求会返回包含唯一操作 ID 的完整操作名称。使用此完整操作名称轮询视频生成请求的状态。
      {
        "name": "projects/PROJECT_ID/locations/us-central1/publishers/google/models/veo-2.0-generate-001/operations/a1b07c8e-7b5a-4aba-bb34-3e1ccb8afcc8"
      }
      

    2. 可选:检查视频生成长时间运行操作的状态。

      在使用任何请求数据之前,请先进行以下替换:

      • PROJECT_ID:您的 Google Cloud 项目 ID
      • MODEL_ID:要使用的模型 ID。可用的值:
        • veo-2.0-generate-001(正式版)
        • veo-3.0-generate-preview(预览版)
      • 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
      此请求会返回有关操作的信息,包括操作是否仍在运行或已完成。

    后续步骤