-
Notifications
You must be signed in to change notification settings - Fork 11
/
flacconvert.sh
executable file
·636 lines (542 loc) · 18.2 KB
/
flacconvert.sh
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#!/bin/bash
#################################################################################
# #
# Copyright (C) 2009-2010, FLAC-Convert team #
# #
# FLAC-Convert is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# FLAC-Convert is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with FLAC-Convert. If not, see <http://www.gnu.org/licenses/>. #
# #
#################################################################################
if [ ! -n "$verbose" ]
then
verbose=2
fi
function info
{
if [ "$verbose" -ge 1 ]
then
echo "$(basename $0): $@"
fi
}
function debug
{
if [ "$verbose" -ge 2 ]
then
echo "$(basename $0): $@"
fi
}
function warn
{
echo "$(basename $0): WARNING: $@"
}
function usage
{
echo "Usage: $0 [profile.prof]"
}
# source profile if specified, or default.prof
function source_profile
{
if [ $# = 0 ]
then
profile="default.prof"
elif [ $# = 1 ]
then
profile="$1"
else
usage
return 1
fi
# if no slash, then path is relative
if [ $(expr index / "$profile") = 0 ]
then
relative=1
else
relative=0
fi
# try to find profile in the script directory
if [ ! -f "$profile" ]
then
if [ $relative = 1 ]
then
profile2="$(dirname "$0")/$profile"
if [ -f "$profile2" ]
then
profile="$profile2"
relative=0
else
echo "Neither $(pwd)/$profile, nor $profile2 exists"
return 1
fi
else
echo "File $profile doesn't exist"
return 1
fi
fi
# this should prevent bash searching profile in PATH
if [ $relative = 1 ]
then
profile="$(pwd)/$profile"
relative=0
fi
info "Using $profile"
source "$profile"
}
# enable ctrl-c abort
control_c()
{
for f in `jobs -p`; do
kill $f 2> /dev/null
done
wait
exit $?
}
trap control_c SIGINT
function check_exit_codes
{
local ps=${PIPESTATUS[*]}
local args=( `echo $@` )
local i=0
for s in $ps
do
if [ $s -ne 0 ]
then
warn "Return code of ${args[$i]} indicates failure"
break
fi
let i=$i+1
done
}
function read_tags
{
flacfile="$1"
shift
for tag in "$@"
do
val=$(metaflac --show-tag=$tag "$flacfile" 2>/dev/null |
awk -F = '{ printf($2) }')
# make tag global (declare -g was added in bash 4.2,
# printf -v was added in bash 3.1, eval is unsafe)
printf -v $tag %s "$val"
done
}
function create_mp3
{
flacfile="$1"
opt="$2"
outputfile="$3"
switches=(--tt --tn --tg --ty --ta --tl)
tags=(TITLE TRACKNUMBER GENRE DATE ARTIST ALBUM)
read_tags "$flacfile" "${tags[@]}"
tag_opts=()
for i in "${!switches[@]}"; do
s="${switches[$i]}"
t="${tags[$i]}"
t="${!t}"
if [ -n "$t" ] ; then
tag_opts+=("$s" "$t")
fi
done
# sleep while max number of jobs are running
until ((`jobs | wc -l` < maxnum)); do
sleep 1
done
debug "Encoding `basename "$flacfile"` to $outputfile"
(nice flac -dcs "$flacfile" 2>/dev/null | lame $opt \
"${tag_opts[@]}" - "$outputfile" &>/dev/null
check_exit_codes flac lame) &
}
function create_ogg
{
flacfile="$1"
opt="$2"
outputfile="$3"
# sleep while max number of jobs are running
until ((`jobs | wc -l` < maxnum)); do
sleep 1
done
debug " Encoding `basename "$flacfile"` to $outputfile"
(nice oggenc $opt "$flacfile" -o "$outputfile" &>/dev/null
check_exit_codes oggenc) &
}
function create_aac
{
flacfile="$1"
opt="$2"
outputfile="$3"
switches=(--artist --writer --title --genre --album --track --disc
--year --comment)
tags=(ARTIST COMPOSER TITLE GENRE ALBUM TRACKNUMBER DISCNUMBER DATE COMMENT)
read_tags "$flacfile" "${tags[@]}"
if [ -n "$TRACKTOTAL" ] && [ -n "$TRACKNUMBER" ]; then
TRACKNUMBER="$TRACKNUMBER/$TRACKTOTAL"
fi
tag_opts=()
for i in "${!switches[@]}"; do
s="${switches[$i]}"
t="${tags[$i]}"
t="${!t}"
if [ -n "$t" ] ; then
tag_opts+=("$s" "$t")
fi
done
# sleep while max number of jobs are running
until ((`jobs | wc -l` < maxnum)); do
sleep 1
done
debug "Encoding `basename "$flacfile"` to $outputfile"
(nice flac -dcs "$flacfile" 2>/dev/null | faac $opt \
"${tag_opts[@]}" -o "$outputfile" - &>/dev/null
check_exit_codes flac faac) &
}
function create_naac
{
flacfile="$1"
opt="$2"
outputfile="$3"
switches=(-meta:artist -meta:composer -meta:title -meta:genre -meta:album
-meta:track -meta:totaltracks -meta:disc -meta:year -meta:comment)
tags=(ARTIST COMPOSER TITLE GENRE ALBUM TRACKNUMBER TRACKTOTAL DISCNUMBER
DATE COMMENT)
read_tags "$flacfile" "${tags[@]}"
tag_opts=()
for i in "${!switches[@]}"; do
s="${switches[$i]}"
t="${tags[$i]}"
t="${!t}"
if [ -n "$t" ] ; then
tag_opts+=("$s=$t")
fi
done
# sleep while max number of jobs are running
until ((`jobs | wc -l` < maxnum)); do
sleep 1
done
debug "Encoding `basename "$flacfile"` to $outputfile"
(nice flac -dcs "$flacfile" 2>/dev/null | neroAacEnc $opt -if - -of "$outputfile" &>/dev/null &&
neroAacTag "$outputfile" "${tag_opts[@]}" &>/dev/null
check_exit_codes flac neroAacEnc neroAacTag) &
}
function convert_path
{
local base="$1" # with trailing slash
local dest="$2" # with trailing slash
local file="$3" # full path
local convpath="$4" # suffix
local backwards="$5" # whether file is a destination file,
# if so, convert it to the source file
if [ "$backwards" = "1" ]
then
local tmp=$base
base=$dest
dest=$tmp
fi
# files right under dest shall not have convpath
if [ -f "$file" -a "${file%/*}/" = "$base" ]
then
convpath=
fi
file=${file#"$base"}
file_substring=${file%%/*}
local replacement=$dest$file_substring
if [ "$backwards" = "1" ]
then
replacement=${replacement%"$convpath"}
else
replacement=$replacement$convpath
fi
file=${file/#"$file_substring"/"$replacement"}
echo "$file"
}
function convert_flacs
{
# getting the parameters
flacfile="$1"
outputfile="$2"
opt="$3"
ext="${outputfile##*.}"
outputfile="$(sed "s/\.m4aNero$/.m4a/" <<< "$outputfile")"
# check if the encoded file is older than the original flac file; if so, encode it!
if [ "$flacfile" -nt "$outputfile" ]
then
case "$ext" in
mp3) create_mp3 "$flacfile" "$opt" "$outputfile";;
ogg) create_ogg "$flacfile" "$opt" "$outputfile";;
m4a) create_aac "$flacfile" "$opt" "$outputfile";;
m4aNero) create_naac "$flacfile" "$opt" "$outputfile";;
esac
# find album path in order to touch the album dir to change last modified date
album_tmp=$(dirname "$outputfile")
album_dir="${album_tmp%/Disc [0-9]*}"
touch "$album_dir"
fi
}
function create_torrents
{
# getting the parameters
sourcefolder="$1"
announce="$2"
torrentpath="$3"
torrentfolder_new="$4"
conv="$5"
conv_create="$6"
flac_type="$7"
torrentname="${sourcefolder##*/}"
# Create .torrent file name to be used. If the conversion type was added during transcoding already, don't readd it
case "$conv_create" in
1) convpath="";;
*) convpath=" [$conv]";;
esac
# Check whether type should be added to the creation FLAC .torrents
case "$flac_type" in
2) convpath=" [$conv]";;
*) convpath="";;
esac
# sourcefolder may contain slashes, but outputfile must not
outputfile="${sourcefolder##*/}$convpath.torrent"
# create torrent
if [ ! -f "$torrentpath$outputfile" ]
then
mkdir -p "$torrentpath"
# sleep while max number of jobs are running
until ((`jobs | wc -l` < maxnum)); do
sleep 1
done
# start new job and add it to the background
nice mktorrent -n "$torrentname$convpath" -p -a "$announce" -o "$torrentpath$outputfile" "$sourcefolder" &
# if a .torrent already exists yet the folder has changed, create a new torrent in the new_torrent subfolder
elif [ "$sourcefolder" -nt "$torrentpath$outputfile" ]
then
mkdir -p "$torrentpath$torrentfolder_new"
# sleep while max number of jobs are running
until ((`jobs | wc -l` < maxnum)); do
sleep 1
done
# start new job and add it to the background
debug "Creating $torrentpath$torrentfolder_new$outputfile"
nice mktorrent -n "$torrentname$convpath" -p -a "$announce" -o "$torrentpath$torrentfolder_new$outputfile" "$sourcefolder" &
fi
}
# list all files with specified extensions in the specified directory
function find_exts
{
local path="$1"
shift
local exts=$@
[ $# -eq 0 ] && return 0
for fileext in ${exts[@]}
do
expr="$expr -o -iname \"*.$fileext\""
done
expr=${expr# -o }
eval "nice find \"$path\" -type f $expr"
}
# remove destination files and folders if corresponding files in flac folder
# don't exist anymore
function remove_obsolete_files
{
local dest="$1" # destination folder with trailing slash
local src="$2" # flac folder with trailing slash
local convpath="$3" # suffix
local ext="$4" # extension of trancscoded files
ext="$(sed "s/^m4aNero$/m4a/" <<< "$ext")"
# remove folders
find "$dest" -type d | while read folder
do
if [ -d "$folder" ] # skip already removed folders
then
srcfolder=$(convert_path "$src" "$dest" "$folder" "$convpath" 1)
if [ ! -d "$srcfolder" ]
then
rm -Rf "$folder"
fi
fi
done
# remove copied, linked and transcoded files
find_exts "$dest" ${copy_exts[@]} ${hard_link_exts[@]} \
${sym_link_exts[@]} "$ext" | while read destfile
do
srcfile=$(convert_path "$src" "$dest" "$destfile" "$convpath" 1)
if [ ! -e "$srcfile" ]
then
if [ "${destfile##*.}" = "$ext" ]
then
srcfile1=${srcfile%.*}.flac
srcfile2=${srcfile%.*}.FLAC
if [ ! -e "$srcfile1" -a ! -e "$srcfile2" ]
then
rm -f "$destfile"
fi
else
rm -f "$destfile"
fi
fi
done
}
#################################################################################
# SCRIPT CONTROL #
# do not edit below #
#################################################################################
source_profile "$@"
[ $? -eq 0 ] || exit $?
info "Starting the flacconvert script."
# determine maximal number of parallel jobs and add 1
maxnum=`grep -c '^processor' /proc/cpuinfo`
maxnum=$(($maxnum+$coreaddition))
# convert flacs
# check if current run level is set to create only .torrent files; if not, do conversion
if [ "$run_level" != "1" ]
then
info "Starting conversion of flac files..."
# if the flac folder does not exist, skip completely as nothing can be converted
if [ -d "$flacfolder" ]
then
for I in ${!conv_arr[*]}
do
conv="${conv_arr[$I]}"
dest="${dest_arr[$I]}"
ext="${ext_arr[$I]}"
opt="${opt_arr[$I]}"
case "$conv_create" in
1) convpath=" [$conv]";;
*) convpath="";;
esac
destfolder="$basefolder$dest"
info "Starting conversion to $conv files..."
if [ "$mirror" = "1" -a -d "$destfolder" ]
then
info "... removing obsolete files..."
remove_obsolete_files "$destfolder" "$flacfolder" \
"$convpath" "$ext"
fi
if [ ${#copy_exts[@]} -ge 1 ]
then
info "... copying files..."
find_exts "$flacfolder" ${copy_exts[@]} | while read extfile
do
file="$(convert_path "$flacfolder" "$destfolder" \
"$extfile" "$convpath")"
mkdir -p "$(dirname "$file")"
if [ "$extfile" -nt "$file" ]
then
debug "Copying $extfile to $file"
cp -a "$extfile" "$file"
fi
done
fi
if [ ${#hard_link_exts[@]} -ge 1 ]
then
info "... hard linking files..."
find_exts "$flacfolder" ${hard_link_exts[@]} | while read extfile
do
file="$(convert_path "$flacfolder" "$destfolder" \
"$extfile" "$convpath")"
mkdir -p "$(dirname "$file")"
if [ ! -e "$file" ]
then
debug "Link $extfile to $file"
ln "$extfile" "$file"
fi
done
fi
if [ ${#sym_link_exts[@]} -ge 1 ]
then
info "... symbolic linking files..."
find_exts "$flacfolder" ${sym_link_exts[@]} | while read extfile
do
file="$(convert_path "$flacfolder" "$destfolder" \
"$extfile" "$convpath")"
mkdir -p "$(dirname "$file")"
if [ ! -e "$file" ]
then
debug "Link $extfile to $file"
ln -s "$extfile" "$file"
fi
done
fi
info "... converting flac files..."
while read flacfile
do
outputfile="$(convert_path "$flacfolder" "$destfolder" \
"$flacfile" "$convpath")"
mkdir -p "$(dirname "$outputfile")"
outputfile="${outputfile%.*}.$ext"
# run convert_flacs function
convert_flacs "$flacfile" "$outputfile" "$opt"
# use process substitution instead of pipes to make wait work
# as wait won't wait for processes spawned by a subshell
done < <(nice find "$flacfolder" -iname '*.flac' )
done
wait
info "... conversion of flac files finished"
else
info "... no flac files found."
fi
fi
# check if current run level is set to only convert music files; if not, create .torrents
if [ "$run_level" != "0" ]
then
# create .torrent files
info "Starting creation of .torrent files..."
for I in ${!conv_arr[*]}
do
conv="${conv_arr[$I]}"
dest="${dest_arr[$I]}"
ext="${ext_arr[$I]}"
opt="${opt_arr[$I]}"
info "... for $conv ..."
# go to the right folder
case "$torrentsubfolder" in
1) torrentpath="$torrentfolder$dest";;
*) torrentpath="$torrentfolder";;
esac
if [ -d "$basefolder$dest" ]
then
# run the create torrent script, skip top directory
while read sourcefolder
do
# run create_torrents function
# Last parameter "0" is for flagging that this is not flac_create
create_torrents "$sourcefolder" "$announce_url" "$torrentpath" "$torrentfolder_new" "$conv" "$conv_create" "0"
done < <(find "$basefolder$dest" -maxdepth 1 -type d | grep -v "^$basefolder$dest\$")
info "... creation of .torrent files for $conv finished."
else
info "... no .torrent files for $conv created."
fi
done
# create .torrent files for the flac files
info "Starting creation of .torrent files..."
if [ "$flac_create" == "1" ]
then
info "... for $flac_conv ..."
# go to the right folder
case "$torrentsubfolder" in
1) torrentpath="$torrentfolder$flac_sub";;
*) torrentpath="$torrentfolder";;
esac
if [ -d "$flacfolder" ]
then
# run the create torrent script, skip top directory
while read sourcefolder
do
# run create_torrents function
# Use flac_type=1 to alaways add the $flac_conv to the torrent path
create_torrents "$sourcefolder" "$announce_url" "$torrentpath" "$torrentfolder_new" "$flac_conv" "$conv_create" "$flac_type"
done < <(find "$flacfolder" -maxdepth 1 -type d | grep -v "^$flacfolder\$")
info "... creation of .torrent files for $flac_conv finished."
else
info "... no .torrent files created."
fi
fi
wait
info "... creation of .torrent files finished."
fi