Last active
August 14, 2019 23:59
-
-
Save Yamakaky/0f0546ea0aeb4ed220caa82d814b7d68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
IFS=$'\n\t' | |
set +e | |
ffmpeg -version > /dev/null 2>&1 | |
if [[ $? -ne 0 ]]; then | |
echo "ffmpeg must be installed" | |
exit 1 | |
fi | |
set -e | |
function process_file { | |
local file=$1 | |
local info=$(ffmpeg -i "$file" -filter replaygain -f null /dev/null 2>&1) | |
track_gain=$(echo $info | sed -n -r 's/.* track_gain = ([-+]?[0-9]+\.[0-9]+ dB).*/\1/p') | |
track_peak=$(echo $info | sed -n -r 's/.* track_peak = ([-+]?[0-9]+\.[0-9]+)/\1/p') | |
output=$(mktemp --suffix ".${file##*.}") | |
function finish { | |
rm -rf "$output" | |
} | |
trap finish EXIT | |
ffmpeg -y -loglevel warning \ | |
-i "$file" \ | |
-codec copy \ | |
-metadata REPLAYGAIN_IMPLEMENTATION="ffmpeg" \ | |
-metadata REPLAYGAIN_TRACK_GAIN="$track_gain" \ | |
-metadata REPLAYGAIN_TRACK_PEAK="$track_peak" \ | |
-metadata REPLAYGAIN_ALBUM_GAIN="$track_gain" \ | |
-metadata REPLAYGAIN_ALBUM_PEAK="$track_peak" \ | |
$output | |
chown --reference="$file" "$output" | |
chmod --reference="$file" "$output" | |
mv "$output" "$file" | |
} | |
files=() | |
compute_album=false | |
verbose=false | |
while [[ $# -gt 0 ]]; do | |
arg="$1" | |
case "$arg" in | |
-h|--help) | |
echo "ffreplaygain [-v|--verbose] [-a|--album] FILE..." | |
exit 0 | |
;; | |
-v|--verbose) | |
verbose=true | |
shift | |
;; | |
-a|--album) | |
# TODO: compute album gain and peak | |
compute_album=true | |
shift | |
;; | |
*) | |
files+=("$arg") | |
shift | |
;; | |
esac | |
done | |
for file in "${files[@]}"; do | |
process_file "$file" | |
if [ $verbose = true ]; then | |
echo "Finished $file: track_gain = $track_gain, track_peak = $track_peak" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment