I'd like to instantiate a TextToSpeechClient()
with an API key rather than service account credentials provided in a global GOOGLE_APPLICATION_CREDENTIALS
variable. Specifically, I'd like to use the following function provided on the relevant website, but I'm unable to instantiate client
due to lack of credentials.
def synthesize_text(text):
"""Synthesizes speech from the input string of text."""
from google.cloud import texttospeech
client = texttospeech.TextToSpeechClient()
input_text = texttospeech.types.SynthesisInput(text=text)
# Note: the voice can also be specified by name.
# Names of voices can be retrieved with client.list_voices().
voice = texttospeech.types.VoiceSelectionParams(
language_code='en-US',
name='en-US-Standard-C',
ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
response = client.synthesize_speech(input_text, voice, audio_config)
# The response's audio_content is binary.
with open('output.mp3', 'wb') as out:
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
Alternately, I get past the credentials issue when I tried using the JSON from the Google text-to-speech website at https://cloud.google.com/text-to-speech and the requests
package as follows:
r = requests.post('https://texttospeech.googleapis.com/v1beta1/text:synthesize?key=API_KEY', data = json.dumps(data))
and I receive a response with text content. If I was to do it this way, how do I save the content I receive as an audio file?