Skip to content

Instantly share code, notes, and snippets.

@ksze
Created April 15, 2013 16:41
Show Gist options
  • Save ksze/5389440 to your computer and use it in GitHub Desktop.
Save ksze/5389440 to your computer and use it in GitHub Desktop.
A script to resample your FLAC files *and* make sure they conform to the FLAC Subset specification so they will play in pretty much all hardware players - e.g. portable music players like those made by COWON) - all this while minimizing the file sizes.
#!/usr/bin/env bash
# A script to resample your FLAC files *and* make sure they conform to the FLAC Subset
# specification so they will play in pretty much all hardware players - e.g. portable
# music players like those made by COWON - all this while minimizing file sizes.
# Requirements:
# - Bash 4 or newer (for the globstar)
# - sox
# - flac
if ((${BASH_VERSION:0:1} > 3)) && [ -n "$(which flac)" ] && [ -n "$(which sox)" ]:
shopt -s globstar
for f in **/*.flac; do
sox_options=''
if (($(metaflac --no-filename --show-bps "$f") > 16)); then
sox_options="-b 16"
fi
if (($(metaflac --no-filename --show-sample-rate "$f") > 44100)); then
sox_options="$sox_options -r 44100"
fi
if [ -n "$sox_options" ]; then
tmp_name="${f:0:(-4)}.new.flac"
if sox -DG "$f" $sox_options "$tmp_name"; then
mv "$f" "${f}.original"
mv "$tmp_name" "$f"
if flac -l 12 -b 4096 -m -e -r 0,8 -p -s "$f" -o "$tmp_name" && (($(stat -f %z "$f") > $(stat -f %z "$tmp_name"))); then
mv -f "$tmp_name" "$f"
fi
echo "$f" >> ~/resampled_tracks.txt
fi
rm "$tmp_name"
fi
done
echo "Done."
else
echo "System requirements are not satisfied."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment