Fixing FLAC errors in multiple files

The problem

Recently I discovered that one of my FLAC files wouldn’t convert to another file format, namely Opus. During the conversion, I got an error.

flac.exe confirmed that the file had errors:

flac --test 1.flac
1.flac: *** Got error code 2:FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH


1.flac: ERROR while decoding data
        state = FLAC__STREAM_DECODER_ABORTED
Screenshot of error checking in Windows Terminal and flac.exe.

Corrupted FLAC files usually occur during the encoding process. This is why you should always use the --verify option when converting with flac.exe.

The solution

The particular file was able to play in the media player, it just wasn’t converting.

Using this command I was able to fix the FLAC file:

flac --verify --decode-through-errors --preserve-modtime -o output.flac input.flac

This command makes a new corrected file.

If the file is not playable and this process fails, there is not much you can do, unfortunately. You will have to get a new source.

Scanning through multiple FLAC files for errors

Because I am an obsessive audio freak, I had to check my whole library for errors to make sure I didn’t have false backups of my CD rips. But how? I got like a trillion FLAC files… 😱

The basic gist, if you just want to use Command Promt is to loop through all FLAC files in a folder (and it’s subfolders) like this:

FOR /R %i IN (*.flac) DO flac -t "%i"

However, this will only print the output to the console which is not very practical.

Since I have way too many FLAC files, I needed a more sophisticated method of scanning them all.

Using my below-average Python skills, I made a script that scans through all FLAC files in a folder (and its subfolders) and prints out a list of all the corrupted files to a txt file.

Animated GIF of the Python script at work.

How to use:

  • You need Python 3 + installed.
  • Run the script.
  • Enter the root directory of your music library (the script will only scan FLAC files and will not modify anything).
  • Wait for the script to complete. A text file should open with a list of your corrupted FLAC files.

Decide for yourself what you want to do with the broken files. Either fix them with the suggestion above or look for a new source, if the files are not able to repair.

Once you have all the broken files, you can repair them with a FOR-loop command in Windows CMD:

FOR /R %i IN (*.flac) DO flac --verify --decode-through-errors --preserve-modtime "%i" -o %~ni-new.flac

If you are not familiar with FLAC, in short, it’s a lossless audio file format often used for high-quality audio archiving. It is the most used and best file format for ripping audio CDs. You can read more about it here: https://xiph.org/flac/index.html

Leave a Reply

Your email address will not be published. Required fields are marked *