AWS SDK for Ruby
Developer Guide

Synthesizing Speech

This example uses the synthesize_speech method to get the text from a file and produce an MP3 file containing the synthesized speech.

Choose Copy to save the code locally.

Create the file polly_synthesize_speech.rb.

Add the required gem.


Version 2 of the AWS SDK for Ruby didn't have service-specific gems.

require 'aws-sdk-polly' # In v2: require 'aws-sdk'

Get the name of the text file from the command line.

if ARGV.empty?() puts 'You must supply a filename' exit 1 end filename = ARGV[0]

Open the text file and read the contents as a string.

contents =

Create an Amazon Polly client and call synthesize_speech.

polly = resp = polly.synthesize_speech({ output_format: "mp3", text: contents, voice_id: "Joanna", })

Save the resulting synthesized speech as an MP3 file.

name = File.basename(filename) parts = name.split('.') first_part = parts[0] mp3_file = first_part + '.mp3' IO.copy_stream(resp.audio_stream, mp3_file)


The resulting MP3 file is in the MPEG-2 format.

See the complete example on GitHub.