102
votes

How can I convert .flac to .mp3 with ffmpeg, keeping all metadata (that is converting Vorbis comment in .flac files to ID3v2 metadata of .mp3)?

8
Please note that for next time you should ask ffmpeg cli usage questions at Super User. Stack Overflow is only for questions involving programming. Also, FLAC officially supports Vorbis comment only, so I assume you want to convert to ID3v2 instead of "keep".llogan
Ok, I fixed my question. I asked here because I saw questions like this on StackOverflow (e.g. this or this), and assumed it could be interesting to share my knowledge. Also, I see that there is a ffmpeg tag, so why don't share my solution here? ffmpeg is used in a lot of application by a lot a lot of developers.Vito Gentile
The ffmpeg tag says, "Questions about interactive use of the FFmpeg command line tool should be asked on Super User.". It's not a big deal, but I just wanted to point it out since there are too many off-topic questions here in my opinion.llogan

8 Answers

172
votes

The following command keeps high quality on .mp3 (320 kbps), and metadata from .flac file are converted to ID3v2 format, which can be included in .mp3 files:

ffmpeg -i input.flac -ab 320k -map_metadata 0 -id3v2_version 3 output.mp3
62
votes

Perfect answer above. I use it together with find to add all FLAC files in a subtree to iTunes with this command

find . -name "*.flac" -exec ffmpeg -i {} -ab 160k -map_metadata 0 -id3v2_version 3 {}.mp3 \;

To automatically add the resulting files to iTunes, get the iTunes import directory with

find ~/Music/ -name "Automatically Add*"

result e.g.

/Users/sir/Music//iTunes/iTunes Media/Automatically Add to iTunes.localized

Then run e.g.

find . -name "*.mp3" -exec mv {} "/Users/sir/Music//iTunes/iTunes Media/Automatically Add to iTunes.localized/" \;

To automatically add all the converted tracks to iTunes.

16
votes

If you want to save a little space, try the recommendation of hydrogenaud.io:

Very high quality: HiFi, home, or quiet listening, with best file size -V0 (~245 kbps), -V1 (~225 kbps), -V2 (~190 kbps) or -V3 (~175 kbps) are recommended. These VBR settings will normally produce transparent results. Audible differences between these presets may exist, but are rare.

Source: http://wiki.hydrogenaud.io/index.php?title=LAME

If you want use this option in ffmpeg, you should use the -q:a 0 alias.

Control quality with -qscale:a (or the alias -q:a). Values are encoder specific, so for libmp3lame the range is 0-9 where a lower value is a higher quality. 0-3 will normally produce transparent results, 4 (default) should be close to perceptual transparency, and 6 produces an "acceptable" quality. The option -qscale:a is mapped to the -V option in the standalone lame command-line interface tool.

Source: https://trac.ffmpeg.org/wiki/Encode/MP3

If you want ID3v1 metatags too, you should add the -write_id3v1 1 parameter.

So my final command is:

ffmpeg.exe -y -i input.flac -codec:a libmp3lame -q:a 0 -map_metadata 0 -id3v2_version 3 -write_id3v1 1 output.mp3
9
votes

I was testing the following command to convert infile.flac file to outfile.mp3:

ffmpeg  -i infile.flac  -q:a 0  outfile.mp3

As of Ubuntu 16.04, the above command seems to copy (most of? all of?) the metadata.

-q:a 0 tells ffmpeg to use the highest quality VBR.

However, ffmpeg was transcoding my album art from jpeg to png, which increased the size of the cover art.

Stream mapping:
  Stream #0:1 -> #0:0 (mjpeg (native) -> png (native))
  Stream #0:0 -> #0:1 (flac (native) -> mp3 (libmp3lame))

(I guess the above conversion sort of makes sense given how ffmpeg works.)

After some digging, I found the -c:v copy option, which specifies that the video stream should be copied, rather than transcoded. The full command is:

ffmpeg  -i infile.flac  -c:v copy  -q:a 0  outfile.mp3

The above command results in:

Stream mapping:
  Stream #0:1 -> #0:0 (copy)
  Stream #0:0 -> #0:1 (flac (native) -> mp3 (libmp3lame))
5
votes

One-liner to convert all .flac files to .mp3 in a single directory, keeping most metadata:

for file in *.flac; do ffmpeg -i $file -q:a 0 ${file:r}.mp3; done

(Note: ${file:r} removes the extension of the given filepath)

ffmpeg: flac to mp3

3
votes

To recursively convert in mp3 all the flac files in nested folders, I used this command:

find '~/Music/' -iname '*.flac' -exec bash -c 'D=$(dirname "{}"); B=$(basename "{}"); mkdir "$D/mp3/"; ffmpeg -i "{}" -ab 320k -map_metadata 0 -id3v2_version 3 -acodec libmp3lame "$D/mp3/${B%.*}.mp3"' \;

It will create a folder named "mp3" inside the one with flac files and, inside the mp3 folder, it will save relative mp3 files with a bitrate of 320kbps, without keeping the old file extension in the name.

2
votes

I know that this was not asked, but considering that one of the reasons that this is done (at least that's what I wanted to do) is so that the music can be imported into Apple iTunes which doesn't support FLAC. In such case it makes more sense to convert FLAC to Apple's own lossless format, m4a. I used this command to convert all the files in the current folder, while retaining similar file sizes.

find . -name "*.flac" -exec ffmpeg -i {} -map_metadata 0 -acodec alac {}.m4a \;

2
votes

This flac2mp3.sh script uses ffmpeg to convert a folder tree of FLAC files into another folder tree of MP3 files. Cover art is included, when present. You can set a CORES variable to create background jobs to convert several files at a time.