如何在python3中将音频流写入文件

时间:2019-03-01 06:57:40

标签: python websocket nexmo

我正在与Nexmo合作。我想将直播Nexmo通话音频保存到磁盘。 我已经实现了Web套接字来读取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()

音频文件的长度已更新,但音频数据未写入文件中。

1 个答案:

答案 0 :(得分:0)

至少可以正常工作

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()
相关问题