Convert FLAC to MP3 with FFmpeg Bash Loop

Converting a batch of FLAC audio files to MP3 format on Linux can be efficiently automated using a simple Bash for loop combined with FFmpeg. This method allows you to process an entire folder of music quickly, preserving metadata like titles and artists while optimizing the files for broader device compatibility. By utilizing the terminal, you avoid the need for heavy graphical software and gain precise control over the output audio quality.

Before running the command, you need to ensure you have FFmpeg installed on your Linux system. You can verify this or install it using your distribution's package manager, such as sudo apt install ffmpeg on Debian/Ubuntu-based systems or sudo dnf install ffmpeg on Fedora.

The Core Bash Command

To convert all FLAC files in your current working directory to high-quality MP3s, open your terminal and run the following single-line command:

for file in *.flac; do ffmpeg -i "$file" -qscale:a 0 "${file%.flac}.mp3"; done

Breaking Down the Loop

Understanding how this command works makes it easier to customize for your specific needs:

  • for file in *.flac; do ...; done: This initializes the Bash loop. It looks at every file in the current directory ending in .flac and temporarily assigns its filename to the variable $file during each iteration.
  • ffmpeg -i "$file": This calls FFmpeg and uses the -i flag to specify the input file, which is the current FLAC file wrapped in quotes to handle any spaces in the filename safely.
  • -qscale:a 0: This flag sets the audio quality using LAME's variable bitrate (VBR). A value of 0 represents the highest possible VBR quality (typically resulting in a bitrate between 220-260 kbps), which preserves excellent audio fidelity while reducing file size.
  • "${file%.flac}.mp3": This is a Bash parameter expansion. It takes the original filename, strips the .flac extension from the end, and appends .mp3 instead. This ensures your output file isn't accidentally named song.flac.mp3.

Alternative: Using Constant Bitrate (CBR)

If you prefer a constant bitrate over a variable one—for instance, a standard 320 kbps encode—you can modify the audio codec flags slightly:

for file in *.flac; do ffmpeg -i "$file" -b:a 320k "${file%.flac}.mp3"; done

In this variation, -b:a 320k explicitly tells FFmpeg to force a constant audio bitrate of 320 kilobits per second for the output MP3 files.