LoginSignup
0
2

More than 3 years have passed since last update.

GCPのCloud Text-to-Speech APIを利用する簡単な方法(cURLを利用)

Last updated at Posted at 2019-09-24

はじめに

GCP(Google Cloud Platform)には、Cloud Text-to-Speechというテキストから音声に変換するサービスがあります。
今回は、cURLを利用し、とりあえず難しいことを考えずにサクッと使ってみます。

事前準備

Cloud Text-to-Speech APIドキュメントの「クイックスタート: コマンドラインの使用」の『始める前に』を参考に環境の準備を進めます。
https://cloud.google.com/text-to-speech/docs/quickstart-protocol

Cloud SDKは下記よりダウンロード可能です。
https://cloud.google.com/sdk/docs/

コマンド

次のコマンドを実行します。

curl \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
--data "`cat <入力ファイル名> | sed 's/"/@@@/g' | sed "s/@@@/'/g"`" \
"https://texttospeech.googleapis.com/v1/text:synthesize" | \
jq -r '.audioContent' | \
base64 --decode  > <出力ファイル名>.mp3

入力ファイル

次のような内容のJSONファイルを指定します。(絶対パスまたは相対パス)
囲み文字はダブルクォート、シングルクォートどちらでもOK(コマンド側で差異を吸収)

※文字列にシングルクォートやダブルクォートが存在する場合の考慮はしていないので、そのような文章を処理する(sedで加工する)必要がある場合は、適宜調整が必要となります。sedで加工するのが面倒な場合は、予めテキストエディタ等でダブルクォートをシングルクォートに置換しておきます。

{
   'input':{
      'text':'Android is a mobile operating system developed by Google,
         based on the Linux kernel and designed primarily for
         touchscreen mobile devices such as smartphones and tablets.'
   },
   'voice':{
      'languageCode':'en-gb',
      'name':'en-GB-Standard-A',
      'ssmlGender':'FEMALE'
   },
   'audioConfig':{
      'audioEncoding':'MP3'
   }
}

なお、下記URLの「試してみましょう」で文章と条件を設定し、「Show JSON」をクリックすると、上記フォーマットと同様のJSONテキストを取得することができます。非常に便利です。

Cloud Text-to-Speech
https://cloud.google.com/text-to-speech/?hl=ja

image.png

以下の出力結果を、ファイルとして保存します。
image.png

出力ファイル

任意のファイル名を指定します。拡張子はmp3とします。

参考情報

Cloud Text-to-Speech API ドキュメント
https://cloud.google.com/text-to-speech/docs/?hl=ja

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2