-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbam_converter
executable file
·335 lines (297 loc) · 9.8 KB
/
bam_converter
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/sh
# This is needed to handle spaces in file names (changes the separator).
OLDIFS=$IFS
IFS=$(echo -en "\n\b")
# Defining default settings
output_location=../tmp
input_location=$PWD
output_mode=mp3
output_quality=320000
overwrite=-n
cover_name=folder.jpg
path_atomicparsley=AtomicParsley
path_ffmpeg=ffmpeg
displayhelp()
{
echo "BAtch Music Converter"
echo ""
echo "Usage: ./bam_converter [options...]"
echo ""
echo "Available options are specified in brackets if applicable. The default option is"
echo "indicated by an asterisk"
echo ""
echo "General options"
echo " -h, --help Show this help"
echo " -d, --daemonize Daemonize bam_converter"
echo ""
echo "Input options:"
echo " -i, --input_location Location to read library from ['./'*]"
echo ""
echo "Output options:"
echo " -o, --output_location Location to write the new library to ['../'*]"
echo " -m, --output_mode [mp3*|alac]"
echo " -q, --output_quality MP3 output quality [128|196|320*]"
echo " -w, --overwrite Overwrite file if it already exists"
echo " -e, --embed_covers Embeds cover art file if available. Requires"
echo " AtomicParsley if converting to ALAC."
echo " -c, --cover_name Filename of cover art [folder.jpg*|...]"
echo ""
echo "Locations:"
echo " -p, --atomicparsley Specify alternative path to AtomicParsley"
echo " -f, --ffmpeg Specify alternative path to ffmpeg"
}
process_arguments()
{
while :
do
case "$1" in
-h | --help )
displayhelp
exit 0
;;
-d | --daemonize )
# nohup ${0} -$q
echo Daemonize function not yet implemented: $@
exit 0
;;
-i | --input_location=* )
if [ "$1" = "-i" ]; then
shift
process_input=$1
else
process_input=${1#*=}
fi
if [ -d $process_input ]; then
input_location=$process_input
else
printf >&2 'FATAL: Location set as input is not valid: %s\n' "$process_input"
exit 1
fi
shift
;;
-o | --output_location=* )
if [ "$1" = "-o" ]; then
shift
process_input=$1
else
process_input=${1#*=}
fi
if [ -d $process_input ]; then
output_location=$process_input
else
printf >&2 'FATAL: Location set as output is not valid: %s\n' "$process_input"
exit 1
fi
shift
;;
-m | --output_mode=* )
if [ "$1" = "-m" ]; then
shift
process_input=$1
else
process_input=${1#*=}
fi
case "$process_input" in
"alac" )
output_mode=alac
;;
"mp3" )
output_mode=mp3
;;
*) # no more options. Stop while loop
output_mode=mp3
printf >&1 'Output mode set to default: mp3'
;;
esac
shift
;;
-q | --output_quality=* )
if [ "$1" = "-q" ]; then
shift
process_input=$1
else
process_input=${1#*=}
fi
case "$process_input" in
"128" )
output_quality=128000
;;
"192" )
output_quality=192000
;;
"320" )
output_quality=320000
;;
*) # no more options. Stop while loop
output_quality=320000
printf >&1 'Output quality set to default: 320kbps'
;;
esac
shift
;;
-w | --overwrite )
overwrite=-y
shift
;;
-e | --embed_covers )
embed_covers=1
shift
;;
-c | --cover_name=* )
if [ "$1" = "-c" ]; then
shift
process_input=$1
else
process_input=${1#*=}
fi
cover_name=$process_input
shift
;;
-p | --atomicparsley=* )
if [ "$1" = "-p" ]; then
shift
process_input=$1
else
process_input=${1#*=}
fi
if [ -f $process_input ]; then
path_atomicparsley=$process_input
else
printf >&2 'FATAL: Path to AtomicParsley is not valid: %s\n' "$process_input"
exit 1
fi
shift
;;
-f | --ffmpeg=* )
if [ "$1" = "-f" ]; then
shift
process_input=$1
else
process_input=${1#*=}
fi
if [ -f $process_input ]; then
path_ffmpeg=$process_input
else
printf >&2 'FATAL: Path to ffmpeg is not valid: %s\n' "$process_input"
exit 1
fi
shift
;;
--) # End of all options
shift
break
;;
-*)
printf >&2 'WARN: Unknown option (ignored): %s\n' "$1"
shift
;;
*) # no more options. Stop while loop
break
;;
esac
done
# Converting relative paths to absolute paths if necessary.
output_location=$(realpath $output_location)
input_location=$(realpath $input_location)
# Changing working directory to set input location
cd $input_location
# Setting location for status file
status_file=$input_location/bam_converter.tmp
}
check_requirements()
{
type "$path_ffmpeg" >/dev/null 2>&1 || { echo >&2 "Can not find ffmpeg. Aborting."; exit 1; }
if [ "$output_mode" = "alac" ] && [ ! -z "$embed_covers" ]; then
{
type "$path_atomicparsley" >/dev/null 2>&1 || { echo >&2 "Can not find AtomicParsley. Aborting."; exit 1; }
}
fi
# Check for other instances
if [ -f $status_file ]; then
printf >&2 'FATAL: bam_converter is already running\n'
exit 1
fi
}
clean_exit()
{
echo ""
echo Process canceled, cleaning up....
local current_file=$(cat $status_file)
if [ ! -z "$current_file" ]; then
echo Removing $current_file
rm $current_file
fi
echo Removing status file
rm $status_file
echo done.... exiting.
exit 0
}
convert_directory()
{
local currentbasedir=$PWD
# Set target folder appropriately
if [ "$currentbasedir" = "$input_location" ]; then
local newdir=$output_location
else
local newdir=$output_location/${currentbasedir#${input_location}/}
fi
if [ -f "$cover_name" ]; then
mkdir -p $newdir
cp $cover_name $newdir/$cover_name
if [ "$output_mode" = "mp3" ]; then
local embed_mp3=1
else
local embed_alac=1
fi
fi
echo Processing $currentbasedir
for i in *.flac; do
[ $i != '*.flac' ] || continue
local newfilename=$newdir/${i%.flac}.$output_extension
# Saving current filename to status file so it can be removed if canceled
echo $newfilename > "$status_file"
[ ! -f "$newfilename" ] || continue
mkdir -p $newdir
echo -n Writing $newfilename
eval $path_ffmpeg -i \"$i\" "$output_parameters" \"$newfilename\" >& /dev/null
echo -en "\033[2K\r"
if [ ! -z "$embed_alac" ] && [ ! -z "$embed_covers" ]; then
echo -n Embedding artwork
$path_atomicparsley $newfilename --artwork $cover_name --overWrite >& /dev/null
echo -en "\033[2K\r"
fi
if [ ! -z "$embed_mp3" ] && [ ! -z "$embed_covers" ]; then
echo Embedding artwork
mv $newfilename $newfilename.tmp
$path_ffmpeg -i $newfilename.tmp -i $cover_name -c copy -map 0 -map 1 -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (Front)" $newfilename >& /dev/null
rm $newfilename.tmp
echo -en "\033[2K\r"
fi
done
echo -en "\033[1A"
echo -e "\t\t\t\t\t\t\t\t\t\t done! "
for d in *; do
if [ -d $d ]; then
(cd $d; convert_directory)
fi
done
}
process_arguments $@
check_requirements
trap clean_exit SIGHUP SIGINT SIGTERM
echo Starting Batch Converter:
if [ $output_mode = "mp3" ]; then
output_parameters="$overwrite -ab $output_quality -acodec mp3"
output_extension=mp3
else
output_parameters="$overwrite -acodec alac"
output_extension=m4a
fi
# Starts at current working directory and works its way through
convert_directory
echo Done converting...
echo Removing status file
rm $status_file
echo Exiting
IFS=$OLDIFS
exit 0