我正在尝试在我的 php 网站中使用 google 的文本转语音功能,并将其托管在实时 Cpanel 服务器上
我已启用文本转语音 API,在凭据部分创建了 API key ,还从创建服务帐户 key 页面下载了凭据的 json 文件。
然后我从 Github 下载了示例文件,并使用 Composer 构建了库
现在我不知道该把 key 放在哪里。在每个地方,它都要求在 Shell 中导出 key ,但这适用于 1 个打开的命令提示符 session ,并且每次都必须导出。
由于我想在基于 cpanel 的实时托管上运行此代码,因此我认为无法导出。
代码中是否有可以传递 key 的地方?
关于this url stackoverflow 上的文章:第一个答案将 CURL 的响应导出到 synthesize-text.txt 但我们需要 mp3 输出
另一个答案指出我们应该使用jq,但由于它是共享的hsoting服务器,我不确定我们是否可以安排jq。
有办法解决这个问题吗?
更新
引用@V.Tur的答案后尝试了以下代码
$params = [
"audioConfig"=>[
"audioEncoding"=>"MP3",
"pitch"=> "1",
"speakingRate"=> "1",
"effectsProfileId"=> [
"medium-bluetooth-speaker-class-device"
]
],
"input"=>[
"ssml"=>'<speak>The <say-as interpret-as=\"characters\">SSML</say-as>
standard <break time=\"1s\"/>is defined by the
<sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>'
],
"voice"=>[
"languageCode"=> "hi-IN",
"name" =>"hi-IN-Wavenet-B",
'ssmlGender'=>'MALE'
]
];
$data_string = json_encode($params);
$speech_api_key = "My_Key_Here";
$url = 'https://texttospeech.googleapis.com/v1/text:synthesize?fields=audioContent&key=' . $speech_api_key;
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handle, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
]
);
$response = curl_exec($handle);
$responseDecoded = json_decode($response, true);
curl_close($handle);
if($responseDecoded['audioContent']){
return $responseDecoded['audioContent'];
}
我下载了音频,但我在 ssml 中提到的暂停/中断不起作用。我尝试将数据传递给 $params,如下所示
$params = "{
'input':{
'ssml':'<speak>The <say-as interpret-as=\"characters\">SSML</say-as>
standard <break time=\"1s\"/>is defined by the
<sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>'
},
'voice':{
'languageCode':'en-us',
'name':'en-US-Standard-B',
'ssmlGender':'MALE'
},
'audioConfig':{
'audioEncoding':'MP3'
}
}";
但我收到以下错误:
Array ( [error] => Array ( [code] => 400 [message] => Invalid JSON payload received. Unknown name "": Root element must be a message. [status] => INVALID_ARGUMENT [details] => Array ( [0] => Array ( [@type] => type.googleapis.com/google.rpc.BadRequest [fieldViolations] => Array ( [0] => Array ( [description] => Invalid JSON payload received. Unknown name "": Root element must be a message. ) ) ) ) ) )
如何解决这个问题?
最佳答案
在我的文本转语音工作示例下面,您可以根据需要重做:
public static function getSound($text)
{
$text = trim($text);
if($text == '') return false;
$params = [
"audioConfig"=>[
"audioEncoding"=>"LINEAR16",
"pitch"=> "1",
"speakingRate"=> "1",
"effectsProfileId"=> [
"medium-bluetooth-speaker-class-device"
]
],
"input"=>[
"text"=>$text
],
"voice"=>[
"languageCode"=> "en-US",
"name" =>"en-US-Wavenet-F"
]
];
$data_string = json_encode($params);
$url = 'https://texttospeech.googleapis.com/v1/text:synthesize?fields=audioContent&key=' . $speech_api_key;
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handle, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
]
);
$response = curl_exec($handle);
$responseDecoded = json_decode($response, true);
curl_close($handle);
if($responseDecoded['audioContent']){
return $responseDecoded['audioContent'];
}
return false;
}
using:
public static function saveSound($text)
{
$speech_data = SpeechAPI::getSound($text);//see method upper
if($speech_data) {
$file_name = strtolower(md5(uniqid($text)) . '.mp3');
$path = FileUpload::getFolder();//just return directory path
if(file_put_contents($path.$file_name, base64_decode($speech_data))){
return $file_name;
}
}
return null;
}
对于 SSML 标准,需要更改输入参数:
$text = "<speak>The <say-as interpret-as=\"characters\">SSML</say-as>
standard <break time=\"1s\"/>is defined by the
<sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>";
$params = [
"audioConfig"=>[
"audioEncoding"=>"LINEAR16",
"pitch"=> "1",
"speakingRate"=> "1",
"effectsProfileId"=> [
"medium-bluetooth-speaker-class-device"
]
],
"input"=>[
//"text"=>$text
"ssml" => $text
],
"voice"=>[
"languageCode"=> "en-US",
"name" =>"en-US-Wavenet-F"
]
];
关于选择音频编码 - https://cloud.google.com/speech-to-text/docs/encoding
关于php - php 中的谷歌云文本转语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64124100/