-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtomp3normvol
executable file
·34 lines (26 loc) · 1.12 KB
/
tomp3normvol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env bash
# problem: my recorders often produce audio streams with volume level far lower than ideal
# ffmpeg (and bash and perl) to the rescue! Catalyst https://superuser.com/questions/323119/how-can-i-normalize-audio-using-ffmpeg
die() { printf %s "${@+$@$'\n'}" 1>&2 ; exit 1 ; }
command -v ffmpeg || die "ffmpeg not in PATH"
read_mean_volume_neg_db() (
ffmpeg -i "$1" -vn -sn -dn -af "volumedetect" -f null /dev/null 2>&1 >/dev/null | perl -lne '/mean_volume: \-(\d+)\.\d+ dB/ && print $1'
)
destext=mp3
outfnm_suffix="_normvol.$destext"
normvol_to_mp3() (
# set -x
outfnm="${1%.*}$outfnm_suffix"
[[ -f "$outfnm" && "$outfnm" -nt "$1" ]] && die "exists $outfnm"
dbadj="$(read_mean_volume_neg_db "$1")" || die "read_mean_volume_neg_db failed on $1"
printf "+%3ddB %s\n" "$dbadj" "$outfnm"
ffmpeg -i "$1" -y -f "$destext" -q:a 0 -af "volume=${dbadj}dB" "$outfnm" 2> /dev/null || die "normvol failed on $1"
)
for fnm in "$@"; do
if [[ -f "$fnm" ]]; then
case "$fnm" in
*"$outfnm_suffix") echo "!recnv $fnm" ;;
*) normvol_to_mp3 "$fnm" ;;
esac
fi
done