使用命令行根据文字创建音频
本文档将引导您完成使用命令行向 Text-to-Speech 发出请求的过程。如需详细了解 Text-to-Speech 中的基本概念,请阅读 Text-to-Speech 基础知识。
准备工作
您必须先完成以下操作,然后才能向 Text-to-Speech API 发送请求。如需了解详情,请参阅准备工作页面。
- 在 GCP 项目上启用 Text-to-Speech。
- 确保已为 Text-to-Speech 启用结算功能。
-
After installing the Google Cloud CLI, configure the gcloud CLI to use your federated identity and then initialize it by running the following command:
gcloud init
将文字合成为音频
您可以通过向 https://texttospeech.googleapis.com/v1/text:synthesize
端点发出 HTTP POST 请求,将文本转换为音频。在 POST 命令正文的 voice
配置部分指定要合成的语音类型,在 input
部分的 text
字段中指定要合成的文本,并在 audioConfig
部分指定要创建的音频类型。
在命令行执行以下命令以使用 Text-to-Speech 从文本合成音频。该命令使用
gcloud auth application-default print-access-token
命令检索请求的授权令牌。在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:您的 Google Cloud 项目的字母数字 ID。
HTTP 方法和网址:
POST https://texttospeech.googleapis.com/v1/text:synthesize
请求 JSON 正文:
{ "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" } }
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
将请求正文保存在名为
request.json
的文件中,然后执行以下命令:curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project:PROJECT_ID " \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://texttospeech.googleapis.com/v1/text:synthesize"PowerShell (Windows)
将请求正文保存在名为
request.json
的文件中,然后执行以下命令:$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_ID " }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://texttospeech.googleapis.com/v1/text:synthesize" | Select-Object -Expand Content您应该收到类似以下内容的 JSON 响应:
{ "audioContent": "//NExAASCCIIAAhEAGAAEMW4kAYPnwwIKw/BBTpwTvB+IAxIfghUfW.." }
REST 命令的 JSON 输出包含 base64 编码格式的合成音频。将
audioContent
字段的内容复制到名为synthesize-output-base64.txt
的新文件中。您的新文件将如下所示://NExAARqoIIAAhEuWAAAGNmBGMY4EBcxvABAXBPmPIAF//yAuh9Tn5CEap3/o ... VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
将
synthesize-output-base64.txt
文件的内容解码到名为synthesized-audio.mp3
的新文件。如需了解如何对 base64 内容进行解码,请参阅对使用 Base64 编码的音频内容进行解码。仅将使用 base64 编码的内容复制到文本文件中。
使用 base64 命令行工具通过
-d
标志对源文本文件进行解码:
$ base64
SOURCE_BASE64_TEXT_FILE -d >DESTINATION_AUDIO_FILE 仅将使用 base64 编码的内容复制到文本文件中。
使用 base64 命令行工具对源文本文件进行解码:
$ base64 --decode
SOURCE_BASE64_TEXT_FILE >DESTINATION_AUDIO_FILE 仅将使用 base64 编码的内容复制到文本文件中。
使用
certutil
命令对源文本文件进行解码。
certutil -decode
SOURCE_BASE64_TEXT_FILE DESTINATION_AUDIO_FILE 在音频应用中或音频设备上播放
synthesized-audio.mp3
的内容。您还可以在 Chrome 浏览器中打开synthesized-audio.mp3
,以通过导航到包含该文件的文件夹来播放音频,例如file://my_file_path/synthesized-audio.mp3
。
清理
为避免产生不必要的 Google Cloud Platform 费用,请使用 Google Cloud 控制台删除您不需要的项目。
后续步骤