Lab setup instructions and requirements
Protect your account and progress. Always use a private browser window and lab credentials to run this lab.

使用 Speech-to-Text API 將語音轉錄為文字

Lab 15 minutes universal_currency_alt 1 Credit show_chart Introductory
info This lab may incorporate AI tools to support your learning.
This content is not yet optimized for mobile devices.
For the best experience, please visit us on a desktop computer using a link sent by email.

GSP048

Google Cloud 自學實驗室

總覽

Speech-to-Text API 可將語音檔轉錄成 80 多種語言的文字檔。

在本實驗室,您將傳送音訊檔案至 Speech API 來製作轉錄稿。

課程內容

在本實驗室,您將瞭解如何執行下列工作:

  • 建立 Speech-to-Text API 要求,並使用 curl 呼叫 API
  • 使用其他語言的音訊檔案呼叫 Speech-to-Text API

設定和需求

瞭解以下事項後,再點選「Start Lab」按鈕

請詳閱以下操作說明。實驗室活動會計時,且中途無法暫停。點選「Start Lab」後就會開始計時,顯示可使用 Google Cloud 資源的時間。

您將在真正的雲端環境完成實作實驗室活動,而不是模擬或示範環境。為此,我們會提供新的暫時憑證,供您在實驗室活動期間登入及存取 Google Cloud。

為了順利完成這個實驗室,請先確認:

  • 可以使用標準的網際網路瀏覽器 (Chrome 瀏覽器為佳)。
注意事項:請使用無痕模式 (建議選項) 或私密瀏覽視窗執行此實驗室,這可以防止個人帳戶和學員帳戶之間的衝突,避免個人帳戶產生額外費用。
  • 是時候完成實驗室活動了!別忘了,活動一旦開始將無法暫停。
注意事項:務必使用實驗室專用的學員帳戶。如果使用其他 Google Cloud 帳戶,可能會產生額外費用。

如何開始研究室及登入 Google Cloud 控制台

  1. 點選「Start Lab」按鈕。如果實驗室會產生費用,畫面上會出現選擇付款方式的對話方塊。左側的「Lab Details」窗格會顯示下列項目:

    • 「Open Google Cloud console」按鈕
    • 剩餘時間
    • 必須在這個研究室中使用的臨時憑證
    • 完成這個實驗室所需的其他資訊 (如有)
  2. 點選「Open Google Cloud console」;如果使用 Chrome 瀏覽器,也能按一下滑鼠右鍵,選取「在無痕視窗中開啟連結」

    接著,實驗室會啟動相關資源,並開啟另一個分頁,顯示「登入」頁面。

    提示:您可以在不同的視窗中並排開啟分頁。

    注意:如果頁面中顯示「選擇帳戶」對話方塊,請點選「使用其他帳戶」
  3. 如有必要,請將下方的 Username 貼到「登入」對話方塊。

    {{{user_0.username | "Username"}}}

    您也可以在「Lab Details」窗格找到 Username。

  4. 點選「下一步」

  5. 複製下方的 Password,並貼到「歡迎使用」對話方塊。

    {{{user_0.password | "Password"}}}

    您也可以在「Lab Details」窗格找到 Password。

  6. 點選「下一步」

    重要事項:請務必使用實驗室提供的憑證,而非自己的 Google Cloud 帳戶憑證。 注意:如果使用自己的 Google Cloud 帳戶來進行這個實驗室,可能會產生額外費用。
  7. 按過後續的所有頁面:

    • 接受條款及細則。
    • 由於這是臨時帳戶,請勿新增救援選項或雙重驗證機制。
    • 請勿申請免費試用。

Google Cloud 控制台稍後會在這個分頁開啟。

注意:如要使用 Google Cloud 產品和服務,請點選「導覽選單」,或在「搜尋」欄位輸入服務或產品名稱。「導覽選單」圖示和搜尋欄位

工作 1:建立 API 金鑰

您將使用 curl 傳送要求至 Speech-to-Text API,因此必須產生 API 金鑰來傳遞要求網址。

  1. 建立 API 金鑰的第一步,是依序點選「導覽選單」圖示 「導覽選單」圖示 >「API 和服務」>「憑證」

  2. 點選「建立憑證」,然後選取「API 金鑰」

  3. 複製並記下剛產生的金鑰,本實驗室的後續工作會用到。

  4. 點選「關閉」

點選「Check my progress」,確認目標已達成。

建立 API 金鑰

請將 API 金鑰儲存為環境變數,就不必在每個要求插入金鑰值。

  1. 為進行後續步驟,必須使用 SSH 連線至為您佈建的 linux-instance

請依序點選「導覽選單」圖示 「導覽選單」圖示 >「Compute Engine」>「VM 執行個體」。

在「VM 執行個體」清單,找出名為 linux-instance 的 VM,名稱右側會顯示詳細資料。

  1. 點選 VM 名稱 linux-instance 右側的「SSH」

互動式殼層會隨即開啟,請在當中完成後續步驟。

  1. 在殼層 (SSH) 執行下列指令,並將 <YOUR_API_KEY> 改成先前複製的金鑰:
export API_KEY=<YOUR_API_KEY>

工作 2:建立 API 要求

注意:本實驗室使用預錄檔案,該檔案可從 Cloud Storage 取得:gs://cloud-samples-data/speech/brooklyn_bridge.flac。在將音訊檔案傳送至 Speech-to-Text API 前,您可以聆聽內容
  1. 為在 request.json 檔案建立 API 要求,請先建立 request.json 檔案:
