使用 gcloud CLI 將語音轉錄為文字

本頁面說明如何使用 gcloud 工具,從指令列將語音辨識要求傳送至 Speech-to-Text。

Speech-to-Text 可讓您將 Google 語音辨識技術輕鬆整合至開發人員應用程式。您可將音訊資料傳送至 Speech-to-Text API,然後 API 會傳回該音訊檔案的文字轉錄結果。如要進一步瞭解這項服務,請參閱語音轉文字基本資訊

事前準備

您必須先完成下列操作,才能向 Speech-to-Text API 傳送要求。詳情請參閱「事前準備」頁面。

  • 在 GCP 專案中啟用 Speech-to-Text。
    1. 請確認已為 Speech-to-Text 啟用計費功能。
  • 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 Storage 值區,用於儲存音訊資料。

提出音訊轉錄要求

現在您可以使用 Speech-to-Text 將音訊檔案轉錄成文字。請使用下列程式碼範例,將 recognize 要求傳送至 Speech-to-Text API。

請開啟指令列 shell 並執行下列指令。

gcloud ml speech recognize gs://cloud-samples-tests/speech/brooklyn.flac \
    --language-code=en-US

這個指令會要求 Speech-to-Text 轉錄託管在公開存取位置的 FLAC 中包含的音訊。

如果要求成功,伺服器會以 JSON 格式傳回回應:

{
  "results": [
    {
      "alternatives": [
        {
          "confidence": 0.9840146,
          "transcript": "how old is the Brooklyn Bridge"
        }
      ]
    }
  ]
}

恭喜!您已將您的第一個要求傳送至 Speech-to-Text。

如果您收到來自 Speech-to-Text 的錯誤訊息或空白回應,請查看疑難排解錯誤緩解步驟。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取您在本頁所用資源的費用,請按照下列步驟操作。

後續步驟