Convert .flac to .mp3 with ffmpeg, keeping all metadata
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))
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
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