How does padding and the "Optimize FLAC" feature work?

I recently figured out how to fix the issue I was having with a few releases that pertained to Windows not displaying metadata. That lead me down the padding rabbit hole, but I would like this explained from a more technical standpoint. I understand audio a little bit, but I never knew about padding until very recently. I'm very much a novice when it comes to this, and I apologize if my questions seem stupid.

  • What is padding and how does it work?

  • How does padding get added/removed to a file?

  • Does changing padding information affect file integrity/audio quality/readable or audible data?

  • I noticed when I would select "Optimize FLAC" that this would change the created/modified/accessed data for the files in question. Does this overwrite them with a completely new copy but with reduced padding? Is the file data (minus the padding) still the same?

I assume that padding is more or less separate from the actual readable/audible data that the files contain, but I want to be reassured from more knowledge folk that changing padding doesn't actually modify the playable audio file data itself compared to when it was ripped/created. Otherwise people probably wouldn't archive lossless music in the first place!

Thank you very much!

Sorry, but I think that request is a bit too broad and general for this forum.

For questions about the FLAC format and padding, a better place to start is the Hydrogen Audio forum or XIPH.org for FLAC basics.

As for FLAC optimization as implemented in Mp3tag, please start by searching the Mp3tag forum on Optimize Flac. There are many posts.

Then come back if you still have questions about this feature.

That is correct.

A simple way to understand padding is to look at an example file in a hex editor.
I removed all padding from this file, which is why straight after the last tag, the actual audio part begins.
NO PADDING:
grafik
If you were to add to the tags of this file, it would have to be rewritten because there is no space for new tags to be written to.

That's where padding comes in.
128 BYTES PADDING:
grafik
This is the same file with a padding of 128 bytes as you can see by the 00 00 area.
If you now add tags to this file that do not exceed 128 bytes, instead of rewriting the entire file, only the tag area (including the 00 00) will be edited to accomodate the new/changed tags.

Since that is far faster than rewriting the entire file, having padding is usually recommended even tho it technically "wastes" a bit of space. I find 4096 bytes or 4KB to be a good value as it allows for adding quite a bit of text while wasting very little space (and 4KB is the usual block size, at least for NTFS).

Another way that huge amounts of padding can end up in a file is if you embed images and then delete them from the file. The padding of flac files is not automatically shrunk back down.
If you add a 2.5MB cover file to a 12 track album and then remove the embedded image, each track will still have 2.5MB of padding, wasting 30MB for nothing.

Optimize FLAC will rewrite such files and set a sensible padding of 4KB.

I've personally solved the padding issue in my workflow by writing my own flac recompression script flacr to recompress flac files with the newest encoder while verifying their integrity, setting a fixed padding of 4KB and also calculating and writing Replaygain tags in one fell swoop. Feel free to check it out.