UNB/ CS/ David Bremner/ blog/ posts/ Fixing flac metadata

I recently downloaded (and paid for!) some audio files in flac format. These files had malformed metadata: it turns out that the flac standard requires the STREAMINFO block to be first, but these files had album cover art as the first metadata block. This causes difficulties for many programs; in particular it makes it difficult to fix the metadata because /metaflac/ does not want to delete the first block. I finally worked around the problem by recompressing the files. Since /flac/ is lossless, this is not such a tragedy as it is with e.g. mp3 or most video codecs. Here is the script.

#!/bin/sh



temp=`mktemp reflacXXXXXX`
bak=$temp-"$1".bak
cp "$1" "$bak"


metaflac --no-utf8-convert --export-tags-to=$temp.tags  "$bak"
metaflac --export-picture-to=$temp.pic "$bak"
flac --silent -d -o - "$bak" | flac -o $temp.flac -
metaflac --no-utf8-convert --import-tags-from`$temp.tags --import-picture-from`$temp.pic $temp.flac
mv $temp.flac "$1"
rm $temp.tags $temp.pic
rm $temp

Update

: added --no-utf8-convert