3 回答

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個(gè)贊
為 JSON 文件配置 Python 應(yīng)用程序并安裝客戶端庫(kù)
創(chuàng)建服務(wù)帳戶
使用此處的服務(wù)帳戶創(chuàng)建服務(wù)帳戶密鑰
JSON 文件下載并安全保存
在您的 Python 應(yīng)用程序中包含 Google 應(yīng)用程序憑據(jù)
安裝庫(kù):
pip install --upgrade google-cloud-texttospeech
使用 Google 的 Python 示例找到:https : //cloud.google.com/text-to-speech/docs/reference/libraries 注意:在 Google 的示例中,它沒(méi)有正確包含 name 參數(shù)。和 https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/texttospeech/cloud-client/quickstart.py
以下是使用谷歌應(yīng)用程序憑據(jù)和女性的 wavenet 語(yǔ)音從示例中修改的內(nèi)容。
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/home/yourproject-12345.json"
from google.cloud import texttospeech
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Set the text input to be synthesized
synthesis_input = texttospeech.types.SynthesisInput(text="Do no evil!")
# Build the voice request, select the language code ("en-US")
# ****** the NAME
# and the ssml voice gender ("neutral")
voice = texttospeech.types.VoiceSelectionParams(
language_code='en-US',
name='en-US-Wavenet-C',
ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)
# Select the type of audio file you want returned
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(synthesis_input, voice, audio_config)
# The response's audio_content is binary.
with open('output.mp3', 'wb') as out:
# Write the response to the output file.
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
語(yǔ)音、姓名、語(yǔ)言代碼、SSML 性別等
語(yǔ)音列表:https : //cloud.google.com/text-to-speech/docs/voices
在上面的代碼示例中,我將 Google 示例代碼中的語(yǔ)音更改為包含名稱參數(shù)并使用 Wavenet 語(yǔ)音(大大改進(jìn)但更貴 16 美元/百萬(wàn)個(gè)字符)和 SSML 性別為 FEMALE。
voice = texttospeech.types.VoiceSelectionParams(
language_code='en-US',
name='en-US-Wavenet-C',
ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊
如果您想避免使用 google Python API,您可以簡(jiǎn)單地執(zhí)行以下操作:
import requests
import json
url = "https://texttospeech.googleapis.com/v1beta1/text:synthesize"
text = "This is a text"
data = {
"input": {"text": text},
"voice": {"name": "fr-FR-Wavenet-A", "languageCode": "fr-FR"},
"audioConfig": {"audioEncoding": "MP3"}
};
headers = {"content-type": "application/json", "X-Goog-Api-Key": "YOUR_API_KEY" }
r = requests.post(url=url, json=data, headers=headers)
content = json.loads(r.content)
它與您所做的類似,但您需要包含您的 API 密鑰。

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
找到了答案并丟失了我打開(kāi)的 150 個(gè) Google 文檔頁(yè)面之間的鏈接。
#(Since I'm using a Jupyter Notebook)
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/Path/to/JSON/file/jsonfile.json"
from google.cloud import texttospeech
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Set the text input to be synthesized
synthesis_input = texttospeech.types.SynthesisInput(text="Hello, World!")
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.types.VoiceSelectionParams(
language_code='en-US',
ssml_gender=texttospeech.enums.SsmlVoiceGender.NEUTRAL)
# Select the type of audio file you want returned
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(synthesis_input, voice, audio_config)
# The response's audio_content is binary.
with open('output.mp3', 'wb') as out:
# Write the response to the output file.
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
我耗時(shí)的追求是嘗試使用 Python 通過(guò) JSON 發(fā)送請(qǐng)求,但這似乎是通過(guò)自己的模塊,工作正常。請(qǐng)注意,默認(rèn)語(yǔ)音性別為“中性”。
添加回答
舉報(bào)