Pydub Convert Mp3 To Wav

0 views
Skip to first unread message

Christin Baus

unread,
Jun 30, 2024, 10:13:40 AM6/30/24
to wratanvico

My web app uses the package pydub, which depends on ffmpeg. My code converts a user-submitted audio file to mp3 and then saves it to the database. Locally, this code is working great, however I am getting an error when it's on pythonanywhere. I am wondering if there is anything I have to do to access ffmpeg.

I also had this problem and managed to find a solution. I was using pydub to load and edit audio segments and then wanted to send a pydub audio segment directly to whisper without having to create a temporary file. The following approach worked: basically create BytesIO buffer, encode the audio into it in a supported format and then pass it to whisper:

simpleaudio is a cross-platform library for playback of (mono and stereo) WAV files with no dependencies. The following code can be used to play a WAV file, and wait for the file to finish playing before terminating the script:

To reduce file size, it may be sufficient to store some recordings (for example of human speech) at a lower sampling rate, such as 8000 samples per second, although this does mean that higher sound frequencies may not be as accurately represented.

Both correspond to a sequence of data points that can be played back at a specified sample rate in order to play a sound. For a bytes object, each sample is stored as a set of two 8-bit values, whereas in a NumPy array, each element can contain a 16-bit value corresponding to a single sample.

An important difference between these two data types is that bytes objects are immutable, whereas NumPy arrays are mutable, making the latter more suitable for generating sounds and for more complex signal processing. For more information on how to work with NumPy, have a look at our NumPy tutorials.

simpleaudio allows you to play NumPy and Python arrays and bytes objects using simpleaudio.play_buffer(). Make sure you have NumPy installed for the following example to work, as well as simpleaudio. (With pip installed, you can do this by running pip install numpy from your console.)

winsound does not support playback of any files other than WAV files. It does allow you to beep your speakers using winsound.Beep(frequency, duration). For example, you can beep a 1000 Hz tone for 100 milliseconds with the following code:

The line containing sf.read() extracts the raw audio data, as well as the sampling rate of the file as stored in its RIFF header, and sounddevice.wait() ensures that the script is only terminated after the sound finishes playing.

Although pydub can open and save WAV files without any dependencies, you need to have an audio playback package installed to play audio. simpleaudio is strongly recommended, but pyaudio, ffplay, and avplay are alternative options.

In order to play back other audio types, such as MP3 files, ffmpeg or libav should be installed. Have a look at the documentation of pydub for instructions. As an alternative to the steps described in the documentation, ffmpeg-python provides bindings for ffmpeg, and can be installed using pip:

In addition to playing back sound files, pydub lets you save audio in different file formats (more on this later), slice audio, calculate the length of audio files, fade in or out, and apply cross-fades.

pyaudio provides bindings for PortAudio, the cross-platform audio I/O library. This means that you can use pyaudio to play and record audio on a variety of platforms, including Windows, Linux, and Mac. With pyaudio, playing audio is done by writing to a .Stream:

It also allows you to play and record audio in callback mode, where a specified callback function is called when new data is required for playback, or available for recording. These options make pyaudio a suitable library to use if your audio needs go beyond simple playback.

The python-sounddevice and pyaudio libraries provide ways to record audio with Python. python-sounddevice records to NumPy arrays and pyaudio records to bytes objects. Both of these can be stored as WAV files using the scipy and wave libraries, respectively.

python-sounddevice allows you to record audio from your microphone and store it as a NumPy array. This is a handy datatype for sound processing that can be converted to WAV format for storage using the scipy.io.wavfile module. Make sure to install the scipy module for the following example (pip install scipy). This automatically installs NumPy as one of its dependencies:

You saw earlier that you can use the scipy.io.wavfile module to store NumPy arrays as WAV files. The wavio module similarly lets you convert between WAV files and NumPy arrays. If you want to store your audio in a different file format, pydub and soundfile come in handy, as they allow you to read and write a range of popular file formats (such as MP3, FLAC, WMA and FLV).

In this example, my_np_array is a NumPy array containing audio, fs is the sample rate of the recording (usually 44100 or 44800 Hz), and sampwidth is the sampling width of the audio (the number of bytes per sample, typically 1 or 2 bytes).

pydub lets you save audio in any format that ffmpeg supports, which includes nearly all audio types you might encounter in your daily life. For example, you can convert your WAV file to MP3 with the following code:

The libraries that are included in this tutorial are chosen for their ease of use and popularity. For a more comprehensive list of audio libraries for Python, have a look at the wiki page on audio in Python.

Having audio files in a single or rather consistent format is essential when playing on different media players. Mp3 format is the most adapted audio codec format and most media players have the capability to render such files. In this tutorial, we will focus on converting other audio formats into mp3.

You will need to have the following installed on your machine. Python, FFmpeg. FFmpeg can be used directly from the terminal if you're looking to convert just a few files. However, if you are converting multiple files, this is where the scripting part with python comes in handy.

After installing the above requirements, add them to the environment path in your machine so you can access the globally. You will also need to install a python package called pydub using the python package manager pip via the following command:

You can add any other file type you have that's not listed there and it will work like a charm. You can also comment out the rest of the files that you don't have. You can basically customize the file above and make it yours.

Pyaudio allows us to play and record sounds with Python. To play MP3, however, we first need to convert the MP3 file to WAV format with ffmpeg. To use ffmpeg in Python, we use an interface tool called Pydub, which directly calls our ffmpeg executable and integrates with Pyaudio.

In last lab we will learned how to play an audio file stored on the flash memory or SD card. We used an early version of a Python module that plays .WAV files in a fixed format: 8,000 samples per second using 16-bit encoding. Because there are hundreds of different formats of audio files, we need a consistent way to convert all of these formats to 8K samples/second 16-bit .WAV formats.

One of the easiest ways to get started is to go to the web site ffmpeg.org and download the program that does the conversion using a command line. Note that on a Mac you will need to go into your System Preferences and indicate that the following programs are trusted:

To get the format we need for the MicroPython wave player class we just specific -i for the input file and use the -ar 8000 to specify the output bit rate of 8K samples per second. The final parameter is a file name that must in .wav so the command knows to use WAV PCM encoding. The default value is 16 gits per sample.

We can use unix shell commands to do a batch conversion of all the . The following is an example of using awk and sed to convert all the .mp3 files in a directory and convert them to 8K Hz WAV files and put them in a sibling directory.

In this case I will show you how to convert files to .mp3. We will use pydub which offers many different formats so you can use python to convert between wav, m4a, mp4, oog and more. You can also to batch converting if you extend my solution. It works pretty much the same on mac and linux. The only difference is installation of ffmpeg which is solution that pydub is based on.

There is a django-elastic-transcoder which is a Django package that helps you leveraging AWS transcoder so you could manipulate files. I wanted to have a simpler solution and use my server to convert files. This way the converting is being done on the server and you could make mp4 to mp3 online converter to not store people's files. This is a also good solution to make django audio player (in case you have for example a Raspberry Pi). I do not have much traffic for now so it's not an obstacle.

The first step is to send the files to our backend. Because we will use MultiPartParser from django REST framework, we need to construct the POST (or PUT, PATCH) request with FormData:

Now we add extra information to the object. Not passing a valid object could cause errors like: The submitted data was not a file. Check the encoding type on the form. or The file was empty. Now we just need to return the File()

Now the function PUT. When we get the _temp_audio_file_ it is already written on our server as TemporaryUploadedFile. Now we know we need to pass it through our custom function. This is all PUT function with commented steps:

Now you can play with this solution, create html audio player or start your django audio streaming app. You could for example make a text reader that would create movies from text. Pass text to mp3 in python and then convert it to mp4 clip. The possibilities are endless and programing is like magic.

Saving to hard drive with django and converting are resource and time consuming operations. In order to provide your users with good User Experience you should implement it once you start getting visitors. You could use django celery

d3342ee215
Reply all
Reply to author
Forward
0 new messages