How to convert audio file from MP3 to WAV in Python?

Python – Convert mp3 to wav

In this tutorial, you shall learn how to convert a given MP3 audio file to WAV audio file in Python using pydub library.

Prerequisites

You must have pydub and FFmpeg installed in your system.

Refer Install pydub.

Step to convert mp3 to wav using pydub

1. Import AudioSegment

We have to import AudioSegment class from pydub package.

from pydub import AudioSegment

2. Create AudioSegment

We have to create AudioSegment from given input audio file. Since the input audio is in MP3 format, we have to call from_mp3() method of the AudioSegment class, and pass the path to given input audio file as string argument.

audio = AudioSegment.from_mp3(input_audio_file)

3. Export AudioSegment

We can export the AudioSegment created in the previous step to an output file in a specified format.

Since our task is to convert given MP3 audio file to WAV file, all we have to do is, call export() method on the AudioSegment object, pass the required output file name as first argument, and the format as wav.

audio.export(output_audio_file, format="wav")

Python Program to Convert MP3 to WAV

Assuming that you installed the pydub and FFmpeg, we shall write a Python program, using the above steps, that converts the given MP3 file input.mp3 to WAV file output.wav.

Python Program

from pydub import AudioSegment

# Convert mp3 to wav                                                            
audio = AudioSegment.from_mp3("input.mp3")
audio.export("output.wav", format="wav")
print('Success. Given audio converted to wav.')

Output

Success. Given audio converted to wav.

A new audio file output.wav should be created.

Summary

In this Python pydub tutorial, we have seen the steps to convert a given audio file from MP3 to WAV format in Python, using pydub library, with example program.

Code copied to clipboard successfully 👍