[GCP] Google Cloud Speech API
keywords: cloud, GCP
- Cloud Speech-to-text @ Google Docs
- Cloud Speech: Node.js Client @ googleapis doc
擷取 1 分鐘以上的長音檔
Transcribing long audio files @ Google Docs > Cloud Speech-to-Text
- 若要轉錄 1 分鐘以上的音檔,該音檔需要放到 GCS 上
- 因為 1 分鐘以上的音檔需要處理較長的時間,因此會分成兩個請求,第一個是請求「開始轉錄的操作」,第二個請求是「取得轉錄的結果」,這個結果可能不會馬上完成,當 done為true時才可得到轉錄的結果(透過 JavaScript sdk 可以再一次 async function 中等到結果完成後再繼續執行)。
請求開始轉錄
這個請求會請 Google Cloud 開始處理某一部音檔的轉錄,並且取得 operation name,之後便需要透過這個 operation name 來詢問處理狀況並取得轉錄結果:
// Authorization Token 的取得可以透過 GCP 的 CLI 輸入
// gCloud auth application-default print-access-token
var request = require('request');
var options = {
  method: 'POST',
  url: 'https://speech.googleapis.com/v1/speech:longrunningrecognize',
  headers: {
    Authorization: 'Bearer $(gcloud auth application-default print-access-token)',
    'Content-Type': 'application/json',
  },
  body: {
    audio: { uri: 'gs://cloud-speech-videos/long-audio.wav' },
    config: {
      encoding: 'LINEAR16',
      sampleRateHertz: 48000,
      languageCode: 'zh-TW',
      enableWordTimeOffsets: true,
    },
  },
};
request(options, function (error, response, body) {
  if (error) throw new Error(error);
  console.log(body);
});
會取得帶有 operation name 的回應,之後便需要透過這個 operation name 來詢問處理狀況並取得轉錄結果:
{
  "name": "8662420302733843496"
}
詢問轉錄狀況並取得結果
在 Request URL 中會帶入 operation name,例如:
https://speech.googleapis.com/v1/operations/<oprenation-name>