Sweet Google TTS2
Posted In Perl By John Hass
Google now has a text to speech service that allows you to type in a URL and some text and it will give you an mp3!Ā It works fairly well, so I decided to use my Swiss Army knife (Perl) and write a script that will make it easy to get mp3′s
#!/usr/bin/perl use LWP; $speak = $ARGV[0]; if (length($speak) ==0 || length($speak) > 100) { print "what you speak needs to be > 0 and <100\n"; exit; }
This code will check to make sure that what we want to say follows with google standards. The program is called ./speak.pl “this is what I want to say”
my @headers1 = ( 'Host' => 'translate.google.com', 'User-Agent' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.5) Gecko/20091109 Ubuntu/9.10 (karmic) Firefox/3.5.5', 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language' => 'en-us,en;q=0.5', 'Accept-Encoding' => 'gzip,deflate', 'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Keep-Alive' => '300', 'Connection' => 'keep-alive', ); $browser = LWP::UserAgent->new;
The above is our magic headers. This allows us to use Google and the browser instantiation.
next we need to format it so Google can see it correctly
$speak =~ s/ /+/g;
This removes all spaces and makes them +’s
lastly we connect and download
$sec = time(); $resp = $browser->get("http://translate.google.com/translate_tts?q=$speak",@headers1); open(FILE,">$sec.mp3"); print FILE $resp->content; close(FILE); print "Wrote $sec.mp3\n";
What we do here get the number of seconds for the out filename and write the file.
Have fun and don’t abuse this service.
Source Files: tts.zip (156)








Great. Worked perfectly.
Nice one, just what I needed, thanks a lot!
I’ve been struggling with espeak and festival for days and still get those weird voices.
This sounds so much better and is ever faster!
If you want to just play the spoken text on your linux pc in stead of saving the mp3, you can simply do the following:
mplayer “http://translate.google.com/translate_tts?tl=en&q=Hello+World”
This will load the mp3 output from the Google service and play it to your speakers right away.
You can change the language (tl) to whatever you prefer.
If you wish to do the same as proposed script in PHP:
Boudewijn