Python3-boto3响应

2024-09-27 07:24:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用AWS-Lex生成对我的声音(http://boto3.readthedocs.io/en/latest/reference/services/lex-runtime.html)的响应。在

响应audioStream是boto3(https://botocore.readthedocs.io/en/latest/reference/response.html#botocore.response.StreamingBody)中的StreamingBody对象。在

问题是我能不能把一个字节转成数组?在

我试过以下方法:

audio_stream = response['audioStream'].read()
f = open('response.wav', 'wb')
f.write(audio_stream)
f.close()

但是我得到了一个错误,sox和aplay的格式是无效的(RIFF头没有找到)

我还尝试使用wave库,代码如下

^{pr2}$

但是当我播放文件时,我只得到白噪音,而且长度很短。在


Tags: ioaws声音streamresponsehtmlreadthedocsboto3
1条回答
网友
1楼 · 发布于 2024-09-27 07:24:31

答案是在将流写入文件之前关闭流。工作代码如下:

    audio_stream = response['audioStream'].read()
    response['audioStream'].close()

    f = wave.open(self.response_fname, 'wb')
    f.setnchannels(2)
    f.setsampwidth(2)
    f.setframerate(16000)
    f.setnframes(0)

    f.writeframesraw(audio_stream)
    f.close()

相关问题 更多 >

    热门问题