-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdowncode
More file actions
executable file
·267 lines (238 loc) · 8.09 KB
/
Copy pathdowncode
File metadata and controls
executable file
·267 lines (238 loc) · 8.09 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
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
#!/bin/bash
# nb: the swp file that editscript relies on is provided by nano
export TERM
red="$(tput setaf 9)"
bold="$(tput bold)"
boldred="$(tput setaf 9 bold)"
tput0="$(tput sgr0)"
editscript(){
local scriptpath script path swp; scriptpath=$(realpath "$0" 2>/dev/null); script="${scriptpath##*/}"; path="${scriptpath%/*}"; swp="$path/.$script.swp"
[[ ! -e "$swp" ]] && printf "\n\n%s\n\n" "$swp" && (/usr/bin/nano "$scriptpath") && exit
printf "\n%s is already being edited.\n%s exists; try fg or look in another window.\n" "$scriptpath" "$swp"; exit ;}
pause(){ read -rp "$*" < /dev/tty; }
yn(){
local yn
while true; do
printf '%s (y/n) ' "$1"
read yn </dev/tty
case $yn in
[yY] ) return 0 ;; # Success (true)
[nN] ) return 1 ;; # Failure (false)
* ) echo "Please answer y or n." ;;
esac
done
}
[[ "$1" = @(--edit|e|-e) ]] && editscript
#if ! command -v mediainfo &> /dev/null; then
# unset mi
# if ! command -v ffprobe &> /dev/null; then
# echo "mediainfo and ffprobe could not be found. Please install one to continue."
# exit 1
# fi
#else
# mi=1
#fi
if command -v mediainfo &> /dev/null; then
mi=1
elif ! command -v ffprobe &> /dev/null; then
echo "mediainfo and ffprobe could not be found. Please install one to continue."
exit 1
fi
# check for flacs first
printf '\nCurrent directory is:\n %s%s%s\n\n' "$boldred" "$(pwd)" "$tput0"
printf 'Checking for flacs and permissions...\n\n'
flacs=(*flac)
[[ "$flacs" = '*flac' ]] && { printf '\n[%sERROR%s] %sNo flacs in the present working directory!%s\nPlease check and try again.\n(exit 1)\n' "$boldred" "$tput0" "$bold" "$tput0"; exit 1; }
# check if pwd is sshfs mount
findmnt -T . | grep -q "fuse.sshfs" && unset SUDO && fuse=true
if "${fuse:=false}"; then
printf '%sOn an sshfsmount!%s\n\n' "$boldred" "$tput0"
touchout="$(touch boobs 2>&1)"; fusetouch="$?"
! (( fusetouch )) && rm boobs
else
(( verbose )) && printf 'Not on an sshfsmount.\n\n'
fi
# check for directory permissions
#pause "$fusetouch $fuse $PWD"
(( fusetouch )) || [[ "$fuse" != true && ! -w "$PWD" ]] &&
if yn "$(printf '\nThe pwd %s is not writable.\nWould you like to "own" this directory?' "$(pwd)")"; then
ownout=( "$(own 2>&1|tee /dev/tty; ec="$?")" )
if (( ec )); then
printf '\n[%sERROR%s] own returned an error:\n' "$boldred" "$tput0"
printf '%s\n' "${ownout[@]}"
printf '\nFix permissions!\n(exit 1)\n\n'
exit 1
else
printf %s\\n "${ownout[@]}"
fi
else
printf '\n[%sERROR%s] Fix permissions!\n(exit 1)\n\n' "$boldred" "$tput0"
exit 1
fi
# check for write permission of flacs
for i in "${flacs[@]}"; do
[[ ! -w "$i" ]] &&
if yn "$(printf '\nThe file %s is not writable.\nWould you like to "own" this directory?' "$i")"; then
ownout=( "$(own 2>&1; ec="$?")" )
if ((ec)); then
printf '\n[%sERROR%s] own returned an error:\n' "$boldred" "$tput0"
printf '%s\n' "${ownout[@]}"
printf '\nFix permissions!\n(exit 1)\n'
exit 1
else
printf %s\\n "${ownout[@]}"
fi
else
printf '\n[%sERROR%s] Fix permissions!\n(exit 1)\n' "$boldred" "$tput0"
exit 1
fi
done
# if [[ "$fuse" != true && ! -w "$i" || ! -w "$PWD" ]] && yn "$(printf '\nDirectory or files are not writable.\nWould you like to "own" this directory?')"; then
# own #|| printf '\nown returned an error.\nFix permissions!\n' && exit 1
# else
# printf \\nFix\ permissions\!\\n && exit 1
# fi
for i in *flac; do
if (( "$mi" )); then
# bits=$(mediainfo --Output='Audio;%BitDepth%' "$i")
# Hz=$(mediainfo --Output='Audio;%SamplingRate%' "$i")
readarray -t bitsHz < <(mediainfo --Output="Audio;%BitDepth%\n%SamplingRate%" "$i")
else
# bits=$(ffprobe \
# -v error \
# -select_streams a:0 \
# -show_entries stream=bits_per_raw_sample \
# -of default=noprint_wrappers=1:nokey=1 \
# file:"$i" \
# )
# Hz=$(ffprobe \
# -v error \
# -select_streams a:0 \
# -show_entries stream=sample_rate \
# -of default=noprint_wrappers=1:nokey=1 \
# file:"$i" \
# )
readarray -t bitsHz < <(ffprobe \
-v error \
-select_streams a:0 \
-show_entries stream=bits_per_raw_sample \
-show_entries stream=sample_rate \
-of default=noprint_wrappers=1:nokey=1 \
file:"$i")
fi
bits="${bitsHz[0]}"
Hz="${bitsHz[1]}"
# if (( bits <= 16 )) || (( Hz < 48000 )); then
if (( bits <= 16 )) || (( Hz < 44100 )); then
# over=true
# (( Hz == 48000 )) && yn "File is $bits/$Hz, continue with processing?" && over=false
# if "$over"; then
kHz=$(( Hz / 1000 ))
kHzr=$(( Hz % 1000 ))
kHzr="${kHzr%%0*}"
[[ "$kHzr" ]] && kHz="$kHz.$kHzr"
printf '%s%sThe file, %s%s%s%s, %s%sdoes not appear to be above 16/48 bits/kHz:%s\n Bit rate : %s bits\n Sample rate: %s kHz\n%s%sPlease investigate. (exit 1)\n\n%s' \
"$bold" \
"$red" \
"$tput0" \
"$bold" \
"$i" \
"$tput0" \
"$bold" \
"$red" \
"$tput0" \
"$bits" \
"$kHz" \
"$bold" \
"$red" \
"$tput0"
exit 1
# fi
fi
# if (( Hz != 48000 )) && (( Hz != 44100 )); then
# printf 'The sample rate is %s Hz.\n' "$Hz"
# if (( Hz > 48000 )) && ! "${changerate:=false}" ; then
# if yn "Do you want to set the sample rate to 48000 Hz?"; then
# Hz=48000
# changerate=true
# else
# printf 'Please investigate. (exit 1)'
# exit 1
# fi
# elif (( Hz > 48000 )) && "${changerate:=false}"; then
# Hz=48000
# fi
# fi
## This change will let the script pick a multiple of 44.1 or 48 kHz
## instead of forcing everything to 48 kHz regardless.
# Check if the rate isn't already a base consumer standard
if (( Hz != 48000 )) && (( Hz != 44100 )); then
printf 'The sample rate is %s Hz.\n' "$Hz"
# Fallback for multiples of 44.1 kHz and non-standard rates (e.g. old 32kHz or weird audio)
targetHz=44100
# Dynamically determine the target based on mathematical family
if (( Hz % 48000 == 0 )); then
targetHz=48000
printf 'The file sample rate is a multiple of 48 kHz...\n'
elif (( Hz % 44100 == 0 )); then
printf 'The file sample rate is a multiple of 44.1 kHz...\n'
else
printf '%s[%swarn%s%s]%s The file sample rate (%s) is %sNOT%s a multiple of 48 or 44.1 kHz!\n' \
"$bold" "$red" "$tput0" "$bold" "$tput0" "$Hz" "$boldred" "$tput0"
printf 'Will use %s as the fallback sample rate if you choose to proceed.\n' "$targetHz"
fi
if (( Hz > targetHz )) && ! "${changerate:=false}" ; then
printf \\n
if yn "Do you want to set the sample rate to $targetHz Hz?"; then
Hz="$targetHz"
changerate=true # Set the flag so the rest of the loop skips prompting
else
printf '\nUser aborted. (exit 130)%s\n\n' "$tput0"
exit 130
fi
elif (( Hz > targetHz )) && "${changerate:=false}"; then
Hz="$targetHz"
fi
fi
if [[ "$i" = *.24.flac ]]; then
flac24="$i"
flac16="${flac24/.24.flac/.flac}"
else
flac16="$i"
flac24="${i/.flac/.24.flac}"
mv "$i" "$flac24"
fi
ffmpeg -n \
-hide_banner \
-nostdin \
-i file:"$flac24" \
-af aresample=osf=s16:dither_method=triangular_hp \
-ar "$Hz" \
file:"$flac16"
# if [[ "$i" = *".24."* ]]
# then
# flac24="$i"
# flac16="${flac24/.24.flac/.flac}"
# ffmpeg -n \
# -hide_banner \
# -nostdin \
# -i file:"$flac24" \
# -af aresample=osf=s16:dither_method=triangular_hp \
# -ar "$Hz" \
# file:"$flac16"
# else
# flac16="$i"
# flac24="${i/.flac/.24.flac}"
# mv "$i" "$flac24"
# ffmpeg -n \
# -hide_banner \
# -nostdin \
# -i file:"$flac24" \
# -af aresample=osf=s16:dither_method=triangular_hp \
# -ar "$Hz" \
# file:"$flac16"
# fi
done
mkdir "24-bit"
mv *.24.flac "24-bit"
rmr 24-bit </dev/tty