touch request.json
  1. 使用 gcloud 或偏好的指令列編輯器 (nanovimemacs) 開啟檔案,然後使用原始範例音訊檔案的 uri 值,將下列內容新增至 request.json 檔案:
{ "config": { "encoding":"FLAC", "languageCode": "en-US" }, "audio": { "uri":"gs://cloud-samples-data/speech/brooklyn_bridge.flac" } }
  1. 視需要儲存檔案。

要求主體包含 configaudio 物件。

您可以透過 config 指示 Speech-to-Text API 如何處理要求:

  • encoding 參數說明檔案傳送至 API 時使用的音訊編碼類型,FLAC 則是 .raw 檔案的編碼類型 (請參閱 RecognitionConfig 參考資料,進一步瞭解編碼類型)。
  • 如未在要求中填入 languageCode,該項目會預設為英文。

您也可以新增其他參數至 config 物件,但 encoding 是唯一必填項目。

在本實驗室的 Cloud Storage 找出音訊檔案後,使用 audio 物件即可將檔案的 uri 值傳遞至 API。

點選「Check my progress」,確認目標已達成。

建立 Speech API 要求

現在,您可以呼叫 Speech-to-Text API 了!

工作 3:呼叫 Speech-to-Text API

  1. 使用下列 curl 指令,將要求主體及 API 金鑰環境變數傳遞至 Speech API。請在單一指令列中輸入完整的指令內容:
curl -s -X POST -H "Content-Type: application/json" --data-binary @request.json \ "https://speech.googleapis.com/v1/speech:recognize?key=${API_KEY}" > result.json

回應內容會儲存在 result.json 檔案。

  1. 使用下列指令即可查看檔案內容:
cat result.json

curl 指令傳回的內容大致如下:

{ "results": [ { "alternatives": [ { "transcript": "how old is the Brooklyn Bridge", "confidence": 0.98216057 } ], "resultEndTime": "1.770s", "languageCode": "en-us" } ], "totalBilledTime": "15s" }

transcript 值傳回 Speech API 轉錄音訊檔案的成果,confidence 值則表示 API 對正確轉錄音訊的信心程度。

您會發現上述要求呼叫了 syncrecognize 方法。Speech-to-Text API 支援同步和非同步的語音轉文字作業。

在本例中,您傳送的是談話已結束的音訊檔案,但您也能透過 syncrecognize 方法,在使用者還在說話時,即時將語音轉錄為文字。

點選「Check my progress」,確認目標已達成。

呼叫 Speech API,將英文語音轉錄為文字

工作 4:將不同語言的語音轉錄為文字

您說多種語言嗎?Speech-to-Text API 語音轉文字功能支援超過 100 種語言!

只要變更 request.jsonlanguage_code 參數,就能調整語言。您可以在語言支援指南查看支援的語言清單。

現在來試試法文音訊檔案。歡迎按這裡預先聆聽內容。

  1. 編輯 request.json,將內容調整如下:
{ "config": { "encoding":"FLAC", "languageCode": "fr" }, "audio": { "uri":"gs://cloud-samples-data/speech/corbeau_renard.flac" } }
  1. 再次執行 curl 指令來呼叫 Speech-to-Text API。

  2. 查看結果:

cat result.json

畫面上應會顯示下列回應內容:

{ "results": [ { "alternatives": [ { "transcript": "maître corbeau sur un arbre perché Tenait dans son bec un fromage maître Renard par l'odeur alléché lui tint à peu près ce langage et bonjour monsieur du corbeau", "confidence": 0.93855613 } ], "resultEndTime": "12.630s", "languageCode": "fr-fr" } ], "totalBilledTime": "15s" }

這個句子取自法國詩人尚德拉封丹所寫的著名童話。如果您有其他語言的音訊檔案,也可以試著轉錄。做法一樣:將檔案新增至 Cloud Storage,並變更要求中的 languageCode 參數。

注意:如要瞭解 Speech-to-Text API 的規定和用量限制,請參閱「配額和限制」資源 呼叫 Speech API,將法文語音轉錄為文字

恭喜!

您順利用 Speech API 將語音轉錄為文字了。您將音訊檔案的 Cloud Storage URI 傳遞至 API,並學到傳遞音訊內容的另一種做法:使用 base64 編碼字串。

後續步驟/瞭解詳情

Google Cloud 教育訓練與認證

協助您瞭解如何充分運用 Google Cloud 的技術。我們的課程會介紹專業技能和最佳做法,讓您可以快速掌握要領並持續進修。我們提供從基本到進階等級的訓練課程,並有隨選、線上和虛擬課程等選項,方便您抽空參加。認證可協助您驗證及證明自己在 Google Cloud 技術方面的技能和專業知識。

使用手冊上次更新日期:2026 年 3 月 10 日

實驗室上次測試日期:2024 年 11 月 4 日

Copyright 2026 Google LLC 保留所有權利。Google 和 Google 標誌是 Google LLC 的商標,其他公司和產品名稱則有可能是其關聯公司的商標。

Before you begin

  1. Labs create a Google Cloud project and resources for a fixed time
  2. Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
  3. On the top left of your screen, click Start lab to begin

Use private browsing

  1. Copy the provided Username and Password for the lab
  2. Click Open console in private mode

Sign in to the Console

  1. Sign in using your lab credentials. Using other credentials might cause errors or incur charges.
  2. Accept the terms, and skip the recovery resource page
  3. Don't click End lab unless you've finished the lab or want to restart it, as it will clear your work and remove the project

This content is not currently available

We will notify you via email when it becomes available

Great!

We will contact you via email if it becomes available

One lab at a time

Confirm to end all existing labs and start this one

Use private browsing to run the lab

Using an Incognito or private browser window is the best way to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.