-
Notifications
You must be signed in to change notification settings - Fork 0
/
quartero4
executable file
·319 lines (256 loc) · 11.6 KB
/
quartero4
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
#!/bin/bash
tmp=tmp
scriptname=$(realpath "$0")
ulon=$(tput smul) #underline on
uloff=$(tput rmul) #underline off
bold="$(tput bold)" #bold on
tput0="$(tput sgr0)" #clear tput
red="$(tput setaf 9)" #red fg
white="$(tput setaf 15)" #white fg
relipsis="${red}..."
shopt -s nullglob extglob dotglob
threads=4
rmmatch=false
screened=false
editscript(){
local script path; script="${scriptname##*/}"; path="${scriptname%/*}"; swp="$path/.$script.swp"
if [[ ! -e "$swp" ]]; then printf "\n\n%s\n\n" "$swp"; (/usr/bin/nano "$scriptname"); exit
else printf "\n%s is already being edited.\n%s exists; try fg or look in another window.\n" "$scriptname" "$swp"; exit;
fi; }
pause(){ read -p "$*" ; }
cd-(){ cd - >/dev/null ; }
rmyn(){
while true
read -rp "$1"$'\n ' rmyn
do
case "$rmyn" in
[yY] ) "${@:3}"; break;;
[nN] ) printf '%s\n' "$2"; break;;
* ) printf "Invalid response...\n";;
esac
done
}
calculatetime(){
duration=$(find -type f -name "$1" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1)
hours=$(find -type f -name "$1" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d\n",$t/3600' | tail -n 1)
minutes=$(find -type f -name "$1" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d\n",$t/60%60' | tail -n 1)
seconds=$(find -type f -name "*$1" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d\n",$t%60' | tail -n 1)
[[ "$hours" =~ "0*(.*)" ]] && hours=${BASH_REMATCH[1]}
[[ "$minutes" =~ "0*(.*)" ]] && minutes=${BASH_REMATCH[1]}
[[ "$seconds" =~ "0*(.*)" ]] && seconds=${BASH_REMATCH[1]}
printf "Duration: %s\n" "$duration"
hours="${hours#0}"
hourmin=$(( hours*60 ))
minutes="${minutes#0}"
secmin=$((hourmin+minutes))
secmin=$((secmin*60))
seconds="${seconds#0}"
totalsec=$(( "$seconds" + "$secmin" ))
divsec=$(python3 -c "print($totalsec/4)")
} #calculatetime()
rmyn(){
while true
read -rp "$1"$'\n ' rmyn
do
case "$rmyn" in
[yY] ) "${@:3}"; break;;
[nN] ) printf '%s\n' "$2"; break;;
* ) printf "Invalid response...\n";;
esac
done
}
##--> rmyn() <--######################################################################################
confirm(){
local ans IFS=
while
printf '%s' "$1"
read -rp " " -n1 ans
do
printf '\n'
case "$ans" in [Yy]) return 0;; [Nn]) return 1;;
esac
done
}
##--> confirm() <--###################################################################################
checkdur(){
local m4a m4b opus mp3
m4a=(*m4a)
m4b=(*m4b)
opus=(*opus)
mp3=(*mp3)
if [[ "$1" ]]
then
find -type f -iname "*.$1" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1
else
printf '\n%sDuration(s):%s\n' "$bold" "$tput0"
[[ "${#m4a[@]}" -gt 0 ]] &&
printf '%s %s%s%s %s%s\n' "$bold" "$red" "$( find -type f -iname "*.m4a" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1)" "$white" "${m4a[@]}" "$tput0"
[[ "${#m4b[@]}" -gt 0 ]] &&
printf '%s %s%s%s %s%s\n' "$bold" "$red" "$( find -type f -iname "*.m4b" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1)" "$white" "${m4b[@]}" "$tput0"
[[ "${#mp3[@]}" -gt 0 ]] &&
printf '%s %s%s%s %s%s\n' "$bold" "$red" "$( find -type f -iname "*.mp3" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1)" "$white" "${mp3[@]}" "$tput0"
[[ "${#opus[@]}" -gt 0 ]] &&
printf '%s %s%s%s %s%s\n' "$bold" "$red" "$( find -type f -iname "*.opus" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1)" "$white" "${opus[@]}" "$tput0"
fi
}
##--> checkdur() <--##################################################################################
#####################################################
# Main #
#####################################################
[[ "$1" == @(edit|e|nano) ]] && editscript
[[ "$1" = @(-ys|-yes) ]] && shift && rmmatch=true && screened=true
[[ "$1" = -y ]] && shift && rmmatch=true
if "$screened"; then
allfiles=( *.mp3 )
[[ ! "$allfiles" ]] && allfiles=( *.m4[ab] )
. indexopus.lib
printline "$bold Calling m4b2opus in GNU screen $tput0"
printf \\n\\n
# allm4s=(*.m4[ab])
screenname="q.o.4-${allfiles[0]}"
screen -dmS "${screenname:0:16}" quartero4 -y "$@"
screen -ls
printf \\n\\n
exit
fi
SWD="$PWD"
if [[ -n "$1" ]] && [[ -f "$1" ]]; then
path="$1"
else
ckbook=( *@(mp3|m4@(a|b)) )
if (( ${#ckbook[@]} == 1 )); then
path="$ckbook"
else # i.e., elif (( ${#ckbook[@]} != 1 )); then
printf '\n%s%sE: No single .mp3 or .m4b/.m4a file found!%s\nListing of %s:\n' "$red" "$bold" "$tput0" "$SWD"
ls
printf '\n\nexit 1\n'
exit 1
fi
fi
if [[ "$path" != *\ --\ Audiobook.@(mp3|m4@(a|b)) ]]
then
path="$ckbook"
printf '\n%s%sFile is not in the format of <title> -- Audiobook.@(mp3|m4a|m4b)!\n%s\n' "$red" "$bold" "$tput0"
ckbookfile="${ckbook%.*}"; ckbookext="${ckbook##*.}"
# [[ "$ckbookfile" =~ \. ]] && n_ckbookfile="${ckbookfile//./_}"
# if "$rmmatch" || confirm "Rename $path to ${ckbook%.*} -- Audiobook.${ckbook##*.}? (y/n) "
n_ckbook="${n_ckbookfile:-$ckbookfile} -- Audiobook.$ckbookext"
if "$rmmatch" || { printf 'Rename\n %s \nto\n %s\n' \
"$path" \
"$n_ckbook";
confirm "(y/n) ?"; }
then
mv "$ckbook" "$n_ckbook"
else
printf "No file name given or filename invalid...\n"
printf "File must be in the format of \"* -- Audiobook.@(mp3|m4?)\n"
exit
fi
fi
[[ "$(mediainfo "$path"|grep Menu)" =~ ^Menu ]] && printf '%s has an index!\nexit 1\n' "$path" && exit 1
inext="${path##*.}"
title="${path%.*}"
title="${title%% -- *}"
ititle="${ulon}$title${uloff}"
if [[ "$inext" == "@(m4b|m4a)" ]]
then
ext="$inext"
outext="m4a"
elif [ "$inext" = "mp3" ] || [ "$inext" = "opus" ] || [[ "$inext" == "m4a" ]] || [[ "$inext" == "flac" ]]
then
ext="$inext"; outext="$inext"
else
printf "%s is not equal to a compatible file format of .mp3, .opus, .m4a, m4b. Please correct the error and try again."
exit 0
fi
#printf ' inext=%s\noutext=%s\n ext=%s' "$inext" "$outext" "$ext"
count=1 #cannot change.
[[ ! -d "$tmp" ]] && mkdir "$tmp"
for i in *"$ext"
do
wholefile="$i"
printf '\nFile to split: %s\n' "$wholefile"
calculatetime "$wholefile"
printf '\n\n%sSplitting file into %s pieces:\n%s' "$relipsis" "$threads" "$tput0"
while (( $count < $((1+$threads)) ))
do
#echo '>>>>> ffmpeg -i "$wholefile" -ss $(python3 -c "print(($count-1)*$divsec)") -to $(python3 -c "print($count*$divsec)") -acodec copy "${wholefile%.*} -- Part 0$count.$ext" '
#ffmpeg -ss $(python -c "print(($count-1)*$divsec)") -to $(python -c "print($count*$divsec)") -i "$whilefile" -acodec copy "${wholefile%.*} -- Part 0$count.$ext"
#pause "$(python -c "print(($count-1)*$divsec)")"
ffmpeg -n -nostdin -hide_banner -stats -v error -ss $(python3 -c "print(($count-1)*$divsec)") -to $(python3 -c "print($count*$divsec)") -i "$wholefile" -vn -acodec copy "$tmp/${wholefile%% -- *} -- Part 0$count.$outext"
((count++))
done
done
cd "$tmp"
for i in First Second Third Fourth
do
((n++))
for f in *\ --\ Part\ 0"$n".*
do
of="$f"
[[ "${f%.*}" =~ \. ]] && fext="${f##*.}" && ff="${f%.*}" && f="${ff//./·}.$fext"
nf="${f/./: $i Quarter.}"
mv "$of" "$nf"
[[ "$nf" =~ \· ]] && mv "$nf" "${nf//·/.}"
done
done
printf '#####################################################\nentering opus.book.4\n#####################################################\n\n'
opus.book.4
printf '#####################################################\nleaving opus.book.4\n#####################################################\n\n'
mediaduration
printf '#####################################################\nentering indexopus\n#####################################################\n\n'
indexopus -y -t "$title" || { echo "indexopus failed, exit 1" ; exit 1; }
#indexopus -y -t "$title"
#ioexit="$?"
#(( ioexit==1 )) && echo "indexopus failed, exit 1" && exit 1
printf '#####################################################\nleaving indexopus\n#####################################################\n\n'
#printf '\n\n...Done.\n\nPlease see %s/%s\n\n' "$PWD" "$ioutput"
#printf '%s\n%s\n' "$(ls *\ Audiobook.opus)" "$(mediainfo *\ Audiobook.opus|grep ^Duration\ |uniq)"
printf '\n%smoving ./tmp/%s -- Audiobook.opus to %s%s\n\n' "$relipsis" "$ititle" "$SWD" "$tput0"
mv *\ Audiobook.opus "$SWD"
lsopus=( *opus )
((${#lsopus[@]} > 0)) &&
printf '%sopus files still remain in ./tmp!\nls *opus:\n%s' "$relipsis" "$tput0" &&
printf '%s\n' "${lsopus[@]}" &&
echo
ckparts=( "$title -- Part "*.opus )
(( "${#ckparts[@]}" > 0 )) &&
printf -- '--> %sThe duration of the %s -- Part *opus is %s\n\n%s' "$relipsis" "$ititle" "$duration" "$tput0" &&
rmyn "Remove $ititle -- Part *.opus files? " "...Leaving $ititle -- Part *.opus files..." rm "$title"\ --\ Part\ *.opus
cd "$SWD"
# for now i need to just remove the temp files.... at the end of the day, if indexopus sent them back without error, they can just be removed.
rm -r ./tmp
#rmyn "Remove ./tmp? " "...Leaving ./tmp" rm -r ./tmp
checkdur
echo
#printf '%s\n%s\n' "$(ls *\ Audiobook.m4?)" "$(mediainfo *\ Audiobook.m4?|grep ^Duration\ |uniq)"
opusdursec=$(mediainfo --Inform="General;%Duration%" *\ --\ Audiobook.opus)
[[ ! "$allfiles" ]] && allfiles=( *.mp3 )
[[ ! "$allfiles" ]] && allfiles=( *.m4[ab] )
for i in "${allfiles[@]}"; do allfilesdur=$(( allfilesdur + $(mediainfo --Inform="General;%Duration%" "$i") )); done
if [[ "$rmmatch" = true ]]; then # && (( opusdursec > 0 )); then
verifydur=$(bc <<< "scale=4; ($allfilesdur - $opusdursec) / $opusdursec " )
verifydur="${verifydur/#-}"
if [[ $(bc <<< "scale=4; $verifydur < 0.001") ]]; then
rm -r "${allfiles[@]}"
[[ "$m4bindursec" != "$opusdursec" ]] &&
printf '\n%sDurations don'\''t %sexactly%s match but fuckin'\'' close '\''nuff!%s\n' \
"$relipsis" \
"$(tput sitm)" \
"$(tput ritm)" \
"$tput0" ||
printf '%sDurations match!%s\n' "$relipsis" "$tput0"
printf '\n%sDone.\n\nPlease see %s/%s%s\n\n' "$relipsis" "$PWD" "$ioutput" "$tput0"
# elif (( allfilesdur != opusdursec )) #[[ "$(checkdur m4b)" != "$(checkdur opus)" ]]; then
else
printf '%s%sThe m4b and opus audiobook durations do not match!\n' "$bold" "$red"
printf '%s%sm4b2opus will not automatically delete starting and temporary files!\n' "$white" "$tput0"
printf '\n\n'
printf '%s-y/-f specified, but the durations do not match!\n%s' "$bold" "$tput0"
fi
else
rm -i *.@(mp3|m4@(a|b))
fi
exit
#https://www.reddit.com/r/ffmpeg/comments/107p3mh/remove_all_apart_from_audio/
#Mon Dec 11 10:52:50 AM EST 2023