Get Started

Exporting and reformatting audio files

If you've made some changes to your audio files, or if they've got the wrong file extension, you can use PyDub to export and save them as new audio files.

You can do this by using the .export() function on any instance of an AudioSegment you've created. The export() function takes two parameters, out_f, or the destination file path of your audio file and format, the format you'd like your new audio file to be. Both of these are strings. format is "mp3" by default so be sure to change it if you need.

In this exercise, you'll import this .mp3 file (mp3_file.mp3) and then export it with the .wav extension using .export().

Remember, to work with files other than .wav, you'll need ffmpeg.

This is a part of the course

“Spoken Language Processing in Python”

View Course

Exercise instructions

  • Import mp3_file.mp3 and save it to mp3_file.
  • Export mp3_file with the file name mp3_file.wav with "wav" format.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

from pydub import AudioSegment

# Import the .mp3 file
mp3_file = AudioSegment.from_file(____)

# Export the .mp3 file as wav
mp3_file.____(out_f=____,
                format=____)
Edit and Run Code

This exercise is part of the course

Spoken Language Processing in Python

AdvancedSkill Level
4.7+
3 reviews

Learn how to load, transform, and transcribe speech from raw audio files in Python.

Not all audio files come in the same shape, size or format. Luckily, the PyDub library by James Robert provides tools which you can use to programmatically alter and change different audio file attributes such as frame rate, number of channels, file format and more. In this chapter, you'll learn how to use this helpful library to ensure all of your audio files are in the right shape for transcription.

Exercise 1: Introduction to PyDubExercise 2: Import an audio file with PyDubExercise 3: Play an audio file with PyDubExercise 4: Audio parameters with PyDubExercise 5: Adjusting audio parametersExercise 6: Manipulating audio files with PyDubExercise 7: Turning it down... then upExercise 8: Normalizing an audio file with PyDubExercise 9: Chopping and changing audio filesExercise 10: Splitting stereo audio to mono with PyDubExercise 11: Converting and saving audio files with PyDubExercise 12: Exporting and reformatting audio files
Exercise 13: Manipulating multiple audio files with PyDubExercise 14: An audio processing workflow

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free