Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Google Translate TTS with the new V2 API?

I used to call Google Translate TTS to download an audio file using this url: http://translate.google.com/translate_tts?tl=en&q=Hello+world!

However Google changed the way that works and therefore I can no longer download the audio files. I've signed up for a free trial for Google Translate API V2, but can't find how to get the TTS audio files.

Any idea?

like image 806
lolouk44 Avatar asked Jan 25 '16 20:01

lolouk44


People also ask

Can I use Google Translate API for free?

The Google Translate API is not free. Its pricing is based off monthly usage in terms of millions of characters. It costs $20 per 1 million characters for translation or language detection. Price is per character sent to the API for processing, including whitespace characters.


2 Answers

You can use that link without captcha..

https://translate.google.com/translate_tts?ie=UTF-8&tl=tr-TR&client=tw-ob&q=Batsın+bu+dünya+bitsin+bu+rüya

like image 81
Serhat Şatır Avatar answered Sep 22 '22 23:09

Serhat Şatır


I stumbled across this thread and wanted to give my take on it, with reference to @Alexandre Andrade, mainly because he didn't submit any code.

I did this in a react app, but the same procedure should works for a vanilla web project.

I did add the meta tag to my head public/index.html,

<head>
...
  <meta name="referrer" content="no-referrer">
...
</head>

Then added the audio tag in my component:

Javascript:

const playTTS = (text, lang) => {
   // Get the audio element
   const audioEl = document.getElementById('tts-audio');

   const url= `https://translate.google.com/translate_tts?ie=UTF-8&tl=${lang}&client=tw-ob&q=${text}`;

   // add the sound to the audio element
   audioEl.src = url;

   //For auto playing the sound
   audioEl.play();
};

html

...
<audio controls id="tts-audio"/>
...

Then it's just a matter of hooking the function up to some of your life cycle methods. Since I wrote my react code in react hooks, I added the function call in one of my hooks to get it initialized when the component was loaded. (this would be in the componentDidMount() function otherwise).

Hope this helps anyone out!

like image 23
Viktor Sandberg Avatar answered Sep 21 '22 23:09

Viktor Sandberg