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

6

18

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?

Mexicali answered 25/1, 2016 at 20:44 Comment(0)
T
27

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

Turbidimeter answered 10/10, 2017 at 14:1 Comment(2)
sounds like it's not a native voice. What exactly do I change to tweak it to select an English native voice or tone? the current intunation sounds like a non-native English speake @serhat-SatirPudens
If You Programmatically use this URL & For Repeated Queries in short timeframe, You'll get Captcha.Datolite
D
7

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!

Dishonor answered 19/5, 2020 at 7:49 Comment(1)
2 years later and it's still very effective. Upvoted for you!Circumambient
R
5

try this link for English: https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=en&q=Hello+World

For Chinese (Puthonghua) https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=zh-CN&q=世界+你好

Reverent answered 29/12, 2018 at 14:31 Comment(2)
Doesn't work programatically, unfortunately jsfiddle.net/e7ys0dmu/1Termitarium
@Termitarium Add <meta name="referrer" content="no-referrer"> at the beginning & Try.Datolite
H
4

Text-to-speech was always an 'unofficial' API which is now captcha-protected to prevent abuse. It was never advertised as part of the Translate API, and currently there is no TTS functionality in the Translate V2 API, paid or otherwise.

There is some more background on the following groups thread which had been ongoing for some time.

Hermaphroditism answered 7/2, 2016 at 4:44 Comment(1)
Thanks Adam. Ended up using Voice RSS as it's just no longer possible to bypass Google's checks. Voice RSS' quality isn't as good as Google's, but it'll do for now. Thanks again for the pointerMexicali
M
4

Here's to those who have desperately been trying to play Google TTS as an audio in HTML: let me save you a couple of hours of time and tell you how to do it.

Let's say we have this link: https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=en&q=I+love+coffee

If you try to play this audio given the link and using <audio>, <iframe>, using third-party libraries or playing it with Javascript...

var audio = new Audio('https://translate.google.com/translate_tts...'); audio.play();

...then you'll soon find out that none of the aforementioned ways work as Error 404 is being thrown.

Solution

Apparently, the only possible way to play this TTS generic audio is to utilise <embed> tag wrapped into a custom <iframe> and giving the link a unique version number (it is important, as caching by browsers prevents the audio from playing for some reason).

Here is the solution for our example: (assuming you have an iframe#ttsiframe)

function playTTS(lang,sentence) {
    //get the iframe
    var iFrame = document.getElementById('ttsiframe');

    //remove its sandbox property
    iFrame.removeAttribute('sandbox');

    //this is your reference variable for the iframe body and head tag
    var iFrameBody;

    //get the body
    if (iFrame.contentDocument) { // FF
        iFrameBody = iFrame.contentDocument.getElementsByTagName('body')[0];
        iFrameHead = iFrame.contentDocument.getElementsByTagName('head')[0];
    }
    else if (iFrame.contentWindow) { // IE
        iFrameBody = iFrame.contentWindow.document.getElementsByTagName('body')[0];
        iFrameHead = iFrame.contentWindow.document.getElementsByTagName('head')[0];
    }
    else {
        iFrameBody = iFrame.contentDocument.body;
        iFrameHead = iFrame.contentDocument.head;
    }

    //generate link to Google Translate TTS using arguments (pay attention to random version number at the end)
    var link = 'https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=' + lang + '&q=' + sentence.replace(/ /g,'+').replace(/[.]/g,'') + '&rd=' + getRandomInt(0,50000000);

    //add embed element with our link
    iFrameBody.innerHTML = '<embed src="' + link + '" id="TTS">';

    //isolate iframe
    iFrame.setAttribute('sandbox','');
}
Moshemoshell answered 19/6, 2019 at 14:8 Comment(1)
I managed to use only the Audio html tag - the only thing required is to add: <meta name="referrer" content="no-referrer"> . Add it to the Head tag of the html page.Agriculture
T
2

you can simply use the link:

Text to Speech

Twayblade answered 22/9, 2019 at 7:46 Comment(2)
You should write link in commentJuline
Sorry for the -1, but this API is just not working anymore. Better to remove this answer. Also, I wanted to +1-ed another answser of yours to compensate but you don't have any other one XD Last, I'am half "Elmi" by my Italian mother. Any idea where this rare-in-Europe family name comes from ?Termitarium

© 2022 - 2024 — McMap. All rights reserved.