Converting Audio File Type in Python

Converting Audio File Type in Python Header

“This audio format is not supported”

Have you ever gotten this error message? That’s what inspired this article. I had a file that I needed to be an mp3 file that was saved as an m4a. I needed the file type to be mp3 to work with it. There were two solutions available to me. I could find a site online, or I could do it myself.

I went with the latter. In this article, we’re going to cover how you can convert different audio types in Python.

What is PyDub AudioSegment?

PyDub is one of a few good Python audio manipulation libraries. The AudioSegment module from PyDub is the most useful module in the library. It provides an all around powerful interface for manipulating your audio data. You can use AudioSegment to clip audio data, to play with the volume, change frame rates, and much more. 

Most relevant to us at the moment, you can use PyDub AudioSegment to convert audio file types. Before we dive into the code, make sure that you have the PyDub library installed via pip install pydub. If you are using Anaconda, you should be able to install it with conda install pydub.

Convert Audio File Types with Python

The code to convert audio file types with Python is incredibly easy to implement with PyDub. We start off our code by importing the AudioSegment module from PyDub. Then, we write a simple function that converts an audio file from one format to another.

This function needs three parameters. It needs to know the name of the file, the original format of the audio file, and the desired format that we want to convert it to. I’ve also added a short documentation blurb in the code below to describe the parameters. There’s no return value here, we’re not going to return the audio file, we’re just going to save it as the desired file type.

The actual audio file type conversion only takes two lines of Python. Isn’t that great? The first line creates an AudioSegment object using from_file, which takes two parameters. We need to pass the name of the file (including the file type), and the format that the file is in.

Now that we have a PyDub AudioSegment object, all we do is call the export function on it. The export function takes two parameters. We need to pass it the filename with the format that we want to convert to and the file format type as a string. That’s it. That’s all there is to creating a function that converts audio file types in Python.

from pydub import AudioSegment
 
def convert(filename: str, from_format: str, to_format: str):
   '''Converts audio file from one format to another and exports it
  
   Params:
       filename: name of original file
       from_format: format of og audio file
       to_format: desired format'''
   raw_audio = AudioSegment.from_file(f"{filename}+{from_format}", format=from_format)
   raw_audio.export(f"{filename}+{to_format}", format=to_format)

Converting an M4A to an MP3 in Python

Before we wrap up, let’s take a look at what it looks like to use this function to convert an m4a file to an mp3 file. Just like the actual function to convert audio file type, calling the function is incredibly easy. For our example, we’ll use this audio file. This is an m4a type audio file that I recorded.

To convert this to an mp3 file, all we do is call the convert function and pass the three parameters we declared: the name, the original audio file type, and the target file type. Once we call this function, we should see a new file in our folder – cows_crows.mp3.

# to run:
convert("cows_crows", "m4a", "mp3")

Summary of Converting Audio File Type in Python

In this tutorial, we learned how to convert audio file types with Python. First we looked at the PyDub function and the AudioSegment module from it. Then we created a function that took three parameters – a file name, the original audio file type, and the target audio file type. Finally, we used that function to convert an audio file as an example.

Further Reading

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

¤5.00
¤15.00
¤100.00
¤5.00
¤15.00
¤100.00
¤5.00
¤15.00
¤100.00

Or enter a custom amount


Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly
Yujian Tang

Leave a Reply

%d