Skip to content

Instantly share code, notes, and snippets.

@samuel22gj
Created November 17, 2022 08:16
Show Gist options
  • Save samuel22gj/753b1dad3c55ac8b10a674d2981e7a9a to your computer and use it in GitHub Desktop.
Save samuel22gj/753b1dad3c55ac8b10a674d2981e7a9a to your computer and use it in GitHub Desktop.
compress mp3 by 48k bitrate and remove title metadata
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset
OIFS="$IFS"
IFS=$'\n'
info () {
printf "\r [\033[0;34m .. \033[0m] $1\n"
}
success () {
printf "\r [\033[0;32m OK \033[0m] $1\n"
}
fail () {
printf "\r [\033[0;31mFAIL\033[0m] $1\n"
echo ''
exit 1
}
if ! [ -x "$(command -v ffmpeg)" ]; then
fail "ffmpeg not found."
fi
for file in $(find . -type f -name "*.mp3")
do
info "Processing ${file}"
start=`date +%s`
oldSize=$(($(stat -f "%z" $file) / 1024))
tempFile=${file}.tmp
# compress to 48k bitrate and remove all metadata
#ffmpeg -y -i $file -map_metadata -1 -ab 48k -f mp3 $tempFile
# compress to 48k bitrate and remove title metadata
ffmpeg -v error -y -i $file -metadata title= -ab 48k -f mp3 $tempFile
newSize=$(($(stat -f "%z" $tempFile) / 1024))
end=`date +%s`
exeTime=$(($end - $start))
if [[ $newSize -lt $oldSize ]]; then
diff=$(($oldSize - $newSize))
pct=$(($diff * 100 / $oldSize))
rm $file
mv $tempFile $file
success "${oldSize} KB -> ${newSize} KB (-${pct} pct) (${exeTime}s)"
else
rm $tempFile
success "${oldSize} KB -> No change (${exeTime}s)"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment