Skip to content

Instantly share code, notes, and snippets.

@lildadou
Created May 31, 2017 21:19
Show Gist options
  • Select an option

  • Save lildadou/6174a56b8e7ed4ad2ab1ba1b4909aeac to your computer and use it in GitHub Desktop.

Select an option

Save lildadou/6174a56b8e7ed4ad2ab1ba1b4909aeac to your computer and use it in GitHub Desktop.
Normalize FLAC header of your library
#!/bin/bash
padding_low_limit=256
padding_high_limit=8192
padding_setup=2044
seekpoint_interval_limit=20
seekpoint_setup=6s
for album in ./*/*/
do
before_clean_disk_usage=$(du --max-depth=0 "$album" | awk '{print $1}')
echo "Start cleaning $album"
echo " [1/6] Remove undesirable header blocks..."
metaflac --remove --block-type=APPLICATION,PICTURE "$album"*.flac
echo " [2/6] Sort padding blocks..."
metaflac --sort-padding "$album"*.flac
echo " [3/6] Merge padding blocks..."
metaflac --merge-padding "$album"*.flac
echo " [4/6] Add or update Replay Gain..."
metaflac --add-replay-gain "$album/"*.flac
echo " [5/6] Check and fix seektables..."
for track in "$album"*.flac
do
seek_points_count=$(metaflac --list --block-type=SEEKTABLE "$track" | grep "seek points: " | awk -F ": " '{print $2}')
duration=$(metaflac --show-total-samples --show-sample-rate "$track" | tr '\n' ' ' | awk '{print $1/$2}')
seekpoint_interval=""
if [ ! -z "$seek_points_count" ]
then
seekpoint_interval=$(echo "$duration/$seek_points_count" |bc)
fi
if [ -z "$seekpoint_interval" ] || [ $seekpoint_interval -gt $seekpoint_interval_limit ]
then
echo " Missing or not enough seekpoint for $track. Build new seektable..."
metaflac --remove --block-type=SEEKTABLE "$track"
metaflac --add-seekpoint=$seekpoint_setup "$track"
fi
done
echo " [6/6] Check and fix padding block..."
for track in "$album"*.flac
do
padding_length=$(metaflac --list --block-type=PADDING "$track" | grep "length: " | awk '{print $2}')
if [ -z "$padding_length" ] || [ $padding_length -lt $padding_low_limit ] || [ $padding_length -gt $padding_high_limit ]
then
echo " Missing, not enough or too much padding for $track. Rebuild padding block..."
metaflac --dont-use-padding --remove --block-type=PADDING "$track"
metaflac --add-padding=$padding_setup "$track"
fi
done
after_clean_disk_usage=$(du --max-depth=0 "$album" | awk '{print $1}')
delta_disk_usage=$(echo "$before_clean_disk_usage-$after_clean_disk_usage" |bc)
echo "End cleaning $album ($delta_disk_usage bytes free)"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment