使用影片的開頭和結尾影格生成影片

如要使用 Gemini Enterprise Agent Platform 生成影片,請指定影片的第一個和最後一個影格。支援的介面包括 Google Cloud 控制台和 Agent Platform API。

下列模型支援從影片的第一個和最後一個影格生成影片:

如要進一步瞭解如何撰寫有效的文字提示來生成影片,請參閱 Veo 提示指南

在 Colab 中試用 Veo

事前準備

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  2. 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 role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Enable the Agent Platform API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  4. 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 role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  5. Enable the Agent Platform API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  6. 為環境設定驗證機制。

    選取這個頁面上您打算如何使用範例的分頁:

    控制台

    使用 Google Cloud 控制台存取 Google Cloud 服務和 API 時,無須設定驗證。

    REST

    如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。

      安裝 Google Cloud CLI。

      若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

    詳情請參閱 Google Cloud 驗證說明文件中的「使用 REST 進行驗證」。

根據初始和結束影格製作影片

以下範例說明如何指定 Veo 影片的第一個和最後一個影格:

控制台

  1. 在 Google Cloud 控制台中,依序前往「Agent Platform」 >「Media Studio」頁面。

    Media Studio

  2. 按一下「影片」

  3. 在「Task」選單中,選取「Image-to-video」

  4. 從「模式」選單中,選取顯示的選項。

  5. 提示詞輸入框中,輸入描述要生成影片的文字提示詞。

  6. 在「輸入圖片」部分中,按一下「開始」上傳第一格的圖片。

  7. 選用:按一下「結尾」,上傳最後一格的圖片。

  8. 選用:調整下列參數

    • 顯示比例:選擇 16:99:16

    • 結果數量:調整滑桿或輸入介於 14 之間的值。

    • 影片長度:從選單中選取影片長度。

    • 輸出目錄:按一下「瀏覽」,建立或選取Cloud Storage bucket,用於儲存產生的檔案。

  9. 按一下「執行」

Python

安裝

pip install --upgrade google-genai

詳情請參閱 SDK 參考文件

設定環境變數,透過 Vertex AI 使用 Google Gen AI SDK:

# 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_ENTERPRISE=True

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="a hand reaches in and places a glass of milk next to the plate of cookies",
    image=Image(
        gcs_uri="gs://cloud-samples-data/generative-ai/image/cookies.png",
        mime_type="image/png",
    ),
    config=GenerateVideosConfig(
        aspect_ratio="16:9",
        last_frame=Image(
            gcs_uri="gs://cloud-samples-data/generative-ai/image/cookies-milk.png",
            mime_type="image/png",
        ),
        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

如要進一步瞭解 Veo API,請參閱下列內容:

  1. 請使用下列指令傳送影片生成要求。這項要求會啟動長時間執行的作業,並將輸出內容儲存至您指定的 Cloud Storage bucket。

    使用任何要求資料之前,請先修改下列項目的值:

    • PROJECT_ID:代表 Google Cloud 專案 ID 的字串。
    • MODEL_ID:代表要使用的模型 ID 的字串。指定第一個或最後一個影片影格時,請使用下列其中一種方法:
      • Veo 2:veo-2.0-generate-001
      • Veo 3:veo-3.1-generate-001
      • Veo 3:veo-3.1-fast-generate-001
    • TEXT_PROMPT:用來引導影片生成的文字提示。
    • PATH_TO_FIRST_FRAME_IMAGE:代表第一個影格圖片的 Cloud Storage 路徑字串。例如: "gs://video-bucket/input/first-frame.jpg"
    • IMAGE_MIME_TYPE:代表輸入圖片 MIME 類型的字串。系統僅支援下列 MIME 類型的圖片:
      • image/jpeg
      • image/png
    • PATH_TO_LAST_FRAME_IMAGE:代表最後一格影格圖片的 Cloud Storage 路徑字串。例如:"gs://video-bucket/input/last-frame.jpg"
    • OUTPUT_STORAGE_URI:選用:代表 Cloud Storage bucket 的字串,用於儲存輸出影片。如果未提供,回應中會傳回影片位元組。例如: "gs://video-bucket/output/"
    • RESPONSE_COUNT:要生成的影片檔案數量。可接受的值範圍為 14
    • DURATION:整數,代表生成的影片檔案長度。以下是各機型可接受的值:
      • Veo 2 模型:5-8。預設為 8
      • Veo 3 模型:468。預設值為 8
    • 其他選用參數

      請視用途使用下列選用變數。在 "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"
        • "4k" (僅限 Veo 3.1 預先發布版模型)

        預設值為 "720p"

      • RESPONSE_COUNT: 選用。整數值,說明要生成的影片數量。 可接受的值範圍為 14
      • SEED_NUMBER: 選用。模型用來生成確定性影片的 uint32 值。在要求中指定種子號碼,但不變更其他參數,即可引導模型產生相同的影片。可接受的值範圍為 04294967295

    HTTP 方法和網址:

    POST https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/MODEL_ID:predictLongRunning

    JSON 要求內文:

    {
      "instances": [
        {
          "prompt": "TEXT_PROMPT",
           "image": {
             "gcsUri": "PATH_TO_FIRST_FRAME_IMAGE,
             "mimeType": "IMAGE_MIME_TYPE"
           },
           "lastFrame:" {
             "gcsUri": "PATH_TO_LAST_FRAME_IMAGE,
             "mimeType": "IMAGE_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/MODEL_ID: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/MODEL_ID:predictLongRunning" | Select-Object -Expand Content
    這項要求會傳回含有專屬作業 ID 的完整作業名稱。使用這個完整作業名稱,輪詢影片生成要求的狀態。
    {
      "name":
      "projects/PROJECT_ID/locations/us-central1/publishers/google/models/MODEL_ID/operations/a1b07c8e-7b5a-4aba-bb34-3e1ccb8afcc8"
    }
    

  2. 選用:檢查影片生成長時間執行作業的狀態。

    使用任何要求資料之前,請先修改下列項目的值:

    • 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
    這項要求會傳回作業相關資訊,包括作業是否仍在執行中或已完成。

後續步驟