如何使用 Python 从 Amazon Polly 将文件另存为 mp3

Shu*_*rma 3 python text-to-speech amazon-web-services boto3 amazon-polly

我正在使用 Amazon Polly 进行 TTS,但我无法了解如何将转换后的语音保存到我的计算机中的 .mp3 文件中

我已经尝试过 gTTS,但我的任务需要 Amazon Polly。

import boto3
client = boto3.client('polly')
response = client.synthesize_speech
(Text = "Hello my name is Shubham", OuptutFormat = "mp3", VoiceId = 'Aditi')
Run Code Online (Sandbox Code Playgroud)

现在,我应该怎么做才能播放此转换后的语音或将其作为 .mp3 文件保存到我的 PC 中?

use*_*432 9

此代码示例直接取自文档:https : //docs.aws.amazon.com/polly/latest/dg/SynthesizeSpeechSamplePython.html

import boto3

polly_client = boto3.Session(
                aws_access_key_id=,                     
    aws_secret_access_key=,
    region_name='us-west-2').client('polly')

response = polly_client.synthesize_speech(VoiceId='Joanna',
                OutputFormat='mp3', 
                Text = 'This is a sample text to be synthesized.')

file = open('speech.mp3', 'wb')
file.write(response['AudioStream'].read())
file.close()
Run Code Online (Sandbox Code Playgroud)