使用文字提示生成影片

您可以在 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 驗證說明文件中的「Authenticate for using REST」。

    以文字生成影片

    以下範例說明如何使用文字提示生成影片:

    控制台

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

      媒體工作室

    2. 按一下「Veo」Veo。系統隨即會顯示 Veo Media Studio 頁面。

    3. 選用:在「設定」窗格中,設定下列選項:

      • 模式:從可用選項中選擇模式。

        如要進一步瞭解可用的模型,請參閱 Veo 模型

      • 結果數量:調整滑桿或輸入值。最大值取決於您選取的模型。

      • 影片長度:選取 5 秒8 秒的長度。

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

    4. 選用步驟:在「安全性」部分,選取下列其中一個「人物生成」設定:

      • 允許 (僅限成人):預設值。只能生成成人人物或臉孔。請勿生成青少年、兒童或臉部。

      • 不允許:不要生成人物或臉孔。

    5. 選用:在「Advanced options」(進階選項) 區段中,輸入「Seed」(種子) 值。 提供種子值可確保使用相同文字提示時,會得到相同輸出內容。

    6. 在「撰寫提示」方塊中,輸入描述要生成影片的文字提示。

    7. 按一下「生成」

    REST

    設定環境後,即可使用 REST 測試文字提示。下列範例會將要求傳送至發布商模型端點。

    如要進一步瞭解 Veo API,請參閱 Vertex AI 上的 Veo API

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

      使用任何要求資料之前,請先替換以下項目:

      • PROJECT_ID:您的 Google Cloud 專案 ID
      • MODEL_ID:要使用的模型 ID。可用值:
        • veo-2.0-generate-001 (GA)
        • veo-3.0-generate-preview (預先發布版)
        • veo-3.0-fast-generate-preview (預先發布版)
      • TEXT_PROMPT:用於引導影片生成的文字提示。
      • 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/MODEL_ID:predictLongRunning

      JSON 要求主體:

      {
        "instances": [
          {
            "prompt": "TEXT_PROMPT"
          }
        ],
        "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。可用值:
        • veo-2.0-generate-001 (GA)
        • 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
      這項要求會傳回作業相關資訊,包括作業是否仍在執行中或已完成。

    Python

    安裝

    pip install --upgrade google-genai

    詳情請參閱 SDK 參考說明文件

    設定環境變數,透過 Vertex AI 使用 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_VERTEXAI=True

    import time
    from google import genai
    from google.genai.types import GenerateVideosConfig
    
    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.0-generate-preview",
        prompt="a cat reading a book",
        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

    後續步驟