首页 > 解决方案 > Google Cloud Text To Speech API 快速入门示例

问题描述

我是这个论坛的新手。我正在尝试让 Google Cloud TTS API 工作,但遇到了一些问题。

该页面是:https ://cloud.google.com/text-to-speech/docs/quickstart-protocol

我设法浏览了所有页面,直到我在下面引用“从文本合成音频”。我的问题是我根本不明白谷歌希望我如何运行脚本。这似乎是一个 Nix 声明,我使用 Windows。

最初我试图让 Python 示例工作,但我从来没有让它工作。

有人试过这个并让它工作吗?

引文:

从文本合成音频您可以通过向https://texttospeech.googleapis.com/v1beta1/text:synthesize端点 发出 HTTP POST 请求将文本转换为音频。在 POST 命令的正文中,在语音配置部分指定要合成的语音类型,在输入部分的文本字段中指定要合成的文本,并在 audioConfig 部分中指定要创建的音频类型。

在命令行中运行以下行以使用 Text-to-Speech API 从文本合成音频。该命令使用gcloud auth application-default print-access-token命令检索请求的授权令牌。

响应被定向到输出文件 synthesize-output.txt。

Curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
  -H "Content-Type: application/json; charset=utf-8" \
  --data "{
    'input':{
      'text':'Android is a mobile operating system developed by Google,
         based on the Linux kernel and designed primarily for
         touchscreen mobile devices such as smartphones and tablets.'
    },
    'voice':{
      'languageCode':'en-gb',
      'name':'en-GB-Standard-A',
      'ssmlGender':'FEMALE'
    },
    'audioConfig':{
      'audioEncoding':'MP3'
    }
  }" "https://texttospeech.googleapis.com/v1beta1/text:synthesize" > synthesize-text.txt

标签: cloudtext-to-speech

解决方案


cURL安装了吗?你可以通过做检查curl -V。如果您没有安装它,您可以按照此处的步骤操作

如果您的问题是返回的响应或缺少响应,我建议使用 API 密钥而不是服务帐户密钥。

这些是获取 API 密钥所需的所有步骤

  1. 在Cloud Console中创建一个项目(或使用现有项目)。
  2. 确保为您的项目启用计费。
  3. 启用文本转语音 API
  4. 创建一个API 密钥

然后你可以像这样使用 curl 命令

Curl -H "X-Goog-Api-Key: PUT_YOUR_API_KEY_HERE" \
  -H "Content-Type: application/json; charset=utf-8" \
  --data "{
    'input':{
      'text':'Android is a mobile operating system developed by Google,
         based on the Linux kernel and designed primarily for
         touchscreen mobile devices such as smartphones and tablets.'
    },
    'voice':{
      'languageCode':'en-gb',
      'name':'en-GB-Standard-A',
      'ssmlGender':'FEMALE'
    },
    'audioConfig':{
      'audioEncoding':'MP3'
    }
  }" "https://texttospeech.googleapis.com/v1beta1/text:synthesize" > synthesize-text.txt

推荐阅读