If you want to convert FLAC to MP3, FFmpeg is an extremely handy tool. It may seem complex initially, but it’s fairly straightforward once you grasp the concepts. In this post, I’ll guide you through using FFmpeg to convert FLAC to MP3.
The
Free Lossless Audio Codec (FLAC)
is an open-source audio compression format known for its lossless compression, which means it preserves audio quality while reducing file size. It differs from lossy codecs like
MP3
and
AAC
, which discard audio information to achieve greater compression.
FLAC is highly appreciated among audiophiles and music enthusiasts who prioritize high fidelity in audio files. The format supports metadata tagging, album cover art, and fast seeking, which enhances its usability. For a more in-depth understanding of FLAC, you can refer to the
official FLAC documentation
or
Hydrogenaudio’s FLAC page
, which offers comprehensive insights.
For now, let’s proceed to the code!
Table of Contents
Command to Convert FLAC to MP3 using FFmpeg
In its most basic form, the command to convert a FLAC to MP3 using FFmpeg is as follows:
ffmpeg -i input.flac -ab 192k output.mp3
Here,
-i input.flac
specifies the input file,
-ab 192k
sets the audio bitrate, and
output.mp3
is the output file.
Let’s break down each parameter:
-i
: This stands for the input file.
input.flac
: This is the FLAC file we want to convert. Replace it with the name of your file.
-ab
: This sets the audio bitrate. In the example command, we’ve set it to 192k, which generally provides a good balance between file size and audio quality. The value can be adjusted based on your needs.
output.mp3
: This is the output file’s name (and path, if necessary). FFmpeg will automatically detect the desired format from the file extension.
Further Parameter Adjustments
While the basic command is often sufficient, there are other parameters you can tweak to customize the output to your exact needs.
-ac
: This parameter sets the number of audio channels.
2
stands for stereo, while
1
would specify mono. Go here to learn more about
mono and studio channels in audio
.
-ab
: This sets the audio bitrate. In the example command, we’ve set it to 320k.
-ar
: This sets the audio sampling frequency. For most applications,
48000
or
44100
Hz will suffice.
-vn
: This parameter is used when you want to skip any video stream that might be present in the input file.
-y
: This option is to overwrite the output file if it already exists.
That’s it. This simple FFmpeg one-liner allows you to easily convert, re-encode, and manipulate your FLAC to MP3 file format. The next few sections’ll look at advanced operations, such as batch conversion and metadata preservation.
If you have a folder full of FLAC files that you want to convert to MP3, here’s how to do it in one shot:
for file in *.flac; do ffmpeg -i "$file" -ab 192k "${file%.flac}.mp3"; done
This command loops over all the FLAC files in the current directory and converts them one by one to MP3.
${file%.flac}.mp3
is a neat bit of shell scripting that strips the
.flac
extension from the input file name and replaces it with
.mp3
for the output file name.
Remember to replace the parameters we learned in the previous sections with the batch conversion script if you want to vary the audio frequency, bitrate, channels, etc.
Finally, let’s take a look at audio metadata preservation.
Preserving Metadata During Conversion
One concern when converting audio files is the preservation of metadata.
ID3 tags are a form of metadata container
used with the MP3 audio file format. They allow information such as the title, artist, album, track number, or other details about the file to be stored in the file itself.
Two versions of ID3 tags are commonly used: ID3v1 and ID3v2. Each has different attributes and capabilities.
ID3v1
: This version of ID3 is quite limited and can only hold a small amount of data (128 bytes). The categories available for data are very specific (title, artist, album, year, comment, and genre), and each category can only hold a limited number of characters. It’s typically found at the end of the file.
ID3v2
: This is a more advanced format. It can include a wide variety of metadata, including album art, BPM, lyrics, and more detailed information about the track. The data is stored at the beginning of the file.
To keep metadata during the conversion, use the
-map_metadata 0
option:
-map_metadata 0
instructs FFmpeg to copy metadata from the first input file to the output file.
-id3v2_version 3
: This option sets the version of ID3v2 to be used. In this case, it’s set to 3 (ID3v2.3). The ID3v2.3 is the most widely supported version, thus it’s commonly used to ensure maximum compatibility with various media players.
-write_id3v1 1: FFmpeg will also write ID3v1 tags when adding this parameter. This is useful because although ID3v1 tags are less capable, they are widely supported due to their longevity and simplicity. Adding them and ID3v2 tags can help ensure metadata is accessible to many players.
To fully comprehend the use of
-id3v2_version 3
and
-write_id3v1
, it’s necessary to have some understanding of what ID3 tags are.
By including these parameters, you’re ensuring the metadata from your original FLAC file is preserved and broadly accessible in your new MP3 file. This can be particularly helpful if you have a large music library and rely on metadata to organize and navigate your collection.
Conclusion
FFmpeg offers a powerful and flexible way to convert FLAC to MP3. FFmpeg can handle everything from basic single-file conversions to batch processes and metadata preservation.
I hope you found this tutorial useful. If you did, please share it with your friends and colleagues 🙂
Krishna Rao Vijayanagar, Ph.D., is the Editor-in-Chief of OTTVerse, a news portal covering tech and business news in the OTT industry.
With extensive experience in video encoding, streaming, analytics, monetization, end-to-end streaming, and more, Krishna has held multiple leadership roles in R&D, Engineering, and Product at companies such as
Harmonic Inc.,
MediaMelon
, and Airtel Digital. Krishna has published numerous articles and research papers and speaks at industry events to share his insights and perspectives on the fundamentals and the future of OTT streaming.