如何将谷歌翻译TTS与新的V2 API一起使用



我曾经调用谷歌翻译TTS来下载这个网址的音频文件:http://translate.google.com/translate_tts?tl=en&q=Hello+world!

但是,Google改变了工作方式,因此我无法再下载音频文件。我已经注册了谷歌翻译API V2的免费试用版,但找不到如何获取TTS音频文件。

知道吗?

您可以在没有验证码的情况下使用该链接。

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

我偶然发现了这个线程,并想参考@Alexandre Andrade来表达我的看法,主要是因为他没有提交任何代码。

我在 react 应用程序中执行此操作,但相同的过程应该适用于普通 Web 项目。

我确实在头上添加了元标记 public/index.html,

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

然后在我的组件中添加了音频标签:

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"/>
...

然后,只需将函数连接到某些生命周期方法即可。由于我在 react 钩子中编写了 react 代码,因此我在其中一个钩子中添加了函数调用,以便在加载组件时对其进行初始化。(否则这将在组件DidMount()函数中)。

希望这对任何人有所帮助!

试试这个英语链接:https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=en&q=Hello+World

中文(普通话)https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=zh-CN&q=世界+你好

文本到语音转换始终是一个"非官方"API,现在受验证码保护以防止滥用。它从未作为翻译 API 的一部分进行宣传,目前翻译 V2 API 中没有 TTS 功能,无论是付费的还是其他的。

以下组线程的更多背景已经持续了一段时间。

对于那些拼

命尝试将Google TTS作为HTML音频播放的人:让我为您节省几个小时的时间,并告诉您如何做到这一点

假设我们有这个链接:https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=en&q=I+love+coffee

如果您尝试在给定链接的情况下播放此音频并使用 <audio><iframe> ,使用第三方库或使用 Javascript 播放它......

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

。那么你很快就会发现,上述方法都没有工作,因为错误404正在被抛出

溶液

显然,播放此 TTS 通用音频的唯一可能方法是利用包装到自定义<iframe>中的<embed>标签并为链接提供唯一的版本号(这很重要,因为浏览器缓存会阻止音频由于某种原因播放)。

这是我们示例的解决方案:(假设您有一个 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','');
}

您可以简单地使用链接:

文本转语音

最新更新