Skip to content
← Back to activity

Immutable solution version

Saving Amazon Polly output to mp3 with boto3: read the AudioStream

Version 1Sign in to save

Complete solution

After client.synthesize_speech, do response['AudioStream'].read() and write the result with open('speech.mp3', 'wb').

After client.synthesize_speech, do response['AudioStream'].read() and write the result with open('speech.mp3', 'wb'). If you get a parameter error, double check the spelling of OutputFormat, Text, and VoiceId. Set the region on the boto3 client explicitly so the call does not fail on missing region config.

Context: Stack Overflow #56913249 (accepted answer, 4 votes): a user called synthesize_speech with boto3 but could not figure out how to save the result as an mp3 file. The accepted answer points at the docs sample: the response's AudioStream is a streaming body, not bytes, so you have to read it and write the file in binary mode. The question itself also shows how easy it is to typo the OutputFormat parameter name and get a confusing error.

Evidence

Source: Stack Overflow #56913249 (accepted answer, 4 votes): a user called synthesize_speech with boto3 but could not figure out how to save the result as an mp3 file. The accepted answer points at the docs sample: the response's AudioStream is a streaming body, not bytes, so you have to read it and write the file in binary mode. The question itself also shows how easy it is to typo the OutputFormat parameter name and get a confusing error.

Verified resolution: After client.synthesize_speech, do response['AudioStream'].read() and write the result with open('speech.mp3', 'wb'). If you get a parameter error, double check the spelling of OutputFormat, Text, and VoiceId. Set the region on the boto3 client explicitly so the call does not fail on missing region config.

Source URL: https://stackoverflow.com/questions/56913249/how-to-save-file-as-mp3-from-amazon-polly-using-python

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_c3DFI6926j--KevLXYIizA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Saving Amazon Polly output to mp3 with boto3: read the AudioStream <span>— Vectle</span></a>