How to Convert MP3 to WAV in Python?


Many times, we have the need to convert audio files from one format to another. For example, when dealing with streaming services or other applications that require a certain audio file format, you may need to take your MP3 files and convert them into WAV files. This can be accomplished easily using the Python programming language and two readily available modules.

To start, you’ll need to have Python 3 installed on your computer. If you don’t already have it, you can download it for free from the Python website.

Next, you’ll need to install pydub module. This module can be installed using pip, the Python package manager.

pip install pydub

Once the module is installed, open up a new Python file and import the necessary module.

import pydub
Code language: JavaScript (javascript)

f you have not installed ffmpeg yet, install it. You can use your package manager to do that.

For Ubuntu / Debian Linux:

apt-get install ffmpeg
Code language: JavaScript (javascript)

or

pip install ffmpeg

Converting an MP3 File to a WAV File

We’re now ready to start converting our files. We’ll begin by importing the required modules:

from pydub import AudioSegment
Code language: JavaScript (javascript)

# Open the audio file as an MP3

song = AudioSegment.from_mp3("input.mp3")
Code language: JavaScript (javascript)

# Convert the MP3 file into a WAV file

wav_file = song.export("output.wav", format="wav")
Code language: JavaScript (javascript)

A Full Python Script

from pydub import AudioSegment # files src = "transcript.mp3" dst = "output.wav" # convert wav to mp3 sound = AudioSegment.from_mp3(src) sound.export(dst, format="wav")
Code language: PHP (php)

How to run

$python convert.py
Code language: PHP (php)

And that’s it! You’ve now successfully converted your MP3 file into a WAV file using Python.

Converting audio files from one format to another is a common task that is often required by streaming services or other applications. Luckily, it’s a relatively simple process that can be accomplished using nothing more than Python.

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts