-
Notifications
You must be signed in to change notification settings - Fork 0
/
downconvert
executable file
·115 lines (101 loc) · 3.22 KB
/
downconvert
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
#!/bin/bash
# nb: the swp file that editscript relies on is provided by nano
red="$(tput setaf 9)"
bold="$(tput 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
read -p "$1 (y/n) " yn
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
mi=false
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=true
fi
changerate=false
for i in *flac;
do
[[ ! -w "$i" || ! -w "$PWD" ]] && printf \\nFix\ permissions\!\\n && exit 1
if "$mi"; then
bits=$(mediainfo --Output='Audio;%BitDepth%' "$i")
Hz=$(mediainfo --Output='Audio;%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 "$i")
Hz=$(ffprobe -v error -select_streams a:0 -show_entries stream=sample_rate -of default=noprint_wrappers=1:nokey=1 "$i")
fi
# 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 != 41000 )); then
printf 'The sample rate is %s Hz.\n' "$Hz"
if (( Hz > 48000 )) && ! "$changerate" ; 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"; then
Hz=48000
fi
fi
if [[ "$i" = *".24."* ]]
then
flac24="$i"
flac16="${flac24/.24.flac/.flac}"
ffmpeg -hide_banner -nostdin -i "$flac24" -af aresample=osf=s16:dither_method=triangular_hp -n -ar "$Hz" "$flac16"
else
flac16="$i"
flac24="${i/.flac/.24.flac}"
mv "$i" "$flac24"
ffmpeg -hide_banner -nostdin -i "$flac24" -af aresample=osf=s16:dither_method=triangular_hp -n -ar "$Hz" "$flac16"
fi
done
mkdir "24-bit"
mv *.24.flac "24-bit"
rmr 24-bit