How to Convert Flac Music Files to MP3s

botond published Jan. 2020, 12, 30:23 p.m. time

Content

 

Introductory

Not always when downloading music mp3 format for music files, but for example flacor other formats. In this case, it is advisable to convert them to mp3 so that it is compatible with all players and the file size is optimal. I've made a description of that before how to convert m4a music files to mp3, and in this description we convert from flac format to mp3 using several methods on Debian / Ubuntu systems.

The flac (Free Lossless Audio Codec) is a free - to - use, lossless audio code that provides lossless audio compression, resulting in much better compression results than, for example, a ZIP, GZIP compression. The essence of lossless compression is that the conversion process is reversible, i.e. we can recover the original audio material at any time. The mp3, on the other hand, offers lossy compression - from which the exact original audio file can no longer be recovered, but only in approximate quality - in exchange for a fraction of the original file size. Of course, even with such compression, mp3 knows such good quality that it holds its place in studios, which is why it has become so popular and widespread. So if you want to save storage space with almost the same sound quality, you should definitely convert your music to mp3 format.

 

 

Preparing songs to convert

Before we get started, let’s compile our music files that we want to convert to mp3. Put them in a separate directory so you can convert them more conveniently later, even in batches.

In this example, I will illustrate this process with Terminator 2 soundtracks in flac format, which I compiled into a separate library:

As you can see, the songs in the movie take up 2GB of space, which is pretty much it.

Clean up filenames

It's a good idea to drop a word about filenames, as shown in the picture, filenames can contain spaces or other special characters. For example, you may want to rename them to "_" characters or delete them. This step is optional, but you can work with files more conveniently later if, for example, you don't have to cite names in quotation marks / apostrophes, and so on. At least I prefer working with files that don’t have special characters and spaces in their names.

These filenames also include spaces, apostrophes, parentheses, commas, and &. These can be easily removed in a few steps:

First, replace the spaces with "_" characters. It was most simply called in one cycle tr command you can do:

for file in *.flac; do mv "$file" "$(echo $file | tr " " "_")" ; done

Then we remove the apostrophes, parentheses, and ampersands:

for file in *.flac; do mv "$file" "$(echo $file | tr -d "'()&,")" ; done

Of course, if our own filenames contain different special characters, they should be included in the list.

After cleaning the filenames, our directory looks like this:

Not all files had special characters in their names, they were written by mv command that no renaming has taken place. How much more cultured filenames look like this. So they will definitely not run into anything anymore.

We don't have to worry about the converted filenames either, because music players still show ID3 members of mp3 files, so the original titles, artists, etc. will be visible.

You can then create an mp3 subdirectory to which you will convert the converted audio files:

mkdir mp3

With this, our music files are ready to work.

 

Convert music files from flac format to mp3

The conversion can be done in several ways: on the command line or in a graphical interface. Let's look at an example of each.

Convert with the ffmpeg command

 

 

Az ffmpeg command is a very powerful conversion tool that allows you to convert video and audio files between many formats. the ffmpeg To install the package, run the following apt-get command:

sudo apt-get install ffmpeg

Trial conversion

To convert, run the ffmpeg operation on the very first file as a test while remaining in your music directory. So I have:

ffmpeg -i 01_Brad_Fiedel_-_Main_Title_Terminator_2_Theme.flac -acodec mp3 -ac 2 -ab 192k -loglevel error mp3/01_Brad_Fiedel_-_Main_Title_Terminator_2_Theme.mp3
If you did not clean up the file names, enclose them in quotation marks.

The switches and parameters for this command are as follows:

  • -Yo: input file
  • -acodec: codec to use for conversion
  • ac: number of audio channels (eg stereo)
  • ab: audio bit rate. Here, the 192 already results in good quality. Of course, if the sampling of our source file is inherently weak, it won't get any better.
  • -log letter: logging level. It's not neccessary. If we leave, it will write a lot of unnecessary things for us to the output. This switch has several setting levels, see the command for more information manual page we can find out. In the example, I chose the "error" level, so it only writes to the output if something goes wrong during the conversion.
  • and finally we need to specify the output file.

It took me a few seconds to convert. Once we’re done, we can check from a music player to see if it really became the right mp3 for us (sound quality, data, etc.).

We got exactly what we set up. Also, if we even look at the file size, we got an mp75 file of less than 3 MB from the original 3 MB flac file, which is high quality for listening to music at home. If you want to convert at a higher quality, a -ab parameter is raised to the desired level. But usually 192 kbps already results in very good quality.

If all goes well, you may come up with a batch conversion.

Batch conversion

If you have several files, it is advisable to automate here as well, than to enter the names of the files one by one. We also use a for loop for this:

for file in *.flac; do echo "Fájl konvertálása: $file " ; ffmpeg -i "$file" -acodec mp3 -ac 2 -ab 192k -loglevel error "mp3/${file%.*}.mp3" ; done

The command is arranged in several lines for clarity:

for file in *.flac; do \
  echo "Fájl konvertálása: $file " ; \
  ffmpeg \
    -i "$file" \
    -acodec mp3 \
    -ac 2 \
    -ab 192k \
    -loglevel error \
    "mp3/${file%.*}.mp3" ; \
  done

What happens here is that in the loop we first delete the name of the file to be converted to see where the operation is going, then run our ffmpeg command, replacing the names with the loop variable, and at the end we take down the file extension and ".mp3" extension we will tie it to the end.

I even added a timer to this, so the output is:

It all converted in 1 minute 33 seconds. Also, the total contents of the mp3 library are 74MB instead of the 2GB flac file.

Batch conversion with parallelization

 

 

The previous solution is also excellent, but it has the disadvantage that the whole conversion operation runs on only one thread. So if you have a lot of files, it can take a long time to convert while not using the CPU cores. So if we have a multi-core processor, which is already basic these days, we can turbo-convert our conversion to Parallel using the command.

A Parallel package is not part of the base system, so you need to install:

sudo apt-get install parallel

Then we can convert our previous converting command as follows:

parallel ffmpeg -i {} -acodec mp3 -ac 2 -ab 192k -loglevel error ./mp3/{.}.mp3 ::: ./*.flac
here the three colons ":::" are a kind of separator string, indicating to the parallel command that the task to be performed is so far, followed by the input, in this case a list of flac files. So here the for loop is replaced by the parallel command itself, where the parentheses represent the loop variable, in which the ffmpeg command gets the name of the next file.

When you run the command, the "engine" spins up, the processor is running at 100%, and in the meantime you can see that you have run the ffmpeg command on a separate thread for each flac file with our switches:

Here, the threads marked in white run on the physical and HT cores (I7-3770 CPU), so I have a total of 8 threads running at the same time, while the others (green) are waiting for the next free processor core.

Of course, we have nothing to do with this, only I am used to looking at it interestingly htop command to see what happens in the background.

After running the command, you can also see the speed difference:

He ran here in 25 seconds. Which, when calculated, is 3,72 times shorter than the previous 1 minute 33 seconds. This is how the combined performance of the 4 physical processor cores comes out. HT cores don't matter much at this time. Interestingly, there was a broadly similar increase in speed when the I compressed files on multiple threads at once with pigz.

 

Convert with the SoundConverter graphics application

If you want to convert flac files to mp3 in a graphical interface, SoundConverter is an ideal choice, which takes up little space and is easy to use. THE sound converter To install the package, run the following command:

sudo apt-get -y install soundconverter

The program can be found in the "Audio and Video" group, called "Audio Converter", starting from the "start" menu of the desktop. Start:

Add files

 

 

Add the flac files you want to convert. We can do this above Add a file button, or simply drag and drop files from another file manager window:

Beállítások

Click on Beállítások button, the settings panel will appear, where you can set everything in one place:

Here we set everything according to our own needs. For more information, see I wrote in another description.

When everything is set, close the window.

Conversions

If we’re done with everything so far, the conversion can come. In the main window, click Conversions button, the process will start on several threads:

Then after you are done:

With the same settings, it ran faster than the command line running on a single thread, but it did slower converting flac files to mp3 than the command line running on multiple threads. This graphics program also has a progress bar, so for many files we know at any time where we are.

 

 

Conclusion

With these methods, we can easily and automatically convert our music to the required audio format. Although in this example we only converted flac type files to mp3, we can convert them to many other formats with both methods presented.