-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathnormalize
executable file
·179 lines (150 loc) · 5.27 KB
/
normalize
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
#!/bin/sh
#===============================================================================
# nomalize
# normalize audio levels
#===============================================================================
# dependencies:
# ffmpeg file awk tail
#===============================================================================
# script usage
#===============================================================================
usage()
{
echo "\
# normalize audio levels
$(basename "$0") -i input.(mp4|mkv|mov|m4v|aac|m4a|wav|mp3) -o output.(mp4|mkv|mov|m4v|aac|m4a|wav|mp3)
-i input.(mp4|mkv|mov|m4v|aac|m4a|wav|mp3)
-o output.(mp4|mkv|mov|m4v|aac|m4a|wav|mp3) : optional argument
# if option not provided defaults to input-name-normalized-date-time-extension"
exit 2
}
#===============================================================================
# error messages
#===============================================================================
INVALID_OPT_ERR='Invalid option:'
REQ_ARG_ERR='requires an argument'
WRONG_ARGS_ERR='wrong number of arguments passed to script'
NOT_MEDIA_FILE_ERR='is not a media file'
#===============================================================================
# check the number of arguments passed to the script
#===============================================================================
[ $# -gt 0 ] || usage "${WRONG_ARGS_ERR}"
#===============================================================================
# getopts check the options passed to the script
#===============================================================================
while getopts ':i:o:h' opt
do
case ${opt} in
i) infile="${OPTARG}";;
o) outfile="${OPTARG}";;
h) usage;;
\?) usage "${INVALID_OPT_ERR} ${OPTARG}" 1>&2;;
:) usage "${INVALID_OPT_ERR} ${OPTARG} ${REQ_ARG_ERR}" 1>&2;;
esac
done
shift $((OPTIND-1))
#===============================================================================
# variables
#===============================================================================
# input, input name
infile_nopath="${infile##*/}"
infile_name="${infile_nopath%.*}"
infile_ext="${infile##*.}"
# file command check input file mime type
filetype="$(file --mime-type -b "${infile}")"
# audio and video mimetypes
mov_mime='video/quicktime'
mkv_mime='video/x-matroska'
mp4_mime='video/mp4'
m4v_mime='video/x-m4v'
wav_mime='audio/x-wav'
audio_mime='audio/mpeg'
aac_mime='audio/x-hx-aac-adts'
m4a_mime='audio/mp4'
# the file command wrongly identifies .m4a audio as a video file
# so we check if the file extension is .m4a and set the mime type to audio/mp4
if [ "${infile_ext}" = 'm4a' ]; then
filetype="${m4a_mime}"
fi
# check the files mime type
case "${filetype}" in
"${mov_mime}"|"${mkv_mime}"|"${mp4_mime}"|"${m4v_mime}"| \
"${wav_mime}"|"${audio_mime}"|"${aac_mime}"|"${m4a_mime}");;
*) usage "${infile} ${NOT_MEDIA_FILE_ERR}";;
esac
# print analyzing file
echo '+ Analyzing file with ffmpeg'
# ffmpeg loudnorm get stats from file
normalize=$(ffmpeg \
-hide_banner \
-i "${infile}" \
-af "loudnorm=I=-16:dual_mono=true:TP=-1.5:LRA=11:print_format=summary" \
-f null - 2>&1 | tail -n 12)
# read the output of normalize line by line and store in variables
for line in "${normalize}"; do
measured_I=$(echo "${line}" | awk -F' ' '/Input Integrated:/ {print $3}')
measured_TP=$(echo "${line}" | awk -F' ' '/Input True Peak:/ {print $4}')
measured_LRA=$(echo "${line}" | awk -F' ' '/Input LRA:/ {print $3}')
measured_thresh=$(echo "${line}" | awk -F' ' '/Input Threshold:/ {print $3}')
offset=$(echo "${line}" | awk -F' ' '/Target Offset:/ {print $3}')
done
# defaults for variables if not defined
audio_default="${infile_name}-normalized-$(date +"%Y-%m-%d-%H-%M-%S").${infile_ext}"
video_default="${infile_name}-normalized-$(date +"%Y-%m-%d-%H-%M-%S").mp4"
#===============================================================================
# functions
#===============================================================================
# audio function
audio () {
ffmpeg \
-hide_banner \
-stats -v panic \
-i "${infile}" \
-filter_complex \
"loudnorm=I=-16:
dual_mono=true:
TP=-1.5:
LRA=11:
measured_I=${measured_I}:
measured_LRA=${measured_LRA}:
measured_TP=${measured_TP}:
measured_thresh=${measured_thresh}:
offset=${offset}:
linear=true:
print_format=summary [audio]" \
-map "[audio]" \
-ar 44100 \
"${outfile:=${audio_default}}"
}
# video function
video () {
ffmpeg \
-hide_banner \
-stats -v panic \
-i "${infile}" \
-c:v copy \
-filter_complex \
"loudnorm=I=-16:
dual_mono=true:
TP=-1.5:
LRA=11:
measured_I=${measured_I}:
measured_LRA=${measured_LRA}:
measured_TP=${measured_TP}:
measured_thresh=${measured_thresh}:
offset=${offset}:
linear=true:
print_format=summary [audio]" \
-map 0:v -map "[audio]" \
-ar 44100 \
-pix_fmt yuv420p \
-movflags +faststart \
-f mp4 \
"${outfile:=${video_default}}"
}
# check if the mime type is audio or video then run the correct function
case "${filetype}" in
"${mov_mime}"|"${mkv_mime}"|"${mp4_mime}"|"${m4v_mime}") video "${infile}";;
"${wav_mime}"|"${audio_mime}"|"${aac_mime}"|"${m4a_mime}") audio "${infile}";;
*) usage "${infile} ${NOT_MEDIA_FILE_ERR}";;
esac