Repair FLAC files
December 09, 2020 in digital music by Dan Gravell
Back in 2015 I wrote a popular post about finding corrupt FLAC files. Unfortunately I left the post on a bit of a cliff-hanger… once you’ve found some corrupted files, what do you do about them?
Well I’ve got some good news and some bad news.
I’ll start with the bad. It’s impossible to repair all FLAC problems.
There are two main types of FLAC corruption: those that exist in the audio stream and those in the overall structure of the file. Both are important; even though any given music player might be able to play the audio from a structurally-corrupt FLAC file it doesn’t mean other tools will be able to work with the files when different operations are performed, such as tagging.
How do I know my FLACs need repair?
Ideally you’d proactively check your FLACs for corruption. In the real world, though, the first time you’ll know you have a corrupt FLAC is when you try to play it, and you get a nasty error message.
Some common FLAC error messages include:
FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC
- The most common error; this means the software was unable to read the file correctly. It can be caused by badly structured data inside the FLAC file, corrupted audio within a frame within a FLAC file, or many other reasons.
FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER
- More specific than the previous error, this happens when the audio data has a frame header (which gives information about the audio, e.g. sample sizes) that is corrupted.
FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH
- One of the best aspects of FLAC is internal error detection - this means the error checking found the audio did not match what was originally encoded.
Fixing FLAC audio corruption
Corruption related to the audio stream is often the most difficult to fix.
First, try listening to the file.
If you notice a problem in the audio, then it’s most likely you’ve got an unrecoverable problem. To get back to a byte-for-byte faithful reproduction of the original music you will need the original music. For example, you’ll need to re-rip a CD and re-encode with flac
. If you’re lucky to have the original .WAV
files you can encode from them without any nasty physical spinning discs.
If you don’t notice an audio problem, things are a bit more subjective. If you do have to hand the original music it’s probably best to re-encode, as above. If you don’t, and you’re happy to live with a suspected unfaithful reproduction of the original music, you can simply re-encode the FLAC file.
But beware, you might be storing up problems - what if you get a better hi-fi in a few years where you can hear the difference?
Re-encoding a FLAC
The command line to do this is:
flac --verify --decode-through-errors --preserve-modtime -o fixed.flac corrupted.flac
Replace corrupted.flac
with the original FLAC file name.
What this does is use the original FLAC as input. This means, if there’s some (inaudible) audio artifact, this is treated as the original audio and so FLAC error checking via a checksum is ‘reset’ to allow the original problem. The resulting checksum includes the audio problem, and so the checksum comparison works fine - software should no longer complain.
After re-encoding you’ll have a new file, fixed.flac
, which should not complain when played.
If you’re uncomfortable with the command line and on Windows, give the FLAC frontend a try.
Repairing structural FLAC corruption
What do I mean by structural problems? Well, FLAC is a container format which means it contains more than just audio. Importantly, it is written to contain different types of data as well, such as metadata which we use for good stuff like embedding cover art.
So a FLAC file is broken down into many different parts. Each of these parts has a location in the file, a length and other information that helps software go directly to the correct part of the file… a bit like a map.
Unfortunately this “map” can sometimes be… well… wrong. A given part of the file might be identified as being larger than it is, for example, which means software can’t skip through the file correctly.
The good news is that these problems are generally fixable with the previous suggestion of re-encoding the FLAC.
Fixing FLAC corruption the pro-active way
It wouldn’t be a data-loss article without my usual recommendation. The best way to avoid damage or corruption to your FLAC files is to store a redundant copy. If you can recover from backups you can save a lot of time. In some cases you might not even have the original audio, and backups might be the only way to restore your music.
Have you experienced and other FLAC errors? Did you fix them? Let me know in the comments below and I’ll update the article!
Photo by Julia Stepper on Unsplash