Get Started

Converting and saving audio files with PyDub

1. Converting and saving audio files with PyDub

Now you've seen how feature rich PyDub is for manipulating audio and you've made some changes to your files, how do you get access to your altered audio files?

2. Exporting audio files

As you might've guessed, PyDub has a built-in method for exporting AudioSegments. Let's see an example. We'll first import our wav file as usual. Then we'll increase its volume by 10 decibels. And then we'll export the louder AudioSegment by calling the export function on it and passing it an output file pathname as well as the format we'd like it to be in. The default format value for the export function is mp3. So it's important to remember to specify if you're after wav files. In our case, we are, to set it to wav. Running this outputs a message letting us know our file has been exported with the specified file pathname.

3. Reformatting and exporting multiple audio files

Working with only one audio file at a time can be slow and cumbersome. You'll likely want to work with and manipulate many audio files at once. For example, say you had a folder of multiple different audio files with various audio extensions such as flac and mp3 but you need them in wav. Rather than working through one by one, you could create a function for doing so. For example, the function make wav takes a folder pathname containing wrongly formatted audio files and a folder pathname to export correctly formatted audio files. Remember, you'll need ffmpeg for anything other than wav. You might start by going through the folder containing your audio files which have the wrong audio extension using Python's scandir function. Then you make sure you're only working with the files that have audio extensions mp3 and flac using endswith, a function which checks the end of a string. If there's a match, you could create a new filename for the file you're working with by splitting the original filename and removing its extension using os path splitext and os path basename. This will leave you with the original file path without the extension, which we can add on the end with the addition operator and wav string. Once you've got a filename to export to, you can use PyDub to import the original audio file and export it with the new filename and the updated wav format. We'll add a print function to check what file we're creating.

4. Reformatting and exporting multiple audio files

You could expand upon this function to manipulate the audio files as you go before exporting them. Let's see an example.

5. Manipulating and exporting

During your exploratory data analysis, you find each of your audio files has 3-seconds of static at the start and are quieter than you'd like. The workflow is much the same as the previous function. Except this time to fix the problem, we start by importing each of our audio files. Then we'll loop through each audio file, remove the first 3-seconds using slicing, then increase the volume of each file by 10 decibels using the addition operator. Once our changes have been made, we'll export the files to the wav format.

6. Manipulating and exporting

Now, our previously unideally formatted audio files are in a much better shape to be transcribed. Of course, based on the problem you're working with, there are other audio altering steps you could implement here, luckily PyDub makes them accessible in a simple manner.

7. Your turn!

Okay, time to put everything you've learned about PyDub together, let's go!