[英]Getting pronunciation of a word using Google Translate API
I am trying to save the pronunciation of a French word into a .wav or .mp3 file. 我试图将法语单词的发音保存为.wav或.mp3文件。
I was wondering if there was anywhere on the Google Translate API (since it has a pronunciation functionality) that allows me to achieve this objective. 我想知道Google Translate API上是否有任何地方(因为它具有发音功能),这使我能够实现这一目标。 Other libraries would work too.
其他图书馆也可以。
Similar functionality is provided by the Speech Synthesis API (under development). Speech Synthesis API (正在开发中)提供了类似的功能。 Third-party libraries are already there, such as ResponsiveVoice.JS .
第三方库已经存在,例如ResponsiveVoice.JS 。
Since this question was asked, it's gotten much harder to "scrape" MP3s from Google Translate, but Google has (finally) set up a TTS API . 自从提出这个问题以来,从谷歌翻译中“刮掉”MP3变得更加困难,但谷歌(终于)设置了一个TTS API 。 Interestingly it is billed in input characters, with the first 1 or 4 million input characters per month being free (depending on whether you use WaveNet or old school voices)
有趣的是,它是用输入字符计费的,每月前1或4百万个输入字符是免费的(取决于你使用的是WaveNet还是旧学校的声音)
Nowadays to do this using gcloud
on the command line (versus building this into an app) you would do roughly as follows (I'm paraphrasing the TTS quick start ). 现在要在命令行上使用
gcloud
(而不是将其构建到应用程序中),您可以大致如下(我正在解释TTS快速启动 )。 You need base64
, curl
, gcloud
, and jq
for this walkthrough. 本演练需要
base64
, curl
, gcloud
和jq
。
gcloud projects create example-throwaway-tts
gcloud projects create example-throwaway-tts
Create
Create
export GOOGLE_APPLICATION_CREDENTIALS="~/Downloads/service-account-file.json"
export GOOGLE_APPLICATION_CREDENTIALS="~/Downloads/service-account-file.json"
gcloud
to use that new project: gcloud config set project example-throwaway-tts
gcloud
使用这个新项目: gcloud config set project example-throwaway-tts
TTS_ACCESS_TOKEN=gcloud auth application-default print-access-token
TTS_ACCESS_TOKEN=gcloud auth application-default print-access-token
request.json
request.json
Lastly, run the following 最后,运行以下命令
curl \\ -H "Authorization: Bearer "$TTS_ACCESS_TOKEN \\ -H "Content-Type: application/json; charset=utf-8" \\ --data-raw @request.json \\ "https://texttospeech.googleapis.com/v1/text:synthesize" \\ | jq '.audioContent' \\ | base64 --decode > very_simple_example.mp3
What this does is to 这是做什么的
jq
can extract the payload) jq
可以提取有效负载) request.json
as the data to send using curl
's --data-raw
flag request.json
作为要使用curl
的--data-raw
标志发送的数据 audioContent
from the response audioContent
的值 base64
decode that content base64
解码该内容 Contents of request.json
follow. request.json
内容如下。 You can see where to insert your desired text, adjust the voice or change output formats via audioConfig : 您可以通过audioConfig查看插入所需文本的位置,调整语音或更改输出格式:
{
'input':{
'text':'very simple example'
},
'voice':{
'languageCode':'en-gb',
'name':'en-GB-Standard-A',
'ssmlGender':'FEMALE'
},
'audioConfig':{
'audioEncoding':'MP3'
}
}
Original Answer 原始答案
As Hugolpz alludes, if you know the word or phrase you want (via a previous Translate API call), you can get MP3s from a URL like http://translate.google.com/translate_tts?ie=UTF-8&q=Bonjour&tl=fr 正如Hugolpz所说,如果你知道你想要的单词或短语(通过之前的Translate API调用),你可以从http://translate.google.com/translate_tts?ie=UTF-8&q=Bonjour&tl=这样的网址获取MP3。 FR
Note that &tl=fr
ensures that you get French instead of the default English. 请注意,
&tl=fr
确保您获得法语而不是默认英语。
You will need to rate-limit yourself, but if you're looking for a small number of words or phrases you should be fine. 您需要对自己进行限价,但如果您正在寻找少量的单词或短语,那么您应该没问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.