Skip to content

Instantly share code, notes, and snippets.

@kepstein
Created January 20, 2017 19:01
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kepstein/206740d919de64248b24e75a9466ae2a to your computer and use it in GitHub Desktop.
Save kepstein/206740d919de64248b24e75a9466ae2a to your computer and use it in GitHub Desktop.
AWS Polly generates TTS and downloads the result to MP3 using Boto3
#!/usr/bin/python
import boto3
from contextlib import closing
client = boto3.client('polly')
response = client.synthesize_speech(
OutputFormat='mp3',
Text='Amazon Polly is a service that turns text into lifelike speech. Polly lets you create applications that talk, enabling you to build entirely new categories of speech-enabled products. Polly is an Amazon AI service that uses advanced deep learning technologies to synthesize speech that sounds like a human voice. Polly includes 47 lifelike voices spread across 24 languages, so you can select the ideal voice and build speech-enabled applications that work in many different countries.',
TextType='text',
VoiceId='Salli'
)
print response
if "AudioStream" in response:
with closing(response["AudioStream"]) as stream:
output = "polly-boto.mp3"
try:
# Open a file for writing the output as a binary stream
with open(output, "wb") as file:
file.write(stream.read())
except IOError as error:
# Could not write to file, exit gracefully
print(error)
sys.exit(-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment