/reflac

Shell script to recompress FLAC files

Primary LanguageShellISC LicenseISC

reflac

This is a script that allows you to recompress FLAC files while preserving their tags, intended for whole directories and for safety regardless of file name characters and encoding.

When storing FLACs on your big storage (desktop computer, NAS, etc), there may be a desire to compress them to the maximum extent possible, saving disk space and potentially many gigabytes for large collections. For this, you might use reflac --best on them.

However, slow and old systems might not have the capability to decode a maximally-compressed (or even the standard compression ratio) quick enough for real-time playback, and recompressing in a lower setting might be beneficial as well. This script started life, in a very rudimentary form for this purpose. The author used Rockbox on an old black/white display iPod, and found that flac -3 was the maximum he could compress FLACs while maintaining uninterrupted playback on the device.

Requirements

flac needs to be in your $PATH. This program is normally packaged as “flac” by distributions.

The script both depends on Bash and GNU features of other core utilities. Versions of mv, getopt, sync, etc from other sources (such as the *BSD operating systems) might work, but require compatibility with the same switches found in the GNU versions.

reflac has only been developed on tested on GNU+Linux, but should work on any other operating system with the appropriate tools.

Safety

By default, reflac performs a sync on files after two points of its process: after moving the recompressed file from the temporary directory to the original location, and after renaming the file from “*.new” to the original name. This should provide safety in the case of system crashes or processes being killed.

Assuming file system syncs have not been disabled, one of two scenarios should arise in the worst case:

  1. No new files appear in the directory. A complete FLAC file may or may not still be available in a temporary staging directory under /tmp.

  2. There exists an additional “*.new” file in the directory being processed, which may or may not be a complete FLAC file, the status of which can be tested with flac -t.

Running

Usage: reflac [OPTION]... [--] DIRECTORY...

 -h --help       Displays this help text
 -0 --fast       Use the fastest, but worst, compression possible.
 -1..-7          Adjust FLAC compression between these standard ranges.
                 The default is -5, the same as for flac itself.
 -8 --best       Use the slowest, but best, compression possible.
 -n --no-action  Do not recompress.  With --verbose, displays a list of
                 files that would be processed.
 -r --recursive  Recurse into directories.
 -s --no-sync    Do not synchronize file data.  Will return faster, with
                 the potential danger to lose your files in a system crash.
 -v --verbose    Increases the verbosity.  Use once to display the FLACs
                 currently being processed, use twice for the full ‘flac’
                 output.
 -V --version    Displays the version of this program

DIRECTORY should point ‘reflac’ to somewhere that contains *.flac
files. Optionally terminate the argument list with -- so that any
possible directory names don’t get misinterpreted as arguments.

Bugs

What, bugs? This program is flawless! Joking aside, although the author tries to resolve in reflac itself, it is at the mercy of bugs and limitations from flac and metaflac.

Tag preservation in the light of malformed tags simply does not exist. Certain release groups use buggy software with the creation of their files and will trigger some grievances.

You might see something like this:

$ reflac FLAC
/tmp/reflac.p8OjPn32z8/1-01 The Strange Green Pipe (Medley) [Mikeaudio].tag: ERROR: malformed vorbis comment field "Super Mario 64: Portrait of a Plumber",
       field contains no '=' character

reflac will not continue after the error, resulting in the untagged-but-recompressed file remaining in the temporary directory, the path of which should be part of the error message as in the example. The original file will not have been overwritten, maintaining the existing compression as well as the existing tags.

The choice is left to the user for repairing the file manually, such as by using metaflac, or removing the temporary directory altogether. The author uses, and recommends, MusicBrainz Picard to retag files before using reflac.