我正在使用Amazon Polly作为文本到语音,但是我不想将转换后的文件保存到PC检索。相反,我想播放它作为流。
我已经使用了playsound
,但它不适用于返回的变量。
client = boto3.client('polly')
output = client.synthesize_speech (
Text = "Some random text I want to convert", OutputFormat = "mp3", VoiceId = 'Aditi'
)
print(output['AudioStream'])
file = open('speech.mp3', 'wb')
file.write(output['AudioStream'].read())
file.close()
您可以使用pydub
直接将MP3流加载到视觉中。
from pydub import AudioSegment
sound = AudioSegment.from_mp3(output['AudioStream'])
如果您不想在当地保存文件,则可以使用pyaudio
直接播放流。根据您的用例,您可以阅读具有定义的缓冲区大小的流块,或者只是阅读整个流并播放:
import sys
from contextlib import closing
import boto3
import pyaudio
from botocore.exceptions import BotoCoreError, ClientError
session = boto3.Session(aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
polly = session.client("polly")
SAMPLE_RATE = 16000
READ_CHUNK = 4096
CHANNELS = 1
BYTES_PER_SAMPLE = 2
try:
response = polly.synthesize_speech(Text="Hello world!",
VoiceId="Joanna",
LanguageCode="en-US",
OutputFormat="pcm",
SampleRate=str(SAMPLE_RATE))
except (BotoCoreError, ClientError) as error:
print(error)
sys.exit(-1)
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(BYTES_PER_SAMPLE),
channels=CHANNELS,
rate=SAMPLE_RATE,
output=True)
with closing(response["AudioStream"]) as polly_stream:
while True:
data = polly_stream.read(READ_CHUNK)
if data is None:
break
stream.write(data)
stream.stop_stream()
stream.close()
p.terminate()