Skip to content

Instantly share code, notes, and snippets.

@dzakyputra
Last active April 25, 2020 15:16
Show Gist options
  • Save dzakyputra/e14350adaa2104be71743cc7307e1404 to your computer and use it in GitHub Desktop.
Save dzakyputra/e14350adaa2104be71743cc7307e1404 to your computer and use it in GitHub Desktop.
def create_audio(text, language='ko-KR'):
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Set the text input to be synthesized
synthesis_input = texttospeech.types.SynthesisInput(text=text)
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.types.VoiceSelectionParams(
language_code='ko-KR',
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('audio/{}.mp3'.format(text), 'wb') as out:
# Write the response to the output file.
out.write(response.audio_content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment