python - 如何在python3中将音频流写入文件
问题描述
我正在与 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()
音频文件的长度已更新,但音频数据未写入文件中。
解决方案
至少这工作正常:
def nexmoServer():
input_filename = "a.wav"
output_filename = "b.wav"
ifile = open(input_filename,'rb')
ofile = open(output_filename, 'wb')
data = ifile.read(1024*1024)
while data:
ofile.write(data)
data = ifile.read(1024*1024)
ofile.close()
ifile.close()
nexmoServer()
推荐阅读
- reactjs - 我的受保护路线不工作 Reactjs
- azure - QNAmaker 最大 QNA 对数
- python - 列表项中间有空格的 Python 列表
- javascript - Angular:使用 forkJoin 调用 api 服务时出现 500 错误
- excel - 公共变量在单独的工作簿模块中未被识别?
- c# - 未创建 Xamarin Forms 实体框架核心数据库
- excel - 工作表更改事件,如果单元格内容被清除,则清除它旁边的单元格
- c - 给定 10 个输入数字并存储在一个数组中,我如何检查下一个值是否与上一个不同?
- excel - 设置给定工作表中的最后一行并复制所有数据并粘贴到另一个工作表中并更新数据透视表
- node.js - 函数lamda的权限被拒绝