简体   繁体   English

使用Google Translate API获取单词的发音

[英]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. 本演练需要base64curlgcloudjq

  1. Create a project on the GCP console, or run something like gcloud projects create example-throwaway-tts 在GCP控制台上创建项目,或运行类似gcloud projects create example-throwaway-tts
  2. Enable billing for the project. 为项目启用结算 Do this even if you don't intend to exceed the freebie quota. 即使您不打算超过免费配额,也要这样做。
  3. Use the GCP console to enable the TTS API for the project you just set up. 使用GCP控制台为刚刚设置的项目启用TTS API
  4. Use the console again, this time to make a new service account . 再次使用控制台这次是建立一个新的服务帐户
    • Use any old name 使用任何旧名称
    • Don't give it a role. 不要发挥作用。 You'll get a warning. 你会收到警告。 This is okay. 这没关系。
    • Select key type JSON if it isn't already selected 如果尚未选择密钥类型JSON,请选择它
    • Click Create 单击“ Create
    • Hold onto the JSON file that your browser downloads 保留浏览器下载的JSON文件
  5. Set an environment variable to point at that file, eg export GOOGLE_APPLICATION_CREDENTIALS="~/Downloads/service-account-file.json" 设置环境变量以指向该文件,例如export GOOGLE_APPLICATION_CREDENTIALS="~/Downloads/service-account-file.json"
  6. Get the appropriate access token: 获取适当的访问令牌:
    1. Tell gcloud to use that new project: gcloud config set project example-throwaway-tts 告诉gcloud使用这个新项目: gcloud config set project example-throwaway-tts
    2. Set a variable TTS_ACCESS_TOKEN=gcloud auth application-default print-access-token 设置变量TTS_ACCESS_TOKEN=gcloud auth application-default print-access-token
  7. Put together a JSON request. 汇总JSON请求。 I'll give an example below. 我将在下面给出一个例子。 For this example we'll call it request.json 对于此示例,我们将其称为request.json
  8. 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 这是做什么的

  • authenticate using the default access token for the project you set up 使用您设置的项目的默认访问令牌进行身份验证
  • set the content type to JSON (so that jq can extract the payload) 将内容类型设置为JSON(以便jq可以提取有效负载)
  • use request.json as the data to send using curl 's --data-raw flag 使用request.json作为要使用curl--data-raw标志发送的数据
  • extract the value of audioContent from the response 从响应中提取audioContent的值
  • base64 decode that content base64解码该内容
  • save the whole mess as an MP3 把整个烂摊子当成MP3

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM