For people without enough knowledge about these formats will always ask which one is better, WAV or MP3? That's because those are the most common formats of audio nowadays that you can find everywhere. WAV is used where uncompromised audio quality is required and MP3 where lightweight music files are needed. You can easily understand it with the example of a song that in WAV format has a size of about 70MB while in MP3 format, it will have a size of up to 5MB only depending on the bitrate.
FFmpeg can be used to convert a huge WAV file into a tiny MP3 file that allows the user to listen to the same song but downloading just a portion of the original size of the WAV file. In this article, I will explain to you how to easily convert a WAV file to MP3 using FFmpeg from the command line.
WAV to MP3 using FFmpeg
As with everything in our blog, you will find the solution right away so you can immediately use it in your own projects. The command to convert WAV to MP3 with a good relation between quality and size can be breakdown like this:
-i: the input WAV file that will be converted to MP3 using the libmp3lame encoder.
-vn: Disable any possible video, just to make sure that there won't be any album cover image attached.
-ar: Set the audio sampling frequency. The most common values for the sampling rate is 8kHz (most common for telephone communications), 44.1kHz (most common for music CDs), and 48kHz (most common for audio tracks in movies).
-ac: Set the number of audio channels. For output streams, it is set by default to the number of input audio channels. For input streams, this option only makes sense for audio grabbing devices and raw demuxers and is mapped to the corresponding demuxer options. So used here to make sure it is stereo (2 channels) instead of mono (1 channel).
-b:a: Converts the audio bitrate to be exact 192kbit per second. If you need constant bitrate (CBR) MP3 audio, you need to use the
-b:aoption instead of
-qscale:a. Here you can specify the number of bits per second, for example,
-b:a 256kif you want 256 Kbit/s (25.6 KB/s) audio. Available options are: 8, 16, 24, 32, 40, 48, 64, 80, 96, 112, 128, 160, 192, 224, 256, or 320 (add a k after each to get that rate). So to get the highest quality setting use
The command to convert the WAV file given the following explanation would be the following one:
ffmpeg -i input-file.wav -vn -ar 44100 -ac 2 -b:a 192k output-file.mp3
The conversion should start and an output similar to the following one will appear:
ffmpeg version 4.3.1-2021-01-01 Copyright (c) 2000-2021 the FFmpeg developers built with gcc 10.2.0 (Rev5, Built by MSYS2 project) libavutil 56. 51.100 / 56. 51.100 libavcodec 58. 91.100 / 58. 91.100 libavformat 58. 45.100 / 58. 45.100 libavdevice 58. 10.100 / 58. 10.100 libavfilter 7. 85.100 / 7. 85.100 libswscale 5. 7.100 / 5. 7.100 libswresample 3. 7.100 / 3. 7.100 libpostproc 55. 7.100 / 55. 7.100 Guessed Channel Layout for Input Stream #0.0 : stereo Input #0, wav, from 'input-file.wav': Metadata: encoded_by : REAPER date : 2020-03-08 creation_time : 16-25-30 time_reference : 1281656 Duration: 00:03:10.31, bitrate: 2116 kb/s Stream #0:0: Audio: pcm_s24le ( / 0x0001), 44100 Hz, stereo, s32 (24 bit), 2116 kb/s Stream mapping: Stream #0:0 -> #0:0 (pcm_s24le (native) -> mp3 (libmp3lame)) Press [q] to stop, [?] for help Output #0, mp3, to 'output-file.mp3': Metadata: TENC : REAPER TDRC : 2020-03-08 time_reference : 1281656 TSSE : Lavf58.45.100 Stream #0:0: Audio: mp3 (libmp3lame), 44100 Hz, stereo, s32p (24 bit), 192 kb/s Metadata: encoder : Lavc58.91.100 libmp3lame size= 4462kB time=00:03:10.32 bitrate= 192.1kbits/s speed=74.8x video:0kB audio:4461kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.016307%
In our case, for a WAV file of about 49MB, the output MP3 file has a size of 4.35 MB only. Doing this with FFmpeg can be useful if you are thinking of the automation of some kind of platform that allows users to upload music in WAV format, but instead of serving the RAW WAV files, the transferred file to listen online will be MP3 instead.
Happy coding ❤️!