【PHP】Google Cloud Text-to-Speech(php-google-tts)を使ってみる

↓composerが使える状態で、

Google Cloud Text-to-Speech のライブラリを使ってみる。

すると、↓こちらのように、

<?php
require 'vendor/autoload.php';
$googleAPIKey = '【Google APIキー】';
$articleText = 'この文字列を読み上げます。';
$client = new GuzzleHttp\Client();
$requestData = [
'input' =>[
'text' => $articleText
],
'voice' => [
'languageCode' => 'ja-JP',
'name' => 'ja-JP-Standard-A'
],
'audioConfig' => [
'audioEncoding' => 'MP3',
'pitch' => 0.00,
'speakingRate' => 1.00
]
];
try {
$response = $client->request('POST', 'https://texttospeech.googleapis.com/v1beta1/text:synthesize?key=' . $googleAPIKey, [
'json' => $requestData
]);
} catch (Exception $e) {
die('Something went wrong: ' . $e->getMessage());
}
$fileData = json_decode($response->getBody()->getContents(), true);
file_put_contents('tts.mp3', base64_decode($fileData['audioContent']));

これで簡単にテキストの内容を音声ファイルとして保存することができます。

返信を残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA