-
Notifications
You must be signed in to change notification settings - Fork 2
/
bmm-converter_mp3
executable file
·335 lines (306 loc) · 10.2 KB
/
bmm-converter_mp3
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
329
330
331
332
333
334
335
#!/bin/bash
## ffmpeg script to remove flac/wav/ogg/m4a/wma from music library.
## Converts to mp3 and removes original files if wanted.
## Point to the audio library root folder as shown below!
## AUDIOROOT='/your/library/music/' in .env file or use
## the interactive mode to enter custom settings
## Tarso Galvao 28/03/23 on Debian 11.
##TODO: -log stuff
## load enviroment variables
if [ -f $HOME/bmm/.env ]; then
. $HOME/bmm/.env
else
echo -e "FATAL ERROR: .env not found.$\n
Use bmm-setup_enviroment to create one.\nAborting...";
exit 1;
fi
## AUTOMODE DEFAULT FLAGS (--auto)
REMOVE=1 # remove original files (0=no 1=yes)
FORMAT_ARRAY=("flac" "wav" "ogg" "m4a" "wma" "ape") # input format(s)
BITRATE='320k' # output mp3 bitrate in k
## FOLDERS
set_new_path (){
echo -e "\n${OK}Set new path:${NC} (ex: ${WARN}/home/user/music/${NC} )";
read -e AUDIOROOT < /dev/tty
check_path;
}
check_path (){
cd "$AUDIOROOT" &>/dev/null && confirm_path || {
echo -e "\n${ERROR}ERROR: Folder ${WARN}$AUDIOROOT${ERROR} could not be found.${NC}\n";
set_new_path;
};
}
confirm_path (){
echo -e "\n${OK}Found:${NC} $AUDIOROOT\n"
echo -e 'Is this the correct path? (Y/n) \c'
read -e yesno < /dev/tty
if [ "$yesno" = "n" ] || [ "$yesno" = "N" ]; then
set_new_path;
else
echo -e "\n${OK}Path set to ${WARN}$AUDIOROOT${NC}\n"
fi
}
## AUDIO FORMAT
## set input file format
set_input_format (){
echo -e 'Choose format(s) to be converted: (1-7)'
select fmt in flac wav ogg m4a wma ape ALL
do
case $fmt in
"flac")
FORMAT_ARRAY=("flac")
break;;
"wav")
FORMAT_ARRAY=("wav")
break;;
"ogg")
FORMAT_ARRAY=("ogg")
break;;
"m4a")
FORMAT_ARRAY=("m4a")
break;;
"wma")
FORMAT_ARRAY=("wma")
break;;
"ape")
FORMAT_ARRAY=("ape")
break;;
"ALL")
FORMAT_ARRAY=("flac" "wav" "ogg" "m4a" "wma" "ape")
break;;
*)
echo -e 'Not a valid choice. (1-7)';;
esac
done
echo -e "\n${OK}Selected input: ${WARN}'${FORMAT_ARRAY[*]}'${NC}"
}
## set output mp3 file quality
set_output_format (){
echo -e '\nChoose MP3 quality: (1-4)'
select bit in 96k 128k 256k 320k
do
case $bit in
"96k")
BITRATE='96'
break;;
"128k")
BITRATE='128'
break;;
"256k")
BITRATE='256'
break;;
"320k")
BITRATE='320'
break;;
*)
echo -e 'Not a valid choice. (1-4)';;
esac
done
echo -e "\n${OK}Selected output: ${WARN}MP3 - ${BITRATE}${NC}"
}
## FILES
## set removal flag
set_removal_flag (){
echo -e '\nRemove original files after conversion? (y/N) \c'
read -r yesno < /dev/tty
if [ "$yesno" = "y" ] || [ "$yesno" = "Y" ]; then
echo -e "\n${OK}Original files ${ERROR}WILL BE REMOVED.${NC}\n"
REMOVE=1
else
echo -e "\n${OK}Original files will ${ERROR}NOT${OK} be removed.${WARN}\nThey will be moved to ${NC}$AUDIOJUNK${WARN}.${NC}\n"
if [ ! -d "$AUDIOJUNK" ]; then mkdir "$AUDIOJUNK" &>/dev/null; fi || {
echo -e "${ERROR}ERROR: Could not create ${WARN}$AUDIOJUNK${ERROR} .\n Check permissions and try again.";
exit 1;
}
REMOVE=0
fi
}
## JOBS
## ffmpeg conversion
execute_tasks (){
if ! command -v $BANNER >> /dev/null; then echo -e "${WARN}BASH MEDIA MANAGER${NC}"; else eval $BANNER; fi
echo -e "${HEAD}Converter MP3${NC}"
echo '----------------------------------------------------------'
echo -e "Audioroot: ${WARN}$AUDIOROOT${NC}"
echo -e "Remove: ${WARN}$REMOVE${NC} (1-Yes 0-No)"
echo -e "Format(s): ${WARN}${FORMAT_ARRAY[*]}${NC}"
echo -e "Bitrate: ${WARN}$BITRATE${NC}"
echo -e "Permission: ${WARN}$AUDIO_OWNER:$GROUP${NC}"
dep_check ffmpeg;
dep_check libmp3lame0;
echo -e "----------------------------------------------------------\n"
echo -e "${OK}Starting...${NC}"
echo -e "Press ${WARN}"Control + C"${NC} to abort.\n"
for (( i=3; i>0; i--)); do sleep 1 & printf " $i \r"; wait; done
start_time=$(date +%s) # Capture the start time in seconds since epoch
for format in "${FORMAT_ARRAY[@]}"; do
echo -e "${OK}Starting ${WARN}$format${OK} conversion...${NC}"
# convert without setting permissions
find -name "*.${format}" -exec bash -c 'ffmpeg -i "{}" -y -vsync 0 -c:v copy -acodec libmp3lame -ab '${BITRATE}' "${0/.'${format}'}.mp3"' {} \;
# convert and set correct permissions
# find -name "*.${format}" -exec bash -c 'ffmpeg -i "{}" -y -vsync 0 -c:v copy -acodec libmp3lame -ab '${BITRATE}' "${0/.'${format}'}.mp3" && sudo chown '${AUDIO_OWNER}:${GROUP}' "${0/.'${format}'}.mp3"' {} \;
# find -name "*.${format}" -exec bash -c '
# ffmpeg -i "{}" -y -vsync 0 -c:v copy -acodec libmp3lame -ab '${BITRATE}' "${0/.'${format}'}.mp3" &&
# sudo chown '${AUDIO_OWNER}:${GROUP}' "${0/.'${format}'}.mp3" &&
# parent_dir=$(dirname "{}")
# dir_to_chown=$(basename "$parent_dir")
# sudo chown '${AUDIO_OWNER}:${GROUP}' "$parent_dir/$dir_to_chown"' {} \;
echo -e "${WARN}$format${OK} conversion completed.${NC}\n"
## originals removal
if [ $REMOVE -eq 1 ]; then
echo -e "${ERROR}Removing original ${WARN}$format${ERROR} files...${NC}"
find -name "*.${format}" -exec rm -rfv {} \;
echo -e "${ERROR}Original ${WARN}$format${ERROR} files removed.${NC}\n"
else
## move originals to junk folder
echo -e "${OK}Moving original ${WARN}$format${OK} files to ${WARN}$AUDIOJUNK...${NC}"
find -name "*.${format}" -exec mv {} "$AUDIOJUNK" \;
echo -e "${OK}Original ${WARN}$format${OK} files moved to ${WARN}$AUDIOJUNK.${NC}\n"
fi
done
# 'Done' message with process time logic
end_time=$(date +%s) # Capture the end time in seconds since epoch
time_taken=$((end_time - start_time)) # Calculate the time taken in seconds
# Convert seconds to minutes and seconds
minutes=$((time_taken / 60))
seconds=$((time_taken % 60))
message="Task complete in ${minutes}m ${seconds}s."
message_length=${#message}
height=$((message_length / 80 + 3)) # Assuming an average line length of 80 characters
width=$((message_length > 80 ? 80 : message_length + 10)) # Set width to at most 80 characters, adding a buffer of 10 characters
if [ "$1" == '--menu' ]; then
dialog --colors --no-lines \
--backtitle "\Z7Bash Media Manager" \
--infobox "$message" $height $width
read
clear && eval bmm-launcher; exit 1;
else
echo -e "${OK}"$message"${NC}"
fi
}
main_menu () {
##------------------------- PATH:
AUDIOROOT=$(dialog --no-lines --colors \
--backtitle "\Z7Bash Media Manager" \
--title "Converter MP3" \
--stdout --menu 'Audio files location:' \
0 0 0 \
$AUDIOROOT '(default)' \
Custom '(add new)')
#custom path
if [ "$AUDIOROOT" == 'Custom' ]; then
AUDIOROOT=$(dialog --no-lines --colors \
--backtitle "\Z7Bash Media Manager" \
--title "Converter MP3" \
--inputbox "Enter location full path: \n \
(ex.: /full/path/to/dir/ )" 10 40 \
3>&1 1>&2 2>&3 3>&-)
clear
fi
# return to launcher on escape or no path
if [ "$AUDIOROOT" == "" ]; then
clear && eval bmm-launcher; exit 1;
else
# return and warn if invalid path
cd "$AUDIOROOT" &>/dev/null || { echo -e "Path does not exist, aborting..."; sleep 2;
clear && eval bmm-launcher; exit 1; }
fi
##------------------------ REMOVAL FLAG
REMOVE=$(dialog --no-lines --stdout --colors \
--backtitle "\Z7Bash Media Manager" \
--title "Remove original files?" \
--yesno "Yes: Remove, No: Keep" 7 60 )
dialog_status=$?
if [ "$dialog_status" -eq 0 ]; then
#echo 'The previous dialog was answered Yes'
REMOVE=1
else
#echo 'The previous dialog was answered No or interrupted with <C-c>'
REMOVE=0
fi
##------------------------ INPUT FORMATS
checklist=(dialog --no-lines --separate-output --colors
--backtitle "\Z7Bash Media Manager"
--title "Converter MP3"
--checklist "Select format(s):" 22 76 16)
options=(
"flac" "Lossless" on
"wav" "Waveform" on
"ogg" "OGG Vorbis" on
"m4a" "Apple Audio" on
"wma" "Windows Audio" on
"ape" "Monkeys Audio" on)
choices=$("${checklist[@]}" "${options[@]}" 2>&1 >/dev/tty)
clear
FORMAT_ARRAY=()
for choice in $choices
do
FORMAT_ARRAY+=("$choice")
done
#error checks
if [ "$FORMAT_ARRAY" == "" ]; then echo -e "nothing selected, aborting..."; sleep 2;
clear && eval bmm-launcher; exit 1; fi
##----------------------- OUTPUT BITRATE
BITRATE=$(dialog --no-lines --stdout --colors \
--backtitle "\Z7Bash Media Manager" \
--title "Converter MP3" \
--menu 'Choose MP3 bitrate:' \
0 0 0 \
'80k' 'Poor Quality Lossy' \
'192k' 'Low Quality Lossy (recommended)' \
'256k' 'Mid Quality Lossy' \
'320k' 'High Quality Lossy' \
Custom '8k -> 320k')
if [ "$BITRATE" == 'Custom' ]; then
BITRATE=$(dialog --no-lines --colors \
--backtitle "\Z7Bash Media Manager" \
--title "Converter MP3" \
--inputbox "Enter desired bitrate:\n (ex.: 196k)" 10 40 \
3>&1 1>&2 2>&3 3>&-)
clear
fi
#error checks
if [ "$BITRATE" == "" ]; then echo -e "bitrate cant be empty, aborting..."; sleep 2;
clear && eval bmm-launcher; exit 1; fi
## Add k to bitrate if missing...
case "$BITRATE" in
*k)
echo "$BITRATE with 'k'"
;;
*)
echo "$BITRATE without 'k'"
# Add 'k' to the end of BITRATE
BITRATE="${BITRATE}k"
echo "Adding 'k' to the bitrate: $BITRATE"
;;
esac
##----------------------- EXECUTE TASKS
clear && execute_tasks --menu #| dialog --no-lines --progressbox 40 70;
}
## START
if [ "$1" = '--auto' ] || [ "$1" = '-a' ]; then
echo -e "auto-mode: ${OK}START${NC}"
dir_check "$AUDIOROOT"
cd "$AUDIOROOT" &>/dev/null || { echo -e "Path does not exist, aborting..."; sleep 2; exit 1; };
execute_tasks;
elif [ "$1" = '--no-gui' ] || [ "$1" = '-n' ]; then
## setup flags
check_path;
set_input_format;
set_output_format;
set_removal_flag;
## start the task(s)
execute_tasks;
elif [ "$1" = '--help' ] || [ "$1" = '-h' ]; then
if ! command -v $BANNER >> /dev/null; then echo -e "${WARN}BASH MEDIA MANAGER${NC}"; else eval $BANNER; fi
echo -e "${HEAD}Converter MP3"
echo -e "${OK}Converts multiple formats to MP3 for streaming.\n${NC}"
echo -e "Use with no arguements to enter the GUI."
echo -e "Usage: bmm-mp3_converter [-arg] [--arg]\n"
echo -e " -n [--no-gui] -> No GUI interactive mode."
echo -e " -a [--auto] -> Use defaults. (asks no questions)"
echo -e " -h [--help] -> This message screen."
exit 1
else
main_menu;
fi