本教學課程說明如何使用 Speech-to-Text 轉錄影片檔案的音軌。
音訊檔案可能來自許多不同的來源。音訊資料可能來自手機 (例如語音留言),也可能來自影片檔案中的音軌。
Speech-to-Text 可以使用數種機器學習模型的其中一種來轉錄音訊檔案,完美配合音訊的原始來源。透過指定原始音訊的來源,即可獲得更好的語音內容轉錄結果。指定來源可以讓 Speech-to-Text 透過使用類似您音訊檔案的資料訓練的機器學習模型來處理音訊檔案。
目標
- 將影片檔案的音訊轉錄要求傳送至 Speech-to-Text。
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
- Speech-to-Text
如要根據預測用量估算費用,請使用 Pricing Calculator。
事前準備
本教學課程有幾項必備條件:
- 您已在 Google Cloud 控制台中設定 Speech-to-Text 專案。
- 您已在 Google Cloud 控制台使用應用程式預設憑證設定環境。
- 您已針對所選程式設計語言建立開發環境。
- 您已針對您選擇的程式設計語言安裝 Google Cloud 用戶端程式庫。
準備音訊資料
您必須先擷取影片檔案的資料,才能從影片轉錄音訊。擷取音訊資料後,您必須將其儲存於 Cloud Storage 值區或是轉換為 base64 編碼。
擷取音訊資料
您可以使用處理音訊和影片檔案的任何檔案轉換工具,例如 FFmpeg。
使用下列程式碼片段,透過 ffmpeg
將影片檔案轉換為音訊檔案。
ffmpeg -i video-input-file audio-output-file
儲存或轉換音訊資料
您可以轉錄儲存於本機電腦或 Cloud Storage 值區中的音訊檔案。
使用下列指令,透過 Google Cloud CLI 將音訊檔案上傳至現有的 Cloud Storage bucket。
gcloud storage cp audio-output-file storage-bucket-uri
如果您使用本機檔案,而且打算從指令列使用 curl
工具傳送要求,則必須先將音訊檔案轉換成 採用 base64 編碼的資料。
請使用以下指令將音訊檔案轉換成文字檔。
base64 audio-output-file -w 0 > audio-data-text
傳送轉錄要求
請使用下列程式碼,將轉錄要求傳送至 Speech-to-Text。
本機檔案要求
通訊協定
如要瞭解完整的詳細資訊,請參閱 speech:recognize
API 端點。
如要執行同步語音辨識,請提出 POST
要求並提供適當的要求內容。以下為使用 curl
的 POST
要求示例。這個範例使用 Google Cloud CLI 產生存取權杖。如需安裝 gcloud CLI 的操作說明,請參閱快速入門導覽課程。
curl -s -H "Content-Type: application/json" \ -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \ https://speech.googleapis.com/v1/speech:recognize \ --data '{ "config": { "encoding": "LINEAR16", "sampleRateHertz": 16000, "languageCode": "en-US", "model": "video" }, "audio": { "uri": "gs://cloud-samples-tests/speech/Google_Gnome.wav" } }'
如要進一步瞭解如何設定要求內容,請參閱 RecognitionConfig
參考說明文件。
如果要求成功,伺服器會傳回 200 OK
HTTP 狀態碼與 JSON 格式的回應:
{ "results": [ { "alternatives": [ { "transcript": "OK Google stream stranger things from Netflix to my TV okay stranger things from Netflix playing on TV from the people that brought you Google home comes the next evolution of the smart home and it's just outside your window me Google know hi how can I help okay no what's the weather like outside the weather outside is sunny and 76 degrees he's right okay no turn on the hose I'm holding sure okay no I'm can I eat this lemon tree leaf yes what about this Daisy yes but I wouldn't recommend it but I could eat it okay Nomad milk to my shopping list I'm sorry that sounds like an indoor request I keep doing that sorry you do keep doing that okay no is this compost really we're all compost if you think about it pretty much everything is made up of organic matter and will return", "confidence": 0.9251011 } ] } ] }
Go
如要瞭解如何安裝及使用 Speech-to-Text 的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Speech-to-Text Go API 參考說明文件。
如要向語音轉文字服務進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Java
如要瞭解如何安裝及使用 Speech-to-Text 的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Speech-to-Text Java API 參考說明文件。
如要向語音轉文字服務進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Node.js
如要瞭解如何安裝及使用 Speech-to-Text 的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Speech-to-Text Node.js API 參考說明文件。
如要向語音轉文字服務進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Python
如要瞭解如何安裝及使用 Speech-to-Text 的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Speech-to-Text Python API 參考說明文件。
如要向語音轉文字服務進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
其他語言
C#: 請按照用戶端程式庫頁面的 C# 設定說明操作, 然後前往 .NET 適用的 Speech-to-Text 參考說明文件。
PHP: 請按照用戶端程式庫頁面的 PHP 設定說明 操作,然後前往 PHP 適用的 Speech-to-Text 參考文件。
Ruby: 請按照用戶端程式庫頁面的Ruby 設定說明操作, 然後前往 Ruby 適用的 Speech-to-Text 參考說明文件。
遠端檔案要求
Go
如要瞭解如何安裝及使用 Speech-to-Text 的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Speech-to-Text Go API 參考說明文件。
如要向語音轉文字服務進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Java
如要瞭解如何安裝及使用 Speech-to-Text 的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Speech-to-Text Java API 參考說明文件。
如要向語音轉文字服務進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Node.js
如要瞭解如何安裝及使用 Speech-to-Text 的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Speech-to-Text Node.js API 參考說明文件。
如要向語音轉文字服務進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Python
如要瞭解如何安裝及使用 Speech-to-Text 的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Speech-to-Text Python API 參考說明文件。
如要向語音轉文字服務進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
其他語言
C#: 請按照用戶端程式庫頁面的 C# 設定說明操作, 然後前往 .NET 適用的 Speech-to-Text 參考說明文件。
PHP: 請按照用戶端程式庫頁面的 PHP 設定說明 操作,然後前往 PHP 適用的 Speech-to-Text 參考文件。
Ruby: 請按照用戶端程式庫頁面的Ruby 設定說明操作, 然後前往 Ruby 適用的 Speech-to-Text 參考說明文件。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。
如要刪除專案:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
刪除執行個體
如要刪除 Compute Engine 執行個體:
- In the Google Cloud console, go to the VM instances page.
- Select the checkbox for the instance that you want to delete.
- To delete the instance, click More actions, click Delete, and then follow the instructions.
刪除預設網路的防火牆規則
刪除防火牆規則:
- In the Google Cloud console, go to the Firewall page.
- Select the checkbox for the firewall rule that you want to delete.
- To delete the firewall rule, click Delete.
後續步驟
- 瞭解如何取得音訊的時間戳記。
- 識別音訊檔案中不同的說話者。
歡迎試用
如果您未曾使用過 Google Cloud,歡迎建立帳戶,親自體驗實際使用 Speech-to-Text 的成效。新客戶可以獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
免費試用 Speech-to-Text