How to Bulk-Convert Audio Files in the Linux Terminal
Audio
Quick Links
-
First, Make Sure You Have FFmpeg
-
Linux Is a Great Platform for Audio Editing
If you’re converting more than a few audio files, using graphical tools (including online tools or apps) can be a laborious process. The instructions below show you how to bulk convert audio files from the Linux terminal, saving you time.
First, Make Sure You Have FFmpeg
FFmpeg is a free command-line tool for processing media files. You can use it to bulk convert audio files to change their format, reduce their size, or make their volume consistent. It’s available in most Linux distributions’ software repositories, and can be installed on Debian/Ubuntu systems using the following command:
sudo apt install ffmpeg
You can also use FFmpeg to convert audio files on Windows.
It’s important that you understand any terminal commands you copy/paste from the internet before you run them. Brief explanations are included below, but make sure you check what each command does before you run it!
Convert Audio File Format
The Linux terminal command below shows how to use FFmpeg to convert a WAV file to MP3:
for file in /path/to/source_directory/*.wav; do ffmpeg -i "$file" "/path/to/output_directory/$(basename "${file%.*}.mp3")"; done
To quickly explain what’s going on here (and in the other examples on this page that use loops to process multiple files):
- A for loop is used to process all the files with a .wav extension in a source directory. Within the loop, the path to each audio file is assigned to the $file variable.
- The $file variable is passed to the ffmpeg command using the -i option which specifies the input file.
- The final parameter passed to FFmpeg is always the output file path. FFmpeg will automatically determine the format of the resulting file based on the extension (in this case, .mp3).
Popular audio file formats supported by FFmpeg for audio conversion are MP3, AAC, OGG, WAV, FLAC, M4A, WMA, and AIFF.
To change the file conversion in the above script, replace .wav with the file extension matching the files you want to convert the format of, and replace .mp3 with a FFmpeg supported format to convert to. Keep in mind that file paths and extensions in Linux are case-sensitive!
Change Audio Bitrate
The below terminal command bulk converts the bitrate of MP3 files to 128k:
for file in /path/to/source_directory/*.mp3; do ffmpeg -i "$file" -b:a 128k "/path/to/output_directory/$(basename "${file%.*}_128k.mp3")"; done
Lowering the bitrate makes audio files smaller, while raising it makes them larger. Note that reducing the bitrate is a one-way process: once you’ve reduced the quality, the information lost cannot be retrieved, and raising the bitrate again will not restore the original quality (though the file may still get bigger, the quality won’t be improved).
Common bitrates used in digital audio are:
- 320k for (almost) CD audio quality
- 256k for high quality
- 192k for standard quality
- 128k for FM radio quality
Bitrates under 128k will really make your audio suffer (and sound like an old phone call), so are only recommended for situations where compressing lots of audio (such as long recordings of conversations) is necessary.
Normalize Audio Volume
Volume normalization makes the audio level of an audio file consistent so that the loud parts of a file aren’t significantly louder than the rest of the recording:
for file in /path/to/source_directory/*.mp3; do ffmpeg -i "$file" -filter:a loudnorm "/path/to/output_directory/$(basename "${file%.*}_normalized.mp3")"; done
The loudnorm filter in FFmpeg is used in the above terminal command to normalize volume.
Convert to Mono/Stereo
One way to further reduce the size of an audio file is to convert it from stereo to mono (provided the content doesn’t need to be in stereo, for example call recordings):
Converting stereo to mono:
for file in /path/to/source_directory/*.mp3; do ffmpeg -i "$file" -ac 1 "/path/to/output_directory/$(basename "${file%.*}_mono.mp3")"; done
Converting mono to stereo:
for file in /path/to/source_directory/*.mp3; do ffmpeg -i "$file" -ac 2 "/path/to/output_directory/$(basename "${file%.*}_stereo.mp3")"; done
Note that, like reducing bitrate, you lose information when converting stereo to mono. The left and right tracks will be merged and won’t be un-merged if you convert the file back to stereo; the mono track will just be duplicated to the right/left channels.
Batch Rename Files
The command below appends the string _renamed to the filename of all MP3 files in a directory:
for file in /path/to/source_directory/*.mp3; do cp "$file" "/path/to/output_directory/$(basename "${file%.*}_renamed.mp3")"; done
If you’re regularly bulk converting or renaming audio files in Linux, you can add these commands to a Bash script for convenient re-use.
Linux Is a Great Platform for Audio Editing
If you’re looking to go beyond simple bulk audio conversion, you can learn how to make your own music in Linux or host your own streaming radio server.