Skip to content

Instantly share code, notes, and snippets.

@iluvcapra
Created November 29, 2009 17:27
Show Gist options
  • Save iluvcapra/244986 to your computer and use it in GitHub Desktop.
Save iluvcapra/244986 to your computer and use it in GitHub Desktop.
A Shell Script for FLACing a broadcast WAV file and binary-comparing (including all wrapper and metadata) with original
#!/bin/sh
for file ; do
# We take every file we're given and...
echo "FLAC'ing" $file
# ...let the user know we've started working on it...
EXT=`echo $file | ruby -e "puts STDIN.read.scan(/([^.]*)$/)"`
# find out wether its name ends in ".wav" or ".aif"
COMP_FILE="$file~comp.$EXT"
# remember this for when we need to make the temp file to compare with
flac --keep-foreign-metadata "$file" -o "$file~.flac" 2>/dev/null
# We flac the file using the "--keep-foreign-metadata" option
# so all of our magic Pro Tools, broadcast wav, Soundminer and
# descriptive metadata is saved into the final product
#
# We supress FLAC's informative output on account of its obnoxiousness
if [[ $? != 0 ]]
# if flac returned an error...
then echo " >>>>>> FLAC FAILED TO COMPRESS FILE"
# let the user know, and...
continue
# move on to the next file
fi
# Now that we've checked that, we can go on to making sure that the file
# we've made decompresses into a file that matches our original, bit-for-bit
echo "Roundtrip-comparing original to FLAC" $file
# Let the use know how far we've gotten
flac -d --keep-foreign-metadata "$file~.flac" -o "$COMP_FILE" 2>/dev/null
# We de-compress our temporarily-created FLAC file and save it, using
# the file name we constructed all the way up on line 11
if test -e "$COMP_FILE";
# if Flac actually decompressed the file
then diff "$COMP_FILE" "$file"
# run diff between the file we started with and the end result of a
# complete compress-decompress run
if [[ $? = 0 ]]
# if diff had no error
then echo "****** Comparison succeeded"
# let the user know
mv "$file~.flac" "$file.flac"
# give our temporary flac file a permanent name...
rm "$file"
# and delete the file we started with, secure in the knowledge that
# that recovering it is possible
else echo " >>>>>> COMPARISON FAILED!!!"
# otherwise, if diff DID return an error, indicating the files are
# NOT the same
rm "$file~.flac"
# remove the temporary FLAC file we made
fi
rm "$COMP_FILE";
# remove the file we temporarily decompressed from our flac file, wether
# it was good or not
else echo " >>>>>> FLAC FAILED TO DECOMPRESS THE FILE TO COMPARE"
# if the flac -d invocation (remember that online 40?) never suceeded in
# decompressing the file, let the user know
continue
# go to the next file on our to-do list
fi
# we end up here if everything worked...
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment