Convert mp3 to wav using FFmpeg for VBR
What command should I use to convert a mp3
file to wav
whose bitrate is variable. Or better how would I know whether that source audio is fixed
bitrate or variable
?
Solution 1:
You can get some info about the bitrate of your input files by using the ffprobe song.mp3
command. However this only tells you bitrate of the first frame. VBR in MP3 files is usually implemented simply by changing the bitrate for each frame, so whether it's being used can't be determined by just reading the header of first frame. I usually use some other audio file player software to determine whether VBR is being used, as many will display that (Foobar2000 does for example).
When you use lossy output codecs (such as MPEG-1 Layer III or AAC), ffmpeg chooses a default bitrate for the output stream, or a variable bitrate. It depends on the encoder itself.
For lossless codecs, you cannot set a variable bitrate, since each sample takes a predefined number of bits. ffmpeg -i song.mp3 song.wav
will therefore get you a PCM-encoded WAV file with 44,100 Hz sample rate and 16 bits per sample. This results in about 1411 kBit/s for the entire container, likely much, much bigger than the MP3 input file.
If you want a smaller file size for the PCM-encoded WAV file, set a sample format with less bit depth (see -encoders
option for a complete list of them) and/or choose a lower sample rate (-ar 22050
would use 22.05 kHz for example).
Here's an example of doing both:
ffmpeg -i song.mp3 -acodec pcm_u8 -ar 22050 song.wav
Solution 2:
According to @naught101 comment, I would follow this process to detect the best acodec
:
$ ffmpeg -formats | grep PCM
DE f32be PCM 32-bit floating-point big-endian
DE f32le PCM 32-bit floating-point little-endian
DE f64be PCM 64-bit floating-point big-endian
DE f64le PCM 64-bit floating-point little-endian
DE s16be PCM signed 16-bit big-endian
DE s16le PCM signed 16-bit little-endian
DE s24be PCM signed 24-bit big-endian
DE s24le PCM signed 24-bit little-endian
DE s32be PCM signed 32-bit big-endian
DE s32le PCM signed 32-bit little-endian
DE u16be PCM unsigned 16-bit big-endian
DE u16le PCM unsigned 16-bit little-endian
DE u24be PCM unsigned 24-bit big-endian
DE u24le PCM unsigned 24-bit little-endian
DE u32be PCM unsigned 32-bit big-endian
DE u32le PCM unsigned 32-bit little-endian
At this point, consider your platform to choose between big-endian
, little-endian
, the choose bitrate:
$ ffmpeg -i sample.mp3
[mp3 @ 0x7fb33180da00] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'sample.mp3':
Metadata:
title : Saturday
artist : Winterwood
album : Love In The Heart
track : 2/15
TPA : 1/1
encoded_by : iTunes 11.0.1
genre : Country & Folk
date : 1997
Duration: 00:04:27.76, start: 0.000000, bitrate: 128 kb/s
Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb/s
We can see that this audio file is a mp3
(this is not obvious, despite of the extension of the input file, just check the bytes) has a bitrate of 128 kb/s
, the codec was s16p
, sampling at 44100 Hz
, so we choose accordingly:
$ ffmpeg -i sample.mp3 -acodec pcm_s16le -ar 44100 sample.wav
Input #0, mp3, from 'sample.mp3':
Metadata:
title : Saturday
artist : Winterwood
album : Love In The Heart
track : 2/15
TPA : 1/1
encoded_by : iTunes 11.0.1
genre : Country & Folk
date : 1997
Duration: 00:04:27.76, start: 0.000000, bitrate: 128 kb/s
Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb/s
File 'sample.wav' already exists. Overwrite ? [y/N] y
Output #0, wav, to 'sample.wav':
Metadata:
INAM : Saturday
IART : Winterwood
IPRD : Love In The Heart
IPRT : 2/15
TPA : 1/1
ITCH : iTunes 11.0.1
IGNR : Country & Folk
ICRD : 1997
ISFT : Lavf56.4.101
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s
Metadata:
encoder : Lavc56.1.100 pcm_s16le
Stream mapping:
Stream #0:0 -> #0:0 (mp3 (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
size= 46125kB time=00:04:27.75 bitrate=1411.2kbits/s
video:0kB audio:46125kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000457%
If you are on mac you can that check the audio if it has no white noise
at the beginning / end (when the mp3 original audio has no audio for some msec/sec):
$ afplay sample.wav
and of course you can double check the bitmap:
$ ffmpeg -i sample.wav
Input #0, wav, from 'sample.wav':
Metadata:
artist : Winterwood
date : 1997
genre : Country & Folk
title : Saturday
album : Love In The Heart
track : 2/15
encoder : Lavf56.4.101
encoded_by : iTunes 11.0.1
Duration: 00:04:27.76, bitrate: 1411 kb/s
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, 2 channels, s16, 1411 kb/s
Solution 3:
Use ffmpeg to convert the media, first check file specification using ffprobe.
use this command
ffmpeg -i kimberly.wav -acodec pcm_s16le -ar 16000 -ac 1 song.wav
where
-pcm_s16le is codec 16 bit conversion
-ar is sampling rate (16000samples/sec)
-ac no of audio channel