Text-to-speech In Php With Google Translate
Solution 1:
There is a problem with the url you try to access. It is broken ! You should have tried first. The new URL, that I found on the FF console is :
http://translate.google.com/translate_tts?ie=UTF-8&q=Hello&tl=en&total=1&idx=0&textlen=5&prev=input
For the single word Hello. And you see that you have to specify the language, and the length of your text in textlen, even though it did work for all the sentences I tried without changing this var.
Another problem is that you have to urlencode() your text, or you will have a bug with accents and punctuation. So the line to download the MP3 becomes :
// Language of the sentence$lang = "fr"; $mp3 = file_get_contents( 'http://translate.google.com/translate_tts?ie=UTF-8&q='. urlencode($text) .'&tl='. $lang .'&total=1&idx=0&textlen=5&prev=input');
So the complete code looks like :
<?php$text = "Bonjour, comment allez vous ?";
// Yes French is a beautiful language.$lang = "fr";
// MP3 filename generated using MD5 hash// Added things to prevent bug if you want same sentence in two different languages$file = md5($lang."?".urlencode($text));
// Save MP3 file in folder with .mp3 extension $file = "audio/" . $file . ".mp3";
// Check folder exists, if not create it, else verify CHMODif (!is_dir("audio/"))
mkdir("audio/");
elseif (substr(sprintf('%o', fileperms('audio/')), -4) != "0777")
chmod("audio/", 0777);
// If MP3 file exists do not create new requestif (!file_exists($file))
{
// Download content$mp3 = file_get_contents(
'http://translate.google.com/translate_tts?ie=UTF-8&q='. urlencode($text) .'&tl='. $lang .'&total=1&idx=0&textlen=5&prev=input');
file_put_contents($file, $mp3);
}
?>
Solution 2:
I found it:
https://translate.google.com.vn/translate_tts?ie=UTF-8&client=tw-ob&q=ANYTHING_TEXT&tl=YOUR_LANGUAGE_CODE
Important: client=tw-ob
YOUR_LANGUAGE_CODE
can be en,us,uk,vi etc.
Solution 3:
An improved version:
// ~~~ Credits to kube ~~~$text = "Hello this is a test for voice api of google";
$text = urlencode($text);
$lang = urldecode("en");
$file = "audio/" . md5($text) .".mp3";
if (!file_exists($file) || filesize($file) == 0) {
$mp3 = file_get_contents('http://translate.google.com/translate_tts?ie=UTF-8&q='.$text.'&tl='.$lang.'&total=2&idx=0&textlen='.strlen($text).'&prev=input');
if(file_put_contents($file, $mp3)){
echo"Saved<br>";
}else{
echo"Wasn't able to save it !<br>";
}
} else {
echo"Already exist<br>";
}
Solution 4:
You cannot use this service for free.
Is there any free quota? No, the Google Translate API is only available as a paid service. Please see Pricing and Support for more details. However we do offer the Google Website Translator gadget, which will translate your website without charge.
Check translate API FAQ
More info about this unofficial way of use you can find on Techcrunch
Solution 5:
You can also use the simple code below. Just echo the code to get the result. In this code, there is no need to save a file or getting permission problems.
echo "<iframehiddensrc='http://translate.google.com/translate_tts?ie=UTF-8&q=Welcome%20back%20".$jvm['firstname']."&tl=en&total=2&idx=0&textlen=5&prev=input'></iframe>";
Post a Comment for "Text-to-speech In Php With Google Translate"