How to convert MediaRecorder stream in Django into wav without disk storage?

I am trying to convert the bytes_data received by the channels receive() method using a WebsocketConsumer. This is the JavaCode:

navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
            const mediaRecorder = new MediaRecorder(stream, {mimeType: 'audio/webm' })
            chatSocket.onopen = () => {
                mediaRecorder.addEventListener("dataavailable", (event) => {
                if ( > 0 && chatSocket.readyState == 1) {
            }) // chatSocket.onopen close

On the server side I now want to convert the bytes_data into a wav file so that I can send these short audio snippets via speech_recognition to the recognize_google api. I tried to use pydub to convert the bytes_data but so far I had no success.

The channels GeneralConsumer(WebsocketConsumer) class now receives the bytes_data and uses this function to convert and send it to google:

def transcriber(bytes_data: bytes):
    r = sr.Recognizer()
    audio = AudioSegment.from_raw(
        bytes_data, sample_width=2, frame_rate=16000, channels=1)

    buffer = io.BytesIO()
    audio.export(buffer, format="wav")

    with sr.AudioFile(buffer) as source:
        audio = sr.AudioData(,
                             sample_rate=16000, sample_width=2)

        transcription = r.recognize_google(audio, language="en-USA").lower()

    except sr.UnknownValueError as e:

    return transcription

But that does not work. I have tried some variations and others I simply don't understand. A comprehensive solution would be appreciated.

Back to Top