12

I am trying to convert words to speech ..

Untill now I have tried this:

<?php
 $text = "Hello this is a test for voice api of google";

// Name of the MP3 file generated using the MD5 hash
   $file  = md5($text);

// Save the MP3 file in this folder with the .mp3 extension 
   $file = "audio/" . $file .".mp3";
   if($file) {
     echo "created";
   } else {
     echo "not created";
   }

// If the MP3 file exists, do not create a new request
   if (!file_exists($file)) {
     $mp3 = file_get_contents(
        'http://translate.google.com/translate_tts?q=' . $text);
     echo "hello";
     file_put_contents($file, $mp3);
   } else {
     echo "hii";
   }
?>

In my html file :

<audio controls="controls" autoplay="autoplay">
  <source src="<?php echo $file; ?>" type="audio/mp3" />
</audio>

I am getting created hello and an audio player in output. But no file is played and neither it is created in the folder?

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
Engineer
  • 5,911
  • 4
  • 31
  • 58

6 Answers6

11
  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.

  2. 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 CHMOD
    if (!is_dir("audio/"))
        mkdir("audio/");
    else
        if (substr(sprintf('%o', fileperms('audio/')), -4) != "0777")
            chmod("audio/", 0777);


    // If MP3 file exists do not create new request
    if (!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);
    }

?>
kube
  • 13,176
  • 9
  • 34
  • 38
  • It works for me. But maybe try to add both solutions. Add the fopen() Inside the if(!file_exists) loop. – kube Feb 07 '13 at 10:29
  • I updated the code, tell me it outputs *The file doesn't exist* ? – kube Feb 07 '13 at 10:32
  • What version of PHP are you using ? – kube Feb 07 '13 at 13:04
  • It can be that you did not create the /audio/ folder, or that you don't have the correct chmod, if you are on Unix. (I don't have this problem I am on Windows) – kube Feb 07 '13 at 13:17
  • I added some changes, not sure about the CHMOD part, I put 0777 to be sure to have all access, but I think the good one would be 0755, but have a try on it first to see if it works. – kube Feb 07 '13 at 13:37
  • Audio folder is there and that too with 777 permissions – Engineer Feb 07 '13 at 13:39
  • Under PHP5 of course ? :) I ask you this because Idk from where it could come. – kube Feb 07 '13 at 13:41
  • Oh my bad...Owner had to be www-data...Now the solution and api is working fine..Thanks a lot kube. – Engineer Feb 07 '13 at 13:42
  • Works perfectly. How i can get translation in Male voice ? – Lal Jan 23 '15 at 10:29
  • I got the mp3 file created , but its not playing the text – Rupali Pemare Mar 20 '17 at 12:50
  • I don't do PHP anymore, but I just tried to do some tts with https://github.com/zlargon/google-tts, which gives correct URL to retrieve the MP3, but it seems that it works better on the browser than on server-side. Maybe it comes from authentication or specific HTTP headers. – kube Mar 23 '17 at 17:18
6

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.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Tan Nguyen
  • 386
  • 4
  • 9
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>";
       }
HamZa
  • 14,671
  • 11
  • 54
  • 75
  • I got Wasn't able to save it ! – Engineer Feb 07 '13 at 12:26
  • 1
    why total=2 in URL params? – Hrvoje Golcic May 10 '14 at 18:11
  • 1
    @hRvoed I couldn't remember so I did some research, and according to this [resource](http://www.hung-truong.com/blog/2013/04/26/hacking-googles-text-to-speech-api/) it seems to specify the total chunks. Now I'm wondering if this answer really makes sense. – HamZa May 10 '14 at 23:12
  • 1
    I researched as well just after. It appears this service is not free and we are not encouraged to use it. Input texts longer than 100 chars are splitted into chunks, thats where total and idx arguments jumps in. I gave up on Google and made an eSpeak powered text-to-speech website. The sound quality is not impressive but it works like a charm – Hrvoje Golcic May 10 '14 at 23:44
  • How i can get translation in Male voice ? – Lal Jan 23 '15 at 10:31
2

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

Hrvoje Golcic
  • 3,446
  • 1
  • 29
  • 33
1

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 "<iframe hidden src='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>";
Arijit Aich
  • 137
  • 2
  • 12
0

Your file is not creating because you forgot to create it , use below code for creating the file.

$file = "audio/".$file.".mp3";
$ourFileHandle = fopen($file, 'w') or die("can't open file");
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100