-
Notifications
You must be signed in to change notification settings - Fork 0
/
normvol
executable file
·50 lines (39 loc) · 1.76 KB
/
normvol
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/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
# fork of tomp3normvol: this one works with .ogg files as well as .mp3 (output file has same format as input; to convert to mp3, use another script)
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'
)
normvol_to_mp3() (
# set -x
fnm="$1" # ; echo "fnm=$1'"
[[ -f "$fnm" ]] || die "$fnm is not a file"
# break down $fnm:
fpath=""
if [[ $fnm == */* ]]; then
fpath="${fnm%/*}/"
cd "$fpath" || die "cd to $fpath failed"
fi # ; echo "fpath=$fpath'"
sanspath="${1##*/}" # ; echo "sanspath=$sanspath'" # https://stackoverflow.com/a/965069
ext="${sanspath##*.}" # ; echo "ext=$ext'"
sansext="${sanspath%.*}" # ; echo "sansext=$sansext'"
outfnm="${sansext}_normvol.$ext"
[[ -f "$outfnm" && "$outfnm" -nt "$1" ]] && die "exists $outfnm"
meanvol="$(read_mean_volume_neg_db "$1")" || die "read_mean_volume_neg_db failed on $1"
dbadj=$(( meanvol / 2 ))
printf "+%3ddB %s\n" "$dbadj" "$outfnm"
if (( dbadj > 0 )); then
ffmpeg -i "$1" -y -f "$ext" -q:a 0 -af "volume=${dbadj}dB" "$outfnm" 2> /dev/null || die "normvol failed on $1"
fi
)
for fnm in "$@"; do
if [[ -f "$fnm" ]]; then
case "$fnm" in
*_normvol.*) echo "!recnv $fnm" ;;
*) normvol_to_mp3 "$fnm" ;;
esac
fi
done