我正在使用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()
答案 0 :(得分:0)
您可以使用pydub
将mp3流直接加载到AudioSegment中。
from pydub import AudioSegment
sound = AudioSegment.from_mp3(output['AudioStream'])
答案 1 :(得分:0)
如果您不想将文件保存在本地,您可以使用 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()