LoginSignup
0
1

More than 1 year has passed since last update.

[PowerShell] Google Cloud Text-to-Speechでテキストから音声ファイルをつくる

Last updated at Posted at 2020-12-11

やりたいこと

テキストから自然な音声を生成したい。無償のソフトなりwebサービスなどがあるが不自然なものばかり。
そのなかでも、GoogleのText to Speechは最も自然に発音してくれた。
GoogleのText to Speechを使って、wavファイルを生成する。PowerShellを使ってやってみる。

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

Google Cloud Platformの設定

Googleのチュートリアルに従えばほぼできる。
https://cloud.google.com/text-to-speech/docs/quickstart-protocol?hl=ja

  • Google Cloud Platformの利用登録
  • Cloud プロジェクトの作成
  • Cloud Text-to-Speech API を有効。
  • 認証の設定 (ここで、.jsonファイルが生成されるのであとで使う)
  • Google cloud SDKのインストール

PowerShellのスクリプト

ほぼこれを使用させてもらった。
https://www.mitabou.com/2020/05/ai.html
(リンク切れ)

そのままでは変換できなかったのと、wavファイルがほしかったので、2点スクリプトは変更している。

  • Googleのチュートリアルに書かれているように、認証設定のところで生成された.jsonのパスを、スクリプトに入れておく必要がある。
$env:GOOGLE_APPLICATION_CREDENTIALS = "C:\xxxxx\xxxxxx\xxxxxx.json"
  • もとのスクリプトは、mp3に変換するものだった。wavにしたいときは、mp3をwavに書き換えるだけでよい。
certutil -decode -f synthesize-output-base64.txt synthesized-audio.wav
  • 食わせるjsonファイル(request.json)は、同じフォルダに置いておく。Cloud Text-to-Speechのサイトで、喋らせたい言葉を入力しし、show jsonを押して、表示されたRequest Bodyのところをそのままコピペする。

image.png

  • request.jsonは、UTF-8にして保存しないとだめ。(保存するときにUTF-8を選択)

スクリプト

$env:GOOGLE_APPLICATION_CREDENTIALS = "C:\xxxxx\xxxxx\xxxx.json"

$cred = gcloud auth application-default print-access-token

$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
  -Method POST `
  -Headers $headers `
  -ContentType: "application/json; charset=utf-8" `
  -InFile request.json `
  -Uri "https://texttospeech.googleapis.com/v1/text:synthesize" | Select-Object -Expand Content > lf_synthesize-output-base64.txt
[string]::Join("`r`n",(Get-Content .\lf_synthesize-output-base64.txt)) | Set-Content .\crlf_synthesize-output-base64.txt
findstr /r "audioContent" crlf_synthesize-output-base64.txt > find_lf_synthesize-output-base64.txt
$audioline = [string]::Join("`r`n",(Get-Content .\find_lf_synthesize-output-base64.txt))
$wqcontent = $audioline.split(":")
$noaudioline = $wqcontent[1]
$wqcontent = $noaudioline.trim()
$content = $wqcontent.trim('"')
$content > .\lfcontent_synthesize-output-base64.txt
[string]::Join("`r`n",(Get-Content .\lfcontent_synthesize-output-base64.txt)) | Set-Content .\synthesize-output-base64.txt
certutil -decode -f synthesize-output-base64.txt synthesized-audio.wav

参考

  • Googleのチュートリアルより細かくかかれている。

https://blog.apar.jp/web/9893/

・Google Cloud SDK のインストールについて

https://cloud.google.com/sdk/docs/install?hl=JA

0
1
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
1