-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
30 lines (22 loc) · 851 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
from google.cloud import texttospeech
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./key.json"
def synthesize_speech(text, output_file):
client = texttospeech.TextToSpeechClient()
input_text = texttospeech.SynthesisInput(text=text)
voice = texttospeech.VoiceSelectionParams(
language_code='en-US',
ssml_gender=texttospeech.SsmlVoiceGender.FEMALE
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3
)
response = client.synthesize_speech(
input=input_text, voice=voice, audio_config=audio_config
)
with open(output_file, 'wb') as out:
out.write(response.audio_content)
# Example usage
text = "Hello, this is a test of the Google Cloud Text-to-Speech API."
output_file = "output.mp3"
synthesize_speech(text, output_file)