-
Notifications
You must be signed in to change notification settings - Fork 2
/
mkvrg
executable file
·328 lines (291 loc) · 15.4 KB
/
mkvrg
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/bin/bash
cat > /dev/null <<LICENSE
Copyright (C) 2016,2023 kevinlekiller
Copyright (C) 2016 WhitePeter
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
LICENSE
reqProgs="awk ffmpeg ffprobe file find grep mkvpropedit mktemp sed"
read -rd '' DESCRIPTION <<DESCRIPTION
mkvrg - Apply replaygain tags to matroska files without remuxing (gain is calculated using ffmpeg and tags are applied using mkvpropedit).
Bash script for analyzing audio tracks in matroska files with ffmpeg and
applying replaygain gain and peak information with mkvpropedit, this avoids remuxing the matroksa file.
With mpv, you can add replaygain=track to mpv.conf to enable replaygain tag parsing.
With VLC, you can enable replaygain by clicking Tools -> Preferences -> Audio -> Replay gain mode.
Pass list of files or a directory to scan files, if you pass a directory it will recursively search in it for files.
It will only process files with these extensions: "mkv, mka, mk3d".
The default alrogrithm used for calculating gain and peak is ffmpeg's ebur128, this can be changed with the FFMPEGFILTER env variable.
Requires: $reqProgs
examples:
./mkrvg --help ; Shows this and exits.
./mkvrg ; Recursive search in current folder for matroska files.
./mkvrg test.mkv ; Process test.mkv in current folder.
./mkvrg Videos/ ; Recursive search in Videos folder for matroska files.
./mkvrg test.mkv Videos/ ; Process test.mkv in current folder and recursive
search in Videos folder for matroska files.
FORCE=true ./mkvrg test.mkv ; Process test.mkv even if it already has replaygain tags.
MINSIZE=+100M ./mkvrg ; Recursive search in current folder for matroska
files larger than 100MiB.
FFMPEGFILTER=loudnorm ./mkvrg ; Use loudnorm ffmpeg filter to scan found files.
Environment Variables:
VERIFY=[true|false] -> Check if the file already has tags or if tags were written succesfully.
Defaults to VERIFY=true
FORCE=[true|false] -> Force mkvrg to process matroska files, even if they already have replaygain tags.
If VERIFY=false, files will always be processed.
Defaults to FORCE=false
MINSIZE=+[0-9][KMBG] -> Minimum matroska file size to work on.
Set to MINSIZE=+0 to disable.
For example, set MINSIZE=+50M to ignore files under 50MB.
Defaults to MINSIZE=+0
REMUX=[true|false] -> Remux non matroska files to .mkv before scanning for .mkv files.
Defaults to REMUX=false
FFMPEG=[path] -> Which ffmpeg executable to use.
Set to FFMPEG=ffmpeg to use the one in PATH.
For example, set to FFMPEG=/home/user/ffmpeg/ffmpeg
Defaults to FFMPEG=ffmpeg
FFMPEGFILTER= ->
[ebur128|loudnorm|replaygain]
Which ffmpeg filter to use for scanning the audio.
Each filter calculates the gain/peak differently.
Lower reference loudness = quieter output (replaygain will be louder than ebur128 for example).
Faster speed = less time it takes to scan the audio.
replaygain: Speed: Fast. Roughly 10x faster than loudnorm.
Reference loudness: -18.00 LUFS
ebur128: Speed: Normal. Roughly 5x faster than loudnorm.
Note: This is with true peak calculation, which is slower.
Reference loudness: -23.00 LUFS
loudnorm: Speed: Slow.
Reference loudness: -24.00 LUFS
Defaults to FFMPEGFILTER=ebur128
PEAKTYPE= ->
[true|sample]
Type of peak calculation to use.
Note: Only used for ebur128 filter.
sample : Faster but less accurate. Varies from file to file, but can be up to 2x faster in testing.
true : Slower but accurate.
Defaults to PEAKTYPE=true
LOUDNESSOFFSET=
[+-]0.0 -> Affects the calculation of LUFS to dB, which affects how loud or quiet the audio will be.
Adding a positive number will make the audio louder, adding a negative number will make it quieter.
For example, the reference loudness of ebur128 is -23.00, if we set LOUDNESSOFFSET=5.00, the gain
will be roughly the same as what the replaygain (-18.00 reference) filter will calculate.
Note: This only affects ebur128 and loudnorm filters.
Disable by setting LOUDNESSOFFSET=0.0
Defaults to LOUDNESSOFFSET=0.0
PREVIEW=[true|false] -> If set to true, no files will be modified, values will be displayed on the console.
Defaults to PREVIEW=false
DESCRIPTION
#########################################################################################
##################################### ENV VARS ##########################################
#########################################################################################
VERIFY=${VERIFY:-true}
FORCE=${FORCE:-false}
MINSIZE=${MINSIZE:-"+0"}
REMUX=${REMUX:-false}
FFMPEG=${FFMPEG:-"ffmpeg"}
FFMPEGFILTER=${FFMPEGFILTER:-"ebur128"}
PEAKTYPE=${PEAKTYPE:-"true"}
LOUDNESSOFFSET=${LOUDNESSOFFSET:-"0.00"}
PREVIEW=${PREVIEW:-false}
#########################################################################################
################################### ENV VARS End ########################################
#########################################################################################
# force number handling to C
LC_NUMERIC=C
# for more convenient regex construction
FLOAT_ERE='[+-]?[0-9]+\.[0-9]+'
if [[ $1 == "--help" || $1 == "-h" ]]; then
echo "$DESCRIPTION"
exit 0
fi
unset DESCRIPTION
if [[ ! $FFMPEGFILTER =~ ^(ebur128|loudnorm|replaygain)$ ]]; then
echo -e "\e[31mERROR: Invalid FFMPEGFILTER.\e[0m" > /dev/stderr
exit 2
fi
if [[ ! $PEAKTYPE =~ ^(sample|true)$ ]]; then
echo -e "\e[31mERROR: PEAKTYPE must be either sample or true.\e[0m" > /dev/stderr
exit 3
fi
if [[ ! $LOUDNESSOFFSET =~ ^\-?[0-9]+\.[0-9]+$ ]]; then
echo -e "\e[31mERROR: Invalid value for LOUDNESSOFFSET.\e[0m" > /dev/stderr
exit 4
fi
if ! command -v "$FFMPEG" &> /dev/null; then
echo -e "\e[31mERROR: Unable to find the ffmpeg executable.\e[0m" > /dev/stderr
exit 5
fi
reqProgs="awk file find grep mkvpropedit mktemp sed"
for reqProg in $reqProgs; do
if ! command -v "$reqProg" &> /dev/null; then
echo -e "\e[31mERROR: This program could not be found: $reqProg\e[0m" > /dev/stderr
exit 6
fi
done
unset reqProg reqProgs
tmpFile="$(mktemp)"
if [[ ! -f $tmpFile ]] || [[ ! -w $tmpFile ]]; then
echo -e "\e[31mERROR: Could not create temp file $tmpFile. Check permissions.\e[0m" > /dev/stderr
exit 7
fi
REFLOUDNESS="-23.00"
[[ $FFMPEGFILTER == "loudnorm" ]] && REFLOUDNESS="-24.00"
[[ $FFMPEGFILTER == "replaygain" ]] && REFLOUDNESS="-18.00"
[[ $FFMPEGFILTER != "replaygain" ]] && REFLOUDNESS="$(printf "%0.2f" "$(awk "BEGIN{print ($REFLOUDNESS + $LOUDNESSOFFSET)}")")"
VERIFY_CHECK="${REPLAYGAIN_ALGORITHM}: ${FFMPEGFILTER}"
trap cleantmp SIGHUP SIGINT SIGQUIT SIGTERM
function cleantmp() {
[[ -f $muxInFile && -f $muxOutFile ]] && rm -f "$muxOutFile"
rm -f "$tmpFile"
[[ -n $1 ]] && exit "$1" || exit 1
}
function lufsTodB() {
[[ $1 =~ ^$ ]] && echo "" && return
printf "%0.2f" "$(awk "BEGIN{print ($REFLOUDNESS - $1)}")"
}
function dBToLufs() {
lufsTodB "$1"
}
function dBtoAmplitude() {
[[ $1 =~ ^$ ]] && $1 && echo "" && return
printf "%06f" "$(awk "BEGIN{print 10^($1/20)}")"
}
function amplitudeToDB() {
[[ $1 =~ ^$ ]] && $1 && echo "" && return
printf "%0.2f" "$(awk "BEGIN{print 20*log($1)/log(10)}")"
}
function isMatroska() {
[[ $(file "$1") =~ "Matroska" ]] && return 0 || return 1
}
function filePos() {
[[ ! $fileIter =~ ^[0-9]+$ ]] || [[ ! ${#files[@]} =~ ^[0-9]+$ ]] && return;
echo "(file $fileIter of ${#files[@]}, $(awk "BEGIN{print 100*($fileIter/${#files[@]})}")%)"
}
if [[ $PREVIEW == false && $REMUX == true ]]; then
REGEX="$(echo "(^.*)\.(asf|avi|flv|m4[pv]|mp[4g]|mov|mpeg|m2?ts|ogv|qt|ts|vob|webm|wmv)$" | sed 's/\([()|]\)/\\\1/g')"
mapfile -t files < <(find "$@" -type f -size "$MINSIZE" -iregex "$REGEX")
for muxInFile in "${files[@]}"; do
((fileIter++))
isMatroska "$muxInFile" && continue
muxOutFile=${muxInFile%.*}.mkv
if [ -e "$muxOutFile" ]; then
unset muxInFile muxOutFile
continue
fi
echo "INFO: Remuxing '$muxInFile' to '$muxOutFile' $(filePos)."
if "$FFMPEG" -n -loglevel error -stats -nostdin -hide_banner -i "$muxInFile" -c copy -map 0 "$muxOutFile"; then
rm -f "$muxInFile"
else
rm -f "$muxOutFile"
fi
unset muxInFile muxOutFile
done
unset REGEX files fileIter
fi
mapfile -t files < <(find "$@" -type f -size "$MINSIZE" \( -iname "*.mk[av]" -o -iname "*.mk3d" \))
filesProcessed=0
for file in "${files[@]}"; do
((fileIter++))
if ! isMatroska "$file"; then
echo -e "\e[92mNOTICE: '$file' is not a matroska file $(filePos).\e[0m"
continue
fi
ffmpegOut=$("$FFMPEG" -nostdin -hide_banner -i "$file" 2>&1)
if [[ ! $FORCE == true ]] && [[ $VERIFY == true ]] && [[ "$ffmpegOut" =~ $VERIFY_CHECK ]]; then
echo -e "\e[92mNOTICE: Skipping, replaygain tags already exist on file '$file' $(filePos).\e[0m"
continue
fi
tracks=$(ffprobe -v error -of default=nw=1:nk=1 -select_streams a -show_entries stream=index "$file")
tracksProcessed=0
for track in $tracks; do
echo "INFO: Running ffmpeg using filter ${FFMPEGFILTER}, this can take a while. (track $track on file '$file') $(filePos) (REFLOUDNESS = $REFLOUDNESS LUFS)"
ffmpegCmd="$FFMPEG -loglevel info -nostats -nostdin -hide_banner -i \"$file\" -map 0:$track -filter:a \""
if [[ $FFMPEGFILTER == "ebur128" ]]; then
ffmpegCmd="${ffmpegCmd}ebur128=peak=$PEAKTYPE:framelog=quiet\" -f null -"
echo "$ffmpegCmd"
ffmpegOut=$(eval "$ffmpegCmd" 2>&1 | tr "\n" " " | sed "s/^.*\(Parsed_ebur128.*$\)/\1/" | sed "s/ \+/ /g")
echo "$ffmpegOut"
trackGain=$(lufsTodB "$(echo "$ffmpegOut" | grep -Po " I: $FLOAT_ERE LUFS" | cut -d\ -f3)")
trackPeak=$(dBtoAmplitude "$(echo "$ffmpegOut" | grep -Po " Peak: $FLOAT_ERE dBFS" | cut -d\ -f3)")
trackRange=$(printf "%0.2f" "$(echo "$ffmpegOut" | grep -Po " Loudness range: LRA: $FLOAT_ERE LU" | cut -d\ -f5)")
elif [[ $FFMPEGFILTER == "loudnorm" ]]; then
ffmpegCmd="${ffmpegCmd}loudnorm=print_format=summary\" -f null -"
echo "$ffmpegCmd"
ffmpegOut=$(eval "$ffmpegCmd" 2>&1 | tr "\n" " " | sed "s/^.*\(Parsed_loudnorm.*$\)/\1/" | sed "s/ \+/ /g")
echo "$ffmpegOut"
trackGain=$(lufsTodB "$(echo "$ffmpegOut" | grep -Po "Input Integrated: $FLOAT_ERE LUFS" | cut -d\ -f3)")
trackPeak=$(dBtoAmplitude "$(echo "$ffmpegOut" | grep -Po "Input True Peak: $FLOAT_ERE dBTP" | cut -d\ -f4)")
trackRange=$(printf "%0.2f" "$(echo "$ffmpegOut" | grep -Po "Input LRA: $FLOAT_ERE LU" | cut -d\ -f3)")
elif [[ $FFMPEGFILTER == "replaygain" ]]; then
ffmpegCmd="${ffmpegCmd}replaygain\" -f null -"
echo "$ffmpegCmd"
ffmpegOut=$(eval "$ffmpegCmd" 2>&1 | sed "s/track_gain = -24.00 dB//" | sed "s/track_peak = 0.000000//")
trackGain=$(echo "$ffmpegOut" | grep -Po "track_gain = $FLOAT_ERE dB" | cut -d\ -f3 | sed "s/^+//")
trackPeak=$(echo "$ffmpegOut" | grep -Po "track_peak = $FLOAT_ERE" | cut -d\ -f3)
else
exit 8
fi
if [[ $trackGain == "" || $trackPeak == "" ]]; then
echo -e "\e[92mNOTICE: Problem finding $FFMPEGFILTER info from ffmpeg for track $track on file '$file' $(filePos).\e[0m"
continue
fi
echo "INFO: Found: Gain ($trackGain dB | $(dBToLufs "$trackGain") LUFS), peak ($trackPeak amplitude | $(amplitudeToDB "$trackPeak") dB) for track $track on file '$file' $(filePos)."
if [[ $PREVIEW == true ]]; then
echo "INFO: PREVIEW mode is on, not applying tags, skipping to next track/file."
continue
fi
echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<!DOCTYPE Tags SYSTEM \"matroskatags.dtd\">
<Tags>
<Tag>
<Targets>
</Targets>
<Simple>
<Name>REPLAYGAIN_ALGORITHM</Name>
<String>$FFMPEGFILTER</String>
</Simple>
<Simple>
<Name>REPLAYGAIN_REFERENCE_LOUDNESS</Name>
<String>$REFLOUDNESS LUFS</String>
</Simple>
<Simple>
<Name>REPLAYGAIN_TRACK_GAIN</Name>
<String>$trackGain dB</String>
</Simple>
<Simple>
<Name>REPLAYGAIN_TRACK_PEAK</Name>
<String>$trackPeak</String>
</Simple>" > "$tmpFile"
if [[ $trackRange != "" ]]; then
echo "
<Simple>
<Name>REPLAYGAIN_TRACK_RANGE</Name>
<String>$trackRange dB</String>
</Simple>" >> "$tmpFile"
fi
echo "
</Tag>
</Tags>" >> "$tmpFile"
sed -i "s/^ //g" "$tmpFile"
sed -i "/^[[:space:]]*$/d" "$tmpFile"
mkvpropedit --tags track:"$((track+1))":"$tmpFile" "$file"
if [[ $VERIFY == true ]] && [[ ! $("$FFMPEG" -nostdin -hide_banner -i "$file" 2>&1) =~ $VERIFY_CHECK ]]; then
echo -e "\e[93mWARNING: Replaygain has not been applied for track $track on file '$file' $(filePos).\e[0m"
continue
fi
echo "INFO: Succesfully applied replaygain tags for track $track on file '$file' $(filePos)."
((tracksProcessed++))
done
[[ $tracksProcessed -gt 0 ]] && ((filesProcessed++))
done
[[ $filesProcessed -gt 0 ]] && cleantmp 0