【问题标题】:How to write audio stream to file in python3如何在python3中将音频流写入文件
【发布时间】: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 相关的所有代码,这将有助于您了解您的函数出了什么问题。

标签: python websocket vonage


【解决方案1】:

至少这工作正常:

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()

【讨论】:

    猜你喜欢
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多