【发布时间】:2019-03-01 06:57:39
【问题描述】:
我正在与 Nexmo 合作。我想将直播 Nexmo 通话音频保存到我的磁盘。 我已经实现了网络套接字来读取 Nexmo 的流。请在下面找到我将音频附加到现有音频文件的代码。
#!/usr/bin/env python
# WS server example
import asyncio
import websockets
async def nexmoServer(websocket, path):
audioData = await websocket.recv()
input_filename = "sample_harshit.wav"
output_filename = "new_file.wav"
ifile = open(input_filename,'rb')
ofile = open(output_filename, 'wb')
data = audioData
while data:
ofile.write(data)
data = ifile.read(1024*1024)
ofile.close()
ifile.close()
音频文件的长度已更新,但音频数据未写入文件。
【问题讨论】:
-
试试 ofile.append(data)
-
@N00b ,它给了我错误说“AttributeError:'_io.BufferedWriter'对象没有属性'append'”
-
您需要遍历从 websocket 接收到的所有音频帧。这将需要
async for message in websocket:而不是await websocket.recv(),后者只会接收一个帧。 -
我还建议您删除与
ifile相关的所有代码,这将有助于您了解您的函数出了什么问题。