# Saving Space with FLAC

There is no reason to store music in WAV format, except for compatibility when working in music production or DJ'ing.

FLAC ([Free Lossless Audio Codec](https://xiph.org/flac/)) is a free, lossless, well documented, well supported and open source audio codec. When using FLAC, audio data will not change and will sound exactly the same as the source.

The speed of decoding FLAC files does practically not depend on the compression level used when encoding.[^1] While encoding with higher compression levels does take more time, it is a one time process and worth the additional saved space, especially if you have a lot of audio.

The following often overlooked procedures can be applied to save some additional space...

!!! abstract
	* Well supported common lossy audio formats (like MP3, AAC, OGG, OPUS, etc) should not be converted to lossless formats to avoid degrading the audio quality.
	* Uncommon lossy formats (like proprietary formats from game rips) can be converted to lossless formats to ensure compatibility with players.

!!! tip
	The procedures described below can be automatically applied using the following Python script: [flaccare.py](/assets/doc/flaccare.py)

!!! danger
	Proper precautions should be taken when performing batch operations on a media library:

	* Keep a restorable backup copy of the library.
	* Run operations on a test copy of the library first.
	* Run operations in batches. (e.g. depending on the library structure, per letter or genre folder)

## 1. Encode with libFLAC

Since version 1.4.0, [libFLAC](https://xiph.org/flac/download.html) has much improved compression ratio for normal and hires audio.

=== "Recommended"
	```
	flac -8 -V -f "$path"
	```

## 2. Remove embedded images

!!! warning
	Will decrease compatibility with some hardware players.

Often images (covers, scans, etc) for an album are identical for all tracks and will be duplicated if stored embedded, therefore taking more space for no content gained.


Removing low quality embedded covers from tracks can free up enough space for a high quality cover.jpg file without the overall size increasing by too much compared to when the covers were embedded.

=== "Recommended"
	```
	metaflac --dont-use-padding --remove --block-type=PICTURE "$path"
	```

## 3. Remove excess padding

!!! tip
	Add a column to [Mp3tag](https://mp3tag.de/en) with `$div(%_tag_size_appended%,1024) KiB` as the value to see the padding size in KiB.

Padding is used to make editing metadata faster. It is added to the beginning of the file, after the existing metadata and before the audio data. When metadata changes the padding will be overwritten instead of the file being lengthened and rewritten.[^2]

Some metadata editors replace removed embedded images with padding instead of shortening the file. This can leave megabytes of unused padding, which will likely never be used unless embedding other images later.

The recommended amount of padding is 8KiB, which is enough for editing vorbis comments.[^3]

=== "Recommended"
	```
	metaflac --dont-use-padding --remove --block-type=PADDING "$path"
	metaflac --add-padding=8192 "$path"
	```

{{ pageinfo(authors="Salty", created="2021/10/02", updated="2022/10/29") }}

[^1]: <https://web.archive.org/web/20150424044239/https://synthetic-soul.co.uk/comparison/lossless/>
[^2]: <https://xiph.org/flac/format.html#def_PADDING>
[^3]: <https://xiph.org/flac/documentation_tools_flac.html#flac_options_padding>
