How to convert flac to mp3 with ffmpeg

 Thu, 18 Apr 2024 09:12 UTC

How to convert flac to mp3 with ffmpeg
Image: CC BY 4.0 by cybrkyd


Here is how to convert flac audio files to mp3 with ffmpeg whilst retaining all the metadata.

To convert a single file:

$ ffmpeg -i flacfile.flac -ab 320k -map_metadata 0 -id3v2_version 3 mp3file.mp3

The above command will output the mp3 file at 320 Kbps and map the original metadata, converting it to ID3v2 format.

VBR / CBR

The command above will output the mp3 files at a CBR (Constant Bit Rate) of 320 Kbps. Also, any JPEG image art will be converted to PNG, further increasing the file size of the mp3.

To use the highest quality VBR (Variable Bit Rate) and avoid image art conversion, the following command can be used to convert a single file:

$ ffmpeg -i flacfile.flac -c:v copy -q:a 0 mp3file.mp3

To use CBR instead anyway, substitute the quality setting:

$ ffmpeg -i flacfile.flac -c:v copy -ab 320k mp3file.mp3

Batch conversion

To convert multiple files, the following command can be used for files in the same directory:

$ find -name "*.flac" -exec ffmpeg -i {} -c:v copy -ab 320k {}.mp3 \;

And for VBR:

$ find -name "*.flac" -exec ffmpeg -i {} -c:v copy -q:a 0 {}.mp3 \;

To convert multiple files and save the mp3 files to a new directory called ‘mp3’:

$ mkdir mp3 && find -name "*.flac" -exec ffmpeg -i {} -c:v copy -q:a 0 mp3/{}.mp3 \;

Cleaning up the file name ‘.flac.mp3’

With the batch conversion commands, the resulting mp3 files retain the original .flac extension.

$ ls
'12 Read Em And Weep - The Black Keys.flac'
'12 Read Em And Weep - The Black Keys.flac.mp3'

If you are a bit OCD like me and it really bothers you, tidy up the file names by using rename.

$ rename 's/.flac//' *.mp3