# Combining Audio Chunks Received via WebSockets into a Single Audio File in Django

**URL:** <https://forum.djangoproject.com/t/combining-audio-chunks-received-via-websockets-into-a-single-audio-file-in-django/33105>\
**Category:** Async/Channels\
**Created:** [July 19, 2024, 10:06am UTC](https://forum.djangoproject.com/t/combining-audio-chunks-received-via-websockets-into-a-single-audio-file-in-django/33105 "2024-07-19T10:06:34Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![BJoshi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bjoshi/32/22718_2.png) [@BJoshi](https://forum.djangoproject.com/u/BJoshi)\
**Post date:** [July 19, 2024, 10:06am UTC](https://forum.djangoproject.com/t/combining-audio-chunks-received-via-websockets-into-a-single-audio-file-in-django/33105/1 "2024-07-19T10:06:34Z")

</div>

I’m working on a Django project where I need to collect streaming audio chunks from the frontend via WebSockets, and then combine these chunks on the backend to create a single audio file. My current setup involves streamin audio on the frontend and sending it to the backend in small chunks. After creating audio file in backend I want to transcript audio file to text.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 19, 2024, 10:51am UTC](https://forum.djangoproject.com/t/combining-audio-chunks-received-via-websockets-into-a-single-audio-file-in-django/33105/2 "2024-07-19T10:51:50Z")

</div>

Welcome @BJoshi !

That sounds like an interesting project.

---

<div class="post-metadata">

**Author:** ![BJoshi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bjoshi/32/22718_2.png) [@BJoshi](https://forum.djangoproject.com/u/BJoshi)\
**Post date:** [July 19, 2024, 11:03am UTC](https://forum.djangoproject.com/t/combining-audio-chunks-received-via-websockets-into-a-single-audio-file-in-django/33105/3 "2024-07-19T11:03:20Z")

</div>

Thank you for reply but I need solution of this because i already search and do R&D about it But still i didn’t found any solution on it.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 19, 2024, 11:06am UTC](https://forum.djangoproject.com/t/combining-audio-chunks-received-via-websockets-into-a-single-audio-file-in-django/33105/4 "2024-07-19T11:06:59Z")

</div>

So what part of this do you have questions about? The more specific you are with identifying where you are stuck, the better chance we have of providing assistance.

---

<div class="post-metadata">

**Author:** ![BJoshi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bjoshi/32/22718_2.png) [@BJoshi](https://forum.djangoproject.com/u/BJoshi)\
**Post date:** [July 19, 2024, 12:56pm UTC](https://forum.djangoproject.com/t/combining-audio-chunks-received-via-websockets-into-a-single-audio-file-in-django/33105/5 "2024-07-19T12:56:32Z")

</div>

```class
    
    async def connect(self):
        await self.accept()
        self.model = whisper.load_model('base')
        # self.audio_chunk = []

    async def disconnect(self, close_code):
        await self.close()

    async def receive(self, text_data=None, bytes_data=None):
        if bytes_data:
            try:
                file_name = 'test.wav'
                audio = AudioSegment.from_file(io.BytesIO(bytes_data), format='webm')
                audio.export(file_name, format="wav")

                text = await speech_to_text(file_name)
                print(text,'text')
            except Exception as e:
                await self.send(text_data=json.dumps({
                    'error': f"Error: {str(e)}"
                }))

async def speech_to_text(file_name):

    recognizer = sr.Recognizer()
    
    with sr.AudioFile(file_name) as source:
        audio_data = recognizer.record(source)
    try:
        text = recognizer.recognize_google(audio_data)
        print(text,'text')
        return text
    except sr.UnknownValueError:
        return "Could not understand the audio"
    except sr.RequestError as e:
        return f"Could not request results from the speech recognition service; {e}"

```

Here is my consumers.py file

---

<div class="post-metadata">

**Author:** ![BJoshi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bjoshi/32/22718_2.png) [@BJoshi](https://forum.djangoproject.com/u/BJoshi)\
**Post date:** [July 19, 2024, 12:57pm UTC](https://forum.djangoproject.com/t/combining-audio-chunks-received-via-websockets-into-a-single-audio-file-in-django/33105/6 "2024-07-19T12:57:27Z")

</div>

```auto
                    const stream = await navigator.mediaDevices.getUserMedia({audio:true});

                    if (!MediaRecorder.isTypeSupported('audio/webm')) {
                        alert('Browser not supported for MediaRecorder');
                        return;
                    }

                    const mediaRecorder = new MediaRecorder(stream, {
                        mimeType: 'audio/webm',
                    });

                    const socket = new WebSocket('ws://' + window.location.host + '/ws/chat/');

                    socket.onopen = () => {
                        document.querySelector('#status').textContent = 'Connected';
                        console.log({ event: 'onopen' });

                        mediaRecorder.addEventListener('dataavailable', async (event) => {
                            if (event.data.size > 0 && socket.readyState === 1) {
                                    socket.send(array);
                            }
                        });
                        mediaRecorder.start(1000);
                    };

```

Here is my javascript code in which i get audio and send it via websocket

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 19, 2024, 12:58pm UTC](https://forum.djangoproject.com/t/combining-audio-chunks-received-via-websockets-into-a-single-audio-file-in-django/33105/7 "2024-07-19T12:58:23Z")

</div>

Please do **not** post images of code here. Copy/paste the code into the body of your post.

When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.

Please delete these images and post the code you would like us to examine.

Also, don’t _just_ post the code. Be specific and identify what the issue or problem is that you would like assistance with.

---

<div class="post-metadata">

**Author:** ![BJoshi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bjoshi/32/22718_2.png) [@BJoshi](https://forum.djangoproject.com/u/BJoshi)\
**Post date:** [July 19, 2024, 12:59pm UTC](https://forum.djangoproject.com/t/combining-audio-chunks-received-via-websockets-into-a-single-audio-file-in-django/33105/8 "2024-07-19T12:59:09Z")

</div>

ohk thank you for information

---

<div class="post-metadata">

**Author:** ![BJoshi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bjoshi/32/22718_2.png) [@BJoshi](https://forum.djangoproject.com/u/BJoshi)\
**Post date:** [July 19, 2024, 1:07pm UTC](https://forum.djangoproject.com/t/combining-audio-chunks-received-via-websockets-into-a-single-audio-file-in-django/33105/9 "2024-07-19T13:07:29Z")

</div>

In this code i want to create audio file of bytes\_data i send throw javascript and transcript this audio file in text.  
In short term I want to transcribe real-time audio into text if you have any other option beside my code then provide me.  
I don’t want to use any paid services.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 19, 2024, 1:25pm UTC](https://forum.djangoproject.com/t/combining-audio-chunks-received-via-websockets-into-a-single-audio-file-in-django/33105/10 "2024-07-19T13:25:24Z")

</div>

> [@BJoshi](#):
>
> In short term I want to transcribe real-time audio into text

Sorry, I am not familiar with any such software.

---

<div class="post-metadata">

**Author:** ![BJoshi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bjoshi/32/22718_2.png) [@BJoshi](https://forum.djangoproject.com/u/BJoshi)\
**Post date:** [July 19, 2024, 1:36pm UTC](https://forum.djangoproject.com/t/combining-audio-chunks-received-via-websockets-into-a-single-audio-file-in-django/33105/11 "2024-07-19T13:36:08Z")

</div>

ohk, have you seen my code ?

Need to improve code ?

---

<div class="post-metadata">

**Author:** ![kenilJoshi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/keniljoshi/32/25082_2.png) [@kenilJoshi](https://forum.djangoproject.com/u/kenilJoshi)\
**Post date:** [November 9, 2024, 8:16am UTC](https://forum.djangoproject.com/t/combining-audio-chunks-received-via-websockets-into-a-single-audio-file-in-django/33105/12 "2024-11-09T08:16:48Z")

</div>

Use ffmpeg  
Its good and i was also facing same issue

---

<div class="post-metadata">

**Author:** ![sademban](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sademban/32/25160_2.png) [@sademban](https://forum.djangoproject.com/u/sademban)\
**Post date:** [March 5, 2026, 10:02am UTC](https://forum.djangoproject.com/t/combining-audio-chunks-received-via-websockets-into-a-single-audio-file-in-django/33105/13 "2026-03-05T10:02:12Z")

</div>

i am able to get the chunk and combine but the combine is eating up all my processor is ther a way we can do it efficiently and cost friendly
