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 Answers
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.
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
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))
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.
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 \;
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.
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