Create an API Key

Check my progress

/ 30

Create your Speech API request

Check my progress

/ 30

Call the Speech API for English language

Check my progress

/ 20

Call the Speech API for French language

Check my progress

/ 20

This lab may incorporate AI tools to support your learning.

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 setup and access」面板會顯示下列項目:

    • 「Open Google Cloud console」按鈕
    • 此實驗室所需的臨時憑證 (使用者名稱和密碼)
    • 完成此實驗室所需的其他資訊 (如有)

    請注意,實驗室計時器位於頁面頂端附近,會顯示剩餘時間。

  2. 按一下「Open Google Cloud console」。如果使用 Chrome 瀏覽器,也可以按一下滑鼠右鍵,選取「在無痕視窗中開啟連結」

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

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

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

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

    您也可以在「Lab setup and access」面板找到「Username」。

  4. 點選「下一步」

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

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

    您也可以在「Lab setup and access」面板找到「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. 「建立 API 金鑰」側邊面板會隨即顯示,請在「可透過這個金鑰存取的 API」部分,點選下拉式選單來選取 API。

  4. 搜尋或捲動畫面並找到「Cloud Speech-to-Text API」,然後勾選核取方塊。

  5. 點選「確定」

  6. 點選「建立」

  7. 複製系統產生的 API 金鑰,然後點選「關閉」

點選「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 要求
touch request.json
  1. 使用 gcloud 或偏好的指令列編輯器 (nanovimemacs) 開啟檔案,接著將下列內容新增至 request.json 檔案,並記得填入範例原始音訊檔案的 uri 值:
{ "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,則預設為英文。

除了唯一必填的 encoding 參數外,您也可以在 config 物件新增其他參數。

audio 物件則是用來將音訊檔案的 uri 值傳送至 API;該檔案儲存在本實驗室的 Cloud Storage 中。

點選「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 年 6 月 17 日

實驗室上次測試日期:2026 年 6 月 17 日

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.