Explore

Close
Python Programs Share

Convert Wav To Mp3 In Python With Code

Convert Wav To Mp3 In Python With Code

Looking for a way to convert wav to mp3 in python programming then you are at the right place as you can see the title today I will show you how to convert wav to mp3 in python.

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 files.

An uncompressed audio file is large in size so in order to make the file size small we use mp3 audio files, so we will see how python convert wav to mp3 format in this tutorial.

How to convert Wav To Mp3 Python Tutorial

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

So in order to convert Wav to Mp3 in python, 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")

Copy the above python code and paste it in the python file. So 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.


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

You can copy and paste your wav file in the project directory and just enter the full name of the wav file in the src variable.

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


dst = "output.mp3"

It will keep the converted mp3 file in the project directory, so now let’s run and test this program. If you don’t have a wav file here you can download a sample wav audio file and paste this file in your project directory.

Enter the name in the src variable as the name of the wav file 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.

Summary

This was the tutorial on converting wav to mp3 in python programming. I hope you now know how to convert wav files to mp3 file format in python programming.

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 🙂