Skip to content

Instantly share code, notes, and snippets.

@paskozdilar
Last active April 5, 2026 20:33
Show Gist options
  • Select an option

  • Save paskozdilar/6095fe73c80ad21fda3f518177699149 to your computer and use it in GitHub Desktop.

Select an option

Save paskozdilar/6095fe73c80ad21fda3f518177699149 to your computer and use it in GitHub Desktop.
Detect true mp3 bitrate
#!/usr/bin/env bash
set -euo pipefail
function main() {
# Check for argument
if [ $# -ne 1 ]
then
echo "usage: $0 INFILE"
exit 1
fi
# Define bitrates to check
local INFILE="$1"
local BITRATES="320 256 224 192 160 128 112 96 80 64 56 48 40 32"
# Check if file exists
if ! [ -f "$INFILE" ]
then
echo "file not found: $INFILE"
exit 1
fi
# Remove temporary files on exit
trap 'rm -f .tmp*.wav .tmp*.mp3' EXIT
# Check if lame and sox commands exist
for cmd in lame sox
do
if ! which "$cmd" >/dev/null
then
echo "command not found: $cmd"
exit 1
fi
done
# Decode original file to wav and invert amplitude
decode "$INFILE" .tmp.src.wav -1
# Decode file to bitrate and compare differences
for BITRATE in $BITRATES
do
# compress "$INFILE" .tmp.mp3 "$bitrate"
compress "$INFILE" .tmp.mp3 "$BITRATE"
decode .tmp.mp3 .tmp.wav
printf "%3s: %s\n" "$BITRATE" \
"$(compare .tmp.src.wav .tmp.wav \
2>&1 \
| grep 'RMS.*amplitude' \
| awk '{print $3}')"
done
}
# Compress mp3 file with given constant bit rate
function compress() {
local INFILE="$1"
local OUTFILE="$2"
local BITRATE="$3"
lame \
--quiet \
-q 0 \
"$INFILE" \
-b "$BITRATE" \
"$OUTFILE"
}
# Decode mp3 file into wav
function decode() {
local INFILE="$1"
local OUTFILE="$2"
local VOLUME="${3-1}" # set to -1 to invert signal
lame \
--quiet \
-q 0 \
"$INFILE" \
--decode \
.tmp.decode.wav
# resample to avoid compare issues
sox \
--volume "${VOLUME}" \
.tmp.decode.wav \
--rate 44100 \
"$OUTFILE" >/dev/null 2>&1
}
# Compare two wav files, assume one is inverted
function compare() {
local FILE1="$1"
local FILE2="$2"
sox \
--combine mix \
"$FILE1" "$FILE2" \
--null \
stat
}
main "$@"
@arseniiv
Copy link
Copy Markdown

arseniiv commented Apr 5, 2026

Hey people I was looking into this as well and see what I found: try looking into the spectrum:
https://www.reddit.com/r/hiphopheads/comments/2t88ne/a_quick_guide_to_checking_the_real_bitrate_of/

I'll try writing a Python script for myself that'll take FFTs of large chunks of a song (I think to be reasonably sure there's no need to try small) and will try to determine a real frequency cutoff if it's present. Aim at −80...−100 dB threshold I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment