Convert MP3 to WAV in Python

  1. Use the pydub Module to Convert MP3 to WAV in Python
  2. Use the subprocess Module to Convert MP3 to WAV in Python

MP3 is a coding format developed mainly by the Fraunhofer Society in Germany. MP3 file uses a compression algorithm which helps to reduce the overall file size.

A WAV file format is an audio file format that uses containers that store audio data, numerical data, bit rate, and sample rate. These files are uncompressed audio files but can take quite a bit of space.

We will introduce how to convert MP3 to WAV format in Python in this tutorial.

Use the pydub Module to Convert MP3 to WAV in Python

We can use the open-source library called pydub to carry out this conversion. It is a high-level yet straightforward interface that can open several multimedia audio and video formats and manipulate them.

We will read the MP3 file using the from_mp3() function. We will then export this file using the export() function, where we will specify the format as wav in the format argument.

For example,

from pydub import AudioSegment

# files                                                                       
src = "transcript.mp3"
dst = "test.wav"

# convert wav to mp3                                                            
audSeg = AudioSegment.from_mp3("transcript.mp3")
audSeg.export(dst, format="wav")

Use the subprocess Module to Convert MP3 to WAV in Python

The subprocess module creates new processes to run different applications. We can use the call() function from this module to perform the necessary conversion. The output from this function is redirected directly to the files.

See the following code.

import subprocess
subprocess.call(['ffmpeg', '-i', 'transcript.mp3',
                   'test.wav'])

The pydub module does the same thing, but essentially in the background.