问题描述
我正在尝试使用google的文字在我的PHP网站中进行语音演讲,以托管在实时的Cpanel服务器上
我已启用文本到语音API,在“凭据”部分中创建了API KEY,还从“创建服务帐户密钥”页面下载了凭据的json文件。
然后我从Github下载了示例文件,还使用了作曲家来构建库
现在我不知道将钥匙放在哪里。在每个地方都要求导出Shell中的密钥,但这对于1个打开的命令提示符会话有效,并且每次都必须导出。
由于我想在基于实时cpanel的托管服务器上运行此代码,因此我认为将无法导出。
代码中是否有我可以通过密钥的地方?
在stackoverflow上的this url文章中:第一个答案将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] =>无效的JSON 有效负载已收到。未知名称“”:根元素必须是一条消息。 [状态] => INVALID_ARGUMENT [详细信息] =>数组([0] =>数组( [@type] => type.googleapis.com/google.rpc.BadRequest [fieldViolations] =>数组([0] =>数组([description] =>接收到无效的JSON有效负载。未知名称“”:根元素必须是一条消息。))))))
如何解决这个问题?
解决方法
在我的工作示例“文字转语音”下方,您可以重做您的需求:
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","effectsProfileId"=> [
"medium-bluetooth-speaker-class-device"
]
],"input"=>[
//"text"=>$text
"ssml" => $text
],"voice"=>[
"languageCode"=> "en-US","name" =>"en-US-Wavenet-F"
]
];
关于选择audioEncoding-https://cloud.google.com/speech-to-text/docs/encoding