Skip to content

Instantly share code, notes, and snippets.

@citrusui
Created April 27, 2019 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save citrusui/3dfd1c6070fa8e69fedaa7e38a9d6e76 to your computer and use it in GitHub Desktop.
Save citrusui/3dfd1c6070fa8e69fedaa7e38a9d6e76 to your computer and use it in GitHub Desktop.
Detect whether or not a 24-bit FLAC has been upconverted from a 16-bit FLAC. Courtesy of "skamp" from the Hydrogenaudio Forums: https://hydrogenaud.io/index.php?topic=97746#msg813393
#!/bin/bash
me="${0##*/}"
if [ -w "$TMPDIR" ]; then
tdir="$TMPDIR"
elif [ -w '/tmp' ]; then
tdir='/tmp'
elif [ -w "$HOME" ]; then
tdir="$HOME"
elif [ -w "$PWD" ]; then
tdir="$PWD"
else
echo "$me: error: can't find a writable directory for creating the temporary file" 1>&2 ; exit 1
fi
tf="$( TMPDIR="$tdir" mktemp "${tdir}/${me}.XXXX" 2>/dev/null )"
if [ -z "$tf" ]; then
echo "$me: error: can't create temporary file" 1>&2 ; exit 1
fi
checkbits ()
{
local bps abps tbps=0 n=0
bps="$( metaflac --show-bps "$1" )"
flac -ac "$1" 2>/dev/null | fgrep 'wasted_bits' | cut -d '=' -f 3 | cut -f 1 > "$tf"
while read wb; do
tbps=$(( tbps + ( bps - wb ) ))
((n++))
done < "$tf"
abps=$(( ( ( tbps * 10 / n) + 5 ) / 10 )) # (* 10 + 5) / 10 for proper rounding
printf "%2u/%2u bits\t%s\n" "$abps" "$bps" "$1"
}
for f in "$@"; do
case "$f" in
*.flac) checkbits "$f" ;;
*) continue ;;
esac
done
rm -f "$tf"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment