R
R
Roman Andreevich2021-08-19 16:55:19
JavaScript
Roman Andreevich, 2021-08-19 16:55:19

How to make mp3 from audioBuffer?

Good day, I'm sorry, but this topic has been confusing me for the second day already, IMHO has never touched the web audio api and has already stopped sleeping. Maybe there are people here who can help solve the problem.

In general, the essence is as follows:

I am writing an application in react. all OK. until the moment when it became necessary for me to upload audio files. they come from different places and in different formats. It can be wave, wav. mp3, acc and so on, in general, everything that has a mime type - audio/*. There are no problems with loading. input file, form data and everything is ok. BUT!!!!!!!

In order not to waste traffic and not load the server with unnecessary data, you need to compress (convert/transcode) the file into mp3 format on the client side and compress it to a certain size.

An example that I like (admins are not advertising, but an example): here.

Having started to understand this topic, I calmly get a file buffer:

const reader = new FileReader();

    reader.readAsArrayBuffer(input.files[0]);

    reader.addEventListener('load', ({ target }) => {
        if (!target.result) {
            return;
        }

        this.audioCtx.decodeAudioData(buffer)
            .then(audioBuffer => {

            // собсна audioBuffer это и есть нужный буффер
        });

    }, false);


After receiving the buffer, you need to somehow convert / re-encode this buffer to mp3, with the following parameters:

ffmpeg -i sound.mp3 -ar 32000 -ac 2 -b:a 64K -f mp3 result.mp3


This is an example of running ffmpeg on the server, it does everything right, but on the server. In the example above, you can see that everything is done in the browser. The task is exactly the same, i.e. take a buffer, convert it and send it to the server with form data.

There is no problem with shipping. But how to convert it?

For some reason I can not find any libs for such manipulations. If someone gives reasonable advice or shows where to look, I will be very grateful. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Step, 2021-08-29
@kellas

Something like this https://stackoverflow.com/questions/61264581/how-t...
This is essentially a copy of the code from the lamejs lib. The
example you like sends a file to the server

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question