147

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)?

Vito Gentile
  • 13,336
  • 9
  • 61
  • 96
  • 8
    Please note that for next time you should ask `ffmpeg` cli usage questions at [su]. [so] is only for questions involving programming. Also, FLAC [officially supports Vorbis comment](http://xiph.org/flac/format.html#format_overview) only, so I assume you want to convert to ID3v2 instead of "keep". – llogan Sep 29 '14 at 23:31
  • 6
    The [tag: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 Sep 30 '14 at 17:09

8 Answers8

248

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
Vito Gentile
  • 13,336
  • 9
  • 61
  • 96
  • 2
    It's worth noting that "ffmpeg" may need to be replaced by "avconv". – shockburner Apr 25 '16 at 22:28
  • 11
    FWIW `ffmpeg` 3.2 automatically copies metadata into ID3v2 from FLAC without needing to specify `-map_metadata 0 -id3v2_version 3`, though it doesn't copy into ID3v1 tags. I use a separate tool for that. – Dai Nov 26 '16 at 23:43
  • 2
    lyrics still don't get copied though – user151496 Oct 14 '17 at 23:22
  • 1
    Does it divide flac if there are several parts? – Artyom Dec 04 '17 at 16:29
  • 1
    @Artyom You should probably have the *cue* file along with your *flac* where the split points are marked. If so then you can use **shnsplit** (console) or **flacon** (GUI) to split your file and convert the parts to mp3. If you only have a single *flac* you can either write a *cui* for it or use ffmpeg's *-ss* and *-t* options to cut each song out. –  Feb 26 '18 at 13:30
  • 4
    For loop conversion (.bat script for Windows): `for %%f in (*.flac) do ( ffmpeg -i "%%f" -ab 320k -map_metadata 0 -id3v2_version 3 "%%f.mp3" )` – deadfish Nov 28 '19 at 19:58
  • just a heads up, claiming that 320k is "high quality" is not good practice according to https://trac.ffmpeg.org/wiki/Encode/MP3#CBREncoding :) – genuinefafa May 06 '20 at 11:51
  • 5
    powershell to do this for the directory `dir *.flac | foreach {ffmpeg -i $_.FullName -ab 320k -map_metadata 0 -id3v2_version 3 $_.FullName.Replace('flac', 'mp3')}` – Jared Beach Sep 14 '20 at 15:19
  • shell version of deadfish's oneliner `for f in *.flac; do ffmpeg -i "$f" -ab 320k -map_metadata 0 -id3v2_version 3 "${f%.flac}.mp3"; done` or if you want to put it in a separate folder `for f in *.flac; do ffmpeg -i "$f" -ab 320k -map_metadata 0 -id3v2_version 3 "mp3/${f%.flac}.mp3"; done` – Kellei May 22 '22 at 13:22
87

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.

user2707001
  • 1,543
  • 12
  • 13
22

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
wiktor
  • 1,605
  • 16
  • 13
13

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))
mpb
  • 1,277
  • 15
  • 18
  • all these years later, and I only just found this little nugget.... `-q:a 0` and `-c:v copy` help a great deal.. thanks for that. :) – Andre Greeff Nov 04 '22 at 18:24
11

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.

Riccardo Volpe
  • 1,471
  • 1
  • 16
  • 30
  • 1
    Big thanks. FYI, your answer credited in an article: https://quantixed.org/2021/11/20/convertible-using-ffmpeg-to-convert-audio-files. – gustafbstrom Feb 09 '22 at 08:59
10

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

woudsma
  • 129
  • 2
  • 4
3

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.

Rick O'Sullivan
  • 351
  • 3
  • 7
2

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 \;

Dhiraj Gupta
  • 9,704
  • 8
  • 49
  • 54