Skip to content

Instantly share code, notes, and snippets.

@gene1wood
Created January 16, 2025 23:56
Show Gist options
  • Save gene1wood/eccc8f70ee00330bded9f78bdeb6057b to your computer and use it in GitHub Desktop.
Save gene1wood/eccc8f70ee00330bded9f78bdeb6057b to your computer and use it in GitHub Desktop.
Analyze FLAC file to determine if it has wasted bits
#!/bin/bash
# https://hydrogenaud.io/index.php/topic,97746.0.html
temp_file="$(mktemp)"
trap 'rm -f -- "$temp_file"' EXIT
bps="$( metaflac --show-bps "$1" )"
flac --analyze --stdout "$1" 2>/dev/null | grep --fixed-strings 'wasted_bits' | cut -d '=' -f 3 | cut -f 1 > "$temp_file"
tbps=0
n=0
while read wasted_bits; do
tbps=$(( tbps + ( bps - wasted_bits ) ))
((n++))
done < "$temp_file"
abps=$(( ( ( tbps * 10 / n) + 5 ) / 10 )) # (* 10 + 5) / 10 for proper rounding
printf "%2u/%2u bits\t%s\n" "$abps" "$bps" "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment