Convert to FLAC

Introduction

FLAC is a free, open source lossless compression audio codec.  The Convert to FLAC script converts audio files compressed with alternative lossless codecs (Monkey's Audio, Shorten, etc.) to the FLAC format.  FLAC is my preferred audio format for archiving music, so I wanted an easy way to convert other formats to FLAC.  In addition to simply transcoding the file to the FLAC format, Convert to FLAC also preserves any existing tags from the original file.

Convert to FLAC is a BASH shell script, and was originally written for use under Linux.  It should, however, run under any OS that supports BASH (please let me know if you find any bugs in cross-platform support).

Convert to FLAC currently supports the following input formats:

Note:  As of version 2.1, Convert to FLAC supports ffmpeg as an optional decoder, which can be used to convert files if you do not have the required binaries as listed above (eg., for converting APE files, since mac can be difficult to find and install).  Please be aware that using ffmpeg as an alternative also has its drawbacks:

  • ffmpeg cannot read metadata, so any existing tags will not be copied to the new FLAC file
  • not all fomats are supported; ffmpeg can only be used for ALAC, APE, MLP, Shorten, WavPack, and WMA Lossless files
  • ffmpeg cannot pipe output directly to the flac, so the conversion process will take longer as it must first write out a temporary WAV file

Return to top

Download  Current Version: 2.1.5, Released: 11/26/2019

convtoflac.sh (15.09 KB) - The Convert to FLAC BASH shell script

ChangeLog - Changes history for previous releases

Return to top

Installation

Installation of Convert to FLAC is simple:

  1. Download the BASH script
  2. Copy to a directory in your $PATH (eg, /usr/local/bin/)
  3. Ensure that the script has the execute bit set (eg, chmod a+x /usr/local/bin/convtoflac.sh)
  4. Verify that required binaries are available for the format you want to encode (see supported formats list above)

To complete installation, please open up convtoflac.sh in any text editor and verify that the programs defined in the "Setup environment" section are installed, and modify the path to each as appropriate.  As of versoin 2.0, Convert to FLAC uses dynamic paths, so this step can be skipped.

Return to top

Usage

Execute convtoflac.sh -h to display the convtoflac help information.  This will describe the syntax for running the program and list all available options.

When run on a supported audio file, convtoflac.sh will automatically determine the input filetype and transcode it to a FLAC file

Tip:  Convert to FLAC can only operate on a single audio file at a time (see note below for update).  However, if you need to process multiple files at once (such as an entire album), so you can so with a little shell magic:
for i in *.ape; do convtoflac.sh "$i"; done

This example converts APE files, but the same can be done for any supported filetype by changing the extension in the above command.

Note:  As of version 2.0, Convert to FLAC does indeed support multiple input files with the simple syntax convtoflac.exe *.ape.  This description has been left here both for archival purposes, and because the above one-liner is a handy way to process multiple files in general.

Return to top

Technical Details

convtoflac begins execution by verifying that all arguments have been passed correctly.  It the determines the input filetype by checking the extension (case-insensitive match).  Using the file command to more accurately identify the input file is not possible because it does not recognize every supported filetype.  Support files are then verified based on the input file format; eg., if you are missing wvunpack but are trying to convert an APE file, then you will not get an error about wvunpack.

convtoflac (usually) transcodes the file by piping the decompression output directly to the flac command to make the process as quick as possible.  If the -f option is used, however, the file must first be written out to a temporary WAV file.  On a multi-processor or multi-core system, transcoding multiple files can be done significantly faster by using the -t option to specify that multiple files should be transcoded concurrently.

flac will apply maximum compression (by default - override with -c) when encoding the file, and the resulting output file will have the same name as the original file, but with the .flac extension.  convtoflac will not overwrite existing files by default, but can be instructed to do so with -o.

If the input file format includes tags, convtoflac will then attempt to apply the tags to the new FLAC file.  It does this by first outputting the tags from the original file to a temp file, converting the tags to the VORBISCOMMENT format if necessary (the tag format used by FLAC), then applying them to the new file using metaflac.  Note that ffmpeg does not support metadata, and as such tags will not be copied if using the -f option.  Tags may be specifically omitted, if desired, by using -n.

Depending on which options were passed, convtoflac may either delete or prompt you to delete the input file at this point.  If you passed the -d or -m option, it will attempt to verify that transcoding was successful before deleting the file.  If not, convtoflac simply exists with an error message.

Return to top