简体   繁体   中英

How to write audio stream to file in python3

I am working with Nexmo. I would like to save live stream Nexmo call audio to my disk. I have implemented web socket to read Nexmo's stream. Please find below the code where I append audio to an existing audio file.

#!/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()

The audio file's length is updated but audio data is not written in the file.

Atleast this is working fine:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM