Python Conversion Programs

Convert Wav To Mp3 In Python With Code

Last updated January 31, 2024 by Jarvis Silva

Looking for a way to convert wav to mp3 in python programming then you are at the right place today in this article we do exactly that.

Wav is an audio file format which was developed by IBM and Microsoft. It is used for storing audio on computers. It is the main format used in windows, which are uncompressed audio file so in order to make the file size small we use mp3 audio files, so we will see how to do this in python.

How to convert Wav To Mp3 Using Python

Converting wav to mp3 format looks difficult but thanks to python libraries, a great advantage of python is that it has tons of libraries, which makes programming in just 5 to 10 lines of code.

So to create this program we will use the pydub python library, Pydub is an audio library which is used to manipulate audio in a simple and easy high level interface.

Install and setup python

Our first step is to install and set up python on your computer. You can skip this if you have python installed on your computer. You can download python from the official python website.

After that, you can start the python installer and complete the setup. Remember to select add python to path while installing. After installing, create a folder and open the folder in a code editor.

I use visual studio code editor. You use any editor. If you want to use vs code you can download it from here: Download VS Code Editor.

Install Pydub Library

Now you need to install the pydub python library to install the library, open up a terminal or command prompt at the project folder location and paste the below command.

pip install pydub

The above command will install pydub in your project. It is a very small library. It should install it quickly after installing, create a python file and get ready to write some code.

Copy and paste the wav to mp3 python code.

from os import path
from pydub import AudioSegment

# Enter the filename you want to convert it should in the same folder as this python file
src = "sample.wav" 
dst = "output.mp3"

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

print("Converted Successfully")

Now you have the code but before you run the program, you need to enter the name and path of the wav file you want to convert in the “src” variable in the code, move your wav file in the project directory and just enter the full name of the wav file in the src variable.

# Enter the filename you want to convert it should in the same folder as this python file
src = "sample.wav" 

You can change the destination of the mp3 file which will get converted from wav format or you can just keep like this

dst = "output.mp3"

It will store the converted mp3 file in the project directory, so now let’s run and test this program, so after running this python program you should see a “Converted Successfully” message in your terminal like this

Converted Successfully

If you see any error message like this “RuntimeWarning: Couldn’t find ffmpeg or avconv – defaulting to ffmpeg, but may not work” then just ignore it.

This was the short tutorial on converting wav file to mp3 file format using python programming, I hope you found this tutorial helpful.

Here are some more python guides you may find helpful:

I hope you found what you were looking for and if you want more python tutorials like this do join our Telegram channel for future tutorials update.

Thanks for reading, have a nice day 🙂