Convert audio file formats using Python
• • Reading time: 1 minute
Last updated:
I work closely with auditory stimuli. Sometimes I need to convert a large number of audio files from one format to another, usually because different experiment software works better with different audio formats. Here I share my Python script that converts all audio files in a folder from one format to another.
Step 1: Preparation
Install ffmpeg. Remember to put ffmpeg in your PATH.
Install Python (obviously) and pydub: pip install pydub
.
Download the Python script. Gather all your files in a folder.
Step 2: Run the script
Open convert_audio.py
in an IDLE. Modify these variables:
- AudioSegment.converter: string. Path to your
ffmpeg.exe
. - AudioSegment.ffmpeg: string. Path to your
ffmpeg.exe
. -
AudioSegment.ffprobe: string. Path to your
ffprobe.exe
. - dataDir: string. Input folder. Where your original audio files are located.
- outDir: string. Output folder. Where you want your converted files.
- input_format: string. Format of your original audio files. Please include ‘.’.
- output_format: string. Format of your output audio files. Please include ‘.’.
Run the script. Pydub is going to call ffmpeg to convert all files in your chosen folder with the original extension to the output format.