Python – Making Use of Google’s Text To Speech Translation Tool

Text to speech (tts) is a difficult task to get right and there are quite a few packages that show just how hard with their terrible tinny voices, these are also usually only limited to English, which for the larger part than most people would like to believe don’t find it very useful. Nevermind, Google to the rescue, their Translation tool has had the ability to also voice translations for a while now. So wouldn’t it be awesome if we could utilize this, stable, constantly developing and ultimately free utlilty? Yes? Well luckly for you it’s possible. It’s been done before with the use of JavaScript, like here but those more used to scripting and desktop programming don’t worry, you can use it too.

Basically it works by sending a request to the Google translate servers which then, pretty much instantly, reply with an MP3 file which contains the requested words in their most sexy of robot voices.

Check this URL for an example of said voice – http://translate.google.com/translate_tts?q=check+out+my+sexy+robot+voice (if it doesn’t load in your browser, open VLC, CTRL+N and paste the URL in)

First, we want to be able to get python to stream an MP3 file off the web:

More info on this can be found here, the following code was is a stripped down version of the code found at codeboje.de.

[gist][/gist]

Now we want to make a string to send :

We’re going to use command line arguments as the string which wants to be read by the Google TTS engine, the following code will grab those arguments and concatenate them into a string. It also has to be in a format like a usual Google URL, these usually replace white space for the ‘+’ sign, that’s easy to do.

[gist][/gist]

Now let’s just pass this URL to the bit we wrote to stream an MP3 and watch it go:

Full Source Code:

(Uses command line arguments as it’s input, run like ‘python ttsString.py Hello World‘)
[gist][/gist]

That’s pretty much how easy it is to use Google’s free online TTS engine, one main thing to watch out for is the 100 character limit to the use of this service in this way along with the API call limits which apply to each IP address. There are loads of cool things this can be used for and I’d love to see what anyone comes up with, keep me posted and have fun!

1 comment to Python – Making Use of Google’s Text To Speech Translation Tool

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>