使用Google文字转语音API保存语音音频 [英] Using Google Text-To-Speech API to save speech audio
问题描述
我尝试实施此问题中讨论的方法写一个php函数,为给定的字符串下载一个音频文件,但我似乎无法绕过google的滥用保护。结果是零星的,有时我得到一个音频文件,其他时候它是一个空的2KB mp3由于我们的系统检测到从您的计算机网络异常流量的响应。这里是我到目前为止(注意$文件在我的代码中有一个位置,但为了这个目的,我已省略它):
I am trying to implement methods discussed in this question to write a php function that downloads an audio file for a given string, but I can't seem to get around google's abuse protection. Results are sporadic, sometimes I get an audio file and other times it's an empty 2KB mp3 due to a response with "Our systems have detected unusual traffic from your computer network". Here is what I've got so far ( note the $file has a location in my code but for the purposes of this I've omitted it ) :
function downloadMP3( $url, $file ){
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_REFERER, 'http://translate.google.com/' );
curl_setopt( $curl, CURLOPT_USERAGENT, 'stagefright/1.2 (Linux;Android 5.0)' );
$output = curl_exec( $curl );
curl_close( $curl );
if( $output === false ) {
return false;
}
$fp = fopen( $file, 'wb' );
fwrite( $fp, $output );
fclose( $fp );
return true;
}
$word = "Test";
$file = md5( $word ) . '.mp3';
if ( !file_exists( $file ) ) {
$url = 'http://translate.google.com/translate_tts?q=' . $word . '&tl=en&client=t';
downloadMP3( $url, $file );
}
推荐答案
尝试其他服务发现一个比Google翻译更好的工具; Google文字转语音API
Try another service, I just found one that works even better than Google Translate; Google Text-To-Speech API
这篇关于使用Google文字转语音API保存语音音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!