-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnorm.sh
More file actions
233 lines (199 loc) · 5.97 KB
/
norm.sh
File metadata and controls
233 lines (199 loc) · 5.97 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/bash
#
# norm.sh — Normalize the loudness of media files using ffmpeg's loudnorm filter.
#
# Author: Conor Klecker (@r0n0c)
# GitHub: https://github.com/r0n0c/Normalize-Audio
# Version: 1.0.0
# License: MIT
# Created: 2024-04-30
# Updated: 2024-04-30
#
# This script scans a directory for .mkv, .mp4, .mov, and .avi files,
# analyzes their integrated loudness (LUFS), calculates the average,
# and normalizes files that are too loud or too quiet.
#
# Run ./norm.sh --help for usage.
set -euo pipefail
# Optional flags
REANALYZE=false
THREADS=1
CONFIRM=false
SEARCH_PATH="."
# Allowed media file extensions (space-separated, no dot)
ALLOWED_EXTENSIONS=("mkv" "mp4" "mov" "avi")
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--reanalyze)
REANALYZE=true
shift
;;
--threads)
THREADS="$2"
shift 2
;;
-y|--yes)
CONFIRM=true
shift
;;
-p|--path)
SEARCH_PATH="$2"
shift 2
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
# Declare associative array for file:loudness mapping
declare -A loudness_map
echo "🔍 Scanning media files under '$SEARCH_PATH'"
# Build find condition dynamically
FIND_CONDITION=""
for ext in "${ALLOWED_EXTENSIONS[@]}"; do
FIND_CONDITION+="\\( -iname '*.${ext}' ! -iname '*.loudnorm.json' \\) -o "
done
FIND_CONDITION=${FIND_CONDITION::-4}
# Correct find structure
echo "DEBUG: Running find command:"
echo "find "$SEARCH_PATH" \( -type f -a \( $FIND_CONDITION \) \) -print0"
mapfile -d '' -t media_files < <(find "$SEARCH_PATH" \( -type f -a \( $FIND_CONDITION \) \) -print0)
if [[ ${#media_files[@]} -eq 0 ]]; then
echo "❌ No media files found to process."
exit 1
fi
echo "📈 Starting parallel loudness analysis with $THREADS thread(s)..."
# Parallel analysis
pids=()
for file in "${media_files[@]}"; do
(
meta_file="${file}.loudnorm.json"
if [[ "$REANALYZE" == false && -f "$meta_file" ]]; then
echo "Using cached analysis: $meta_file"
else
echo "Analyzing: $file"
stats=$(ffmpeg -hide_banner -loglevel error -i "$file" -af loudnorm=I=-16:TP=-1.5:LRA=11:print_format=json -f null - 2>&1)
echo "$stats" | sed '$ s/}$/,\n "normalized": false\n}/' > "$meta_file"
fi
) &
pids+=($!)
if (( ${#pids[@]} >= THREADS )); then
wait -n
pids=("${pids[@]:1}")
fi
done
wait
# Build loudness map
for file in "${media_files[@]}"; do
meta_file="${file}.loudnorm.json"
if [[ -f "$meta_file" ]]; then
stats=$(<"$meta_file")
iI=$(echo "$stats" | awk -F': ' '/"input_i"/ { gsub(/[",]/, "", $2); print $2 }')
if [[ -n "$iI" && "$iI" =~ ^-?[0-9]+(\.[0-9]+)?$ ]]; then
loudness_map["$file"]="$iI"
else
echo "❌ Could not parse input_i for $file"
fi
else
echo "⚠️ Warning: Missing metadata for $file"
fi
done
# Calculate average loudness
sum=0
count=0
for i in "${loudness_map[@]}"; do
sum=$(echo "$sum + $i" | bc)
count=$((count + 1))
done
if [[ "$count" -eq 0 ]]; then
echo "❌ No valid loudness data found. Exiting."
exit 1
fi
avg=$(echo "scale=2; $sum / $count" | bc)
echo -e "\n📊 Average loudness across files: $avg LUFS\n"
printf "| %-80s | %8s |\n" "File Path" "Loudness"
printf -- "-%.0s" {1..95}; echo
average_files=()
loud_files=()
quiet_files=()
for file in "${!loudness_map[@]}"; do
iI="${loudness_map[$file]}"
diff=$(echo "$iI - $avg" | bc | awk '{print ($1 < 0) ? -$1 : $1}')
if (( $(echo "$diff <= 1.0" | bc -l) )); then
average_files+=("$file:$iI")
elif (( $(echo "$iI < $avg" | bc -l) )); then
quiet_files+=("$file:$iI")
else
loud_files+=("$file:$iI")
fi
done
print_group() {
group_name=$1
shift
files=("$@")
if [[ ${#files[@]} -gt 0 ]]; then
echo ""
echo "$group_name Files"
for f in "${files[@]}"; do
path="${f%%:*}"
lvl="${f##*:}"
printf "'%-80s' | %8s\n" "$path" "$lvl"
done
fi
}
print_group "Average" "${average_files[@]}"
print_group "Loud" "${loud_files[@]}"
print_group "Quiet" "${quiet_files[@]}"
# Confirm before proceeding
if [[ "$CONFIRM" == false ]]; then
echo ""
read -rp "Do you want to continue and edit the Loud and Quiet Files? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY]) ;;
*) echo "Aborting."; exit 0 ;;
esac
else
echo "✅ Auto-confirm enabled. Proceeding..."
fi
# Parallel normalization
echo "🔧 Starting parallel normalization with $THREADS thread(s)..."
pids=()
for file in "${!loudness_map[@]}"; do
(
iI="${loudness_map[$file]}"
diff=$(echo "$iI - $avg" | bc | awk '{print ($1 < 0) ? -$1 : $1}')
if (( $(echo "$diff <= 1.0" | bc -l) )); then
echo "Skipping $file (within 1 LUFS of average)"
exit 0
fi
if [[ "$REANALYZE" == false ]] && grep -q '"normalized": true' "${file}.loudnorm.json"; then
echo "✅ Already normalized: $file — skipping"
exit 0
fi
echo "🔧 Normalizing $file (diff = $diff LUFS)"
stats=$(<"${file}.loudnorm.json")
iTP=$(echo "$stats" | grep '"input_tp"' | sed -E 's/.*:\s*"(-?[0-9.]+)".*/\1/')
iLRA=$(echo "$stats" | grep '"input_lra"' | sed -E 's/.*:\s*"(-?[0-9.]+)".*/\1/')
iThresh=$(echo "$stats" | grep '"input_thresh"' | sed -E 's/.*:\s*"(-?[0-9.]+)".*/\1/')
offset=$(echo "$stats" | grep '"target_offset"' | sed -E 's/.*:\s*"(-?[0-9.]+)".*/\1/')
out="${file%.mkv}-matched.mkv"
if ffmpeg -hide_banner -loglevel error -i "$file" -c:v copy -af "loudnorm=I=$avg:TP=-1.5:LRA=11:measured_I=$iI:measured_TP=$iTP:measured_LRA=$iLRA:measured_thresh=$iThresh:offset=$offset:linear=true:print_format=summary" "$out"; then
if grep -q '"normalized":' "${file}.loudnorm.json"; then
sed -i 's/"normalized": *false/"normalized": true/' "${file}.loudnorm.json"
else
sed -i '$ s/}/,\n "normalized": true\n}/' "${file}.loudnorm.json"
fi
else
echo "❌ Normalization failed for $file"
fi
) &
pids+=($!)
if (( ${#pids[@]} >= THREADS )); then
wait -n
pids=("${pids[@]:1}")
fi
done
wait
echo -e "\n✅ Normalization complete."