10.5 Audio Formats Convert

20210607

A simple way to convert between audio formats is to use ffmpeg. Whilst this is a video processing tool it also handles audio, of course, since most video includes audio. The same commands are also useful to extract just the audio track from a video.

To convert a mp3 file to wav:

ffmpeg -i myaudio.mp3 myaudio.wav

If the resulting file is too large, then perhaps it’s sample rate is too high. We can fix that. First use du to determine the file size. And then check the file properties using file:

$ du -h myaudio.mp3
95M     myaudio.mp3

$ file myaudio.mp3
myaudio.mp3: Audio file with ID3 version 2.4.0,...:MPEG ADTS, layer III, v1, 64 kbps, 48 kHz, Stereo

Now convert that mp3 file to wav using ffmpeg and check its properties:

$ ffmpeg -i myaudio.mp3 myaudio.wav

$ du -h myaudio.wav
1.2G    myaudio.wav

$ file myaudio.wav
myaudio.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, stereo 48000 Hz

We can choose a smaller audio frame rate for a smaller file:

$ ffmpeg -i myaudio.mp3 -ar 16000 myaudio.wav

$ du -h myaudio.wav
380M    myaudio.wav

$ file myaudio.wav
myaudio.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, stereo 16000 Hz

We can also convert it as mono to half the size:

$ ffmpeg -i myaudio.mp3 -ac 1 -ar 16000 myaudio.wav

$ du -h myaudio.wav
190M    myaudio.wav

$ file myaudio.wav
myaudio.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 16000 Hz


Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0