-
Notifications
You must be signed in to change notification settings - Fork 0
/
2flac.sh
executable file
·1618 lines (1479 loc) · 47.3 KB
/
2flac.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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# shellcheck disable=SC2001,SC2086,SC2207
# 2flac
# Various lossless to FLAC while keeping the tags.
# \(^o^)/
#
# Author : Romain Barbarot
# https://github.com/Jocker666z/2flac/
# Licence : unlicense
# Search & populate array with source files
search_source_files() {
local codec_test
mapfile -t lst_audio_src < <(find "$PWD" -maxdepth 3 -type f -regextype posix-egrep \
-iregex '.*\.('$input_ext')$' 2>/dev/null | sort)
# Only clean
for i in "${!lst_audio_src[@]}"; do
if [[ "${bits16_only}" = "1" ]] \
&& [[ "${cd_resample}" != "1" ]]; then
codec_test=$(ffprobe -v error -select_streams a:0 \
-show_entries stream=sample_fmt -of csv=s=x:p=0 "${lst_audio_src[i]}" )
if [[ "$codec_test" != "s16" ]] \
&& [[ "$codec_test" != "s16p" ]]; then
unset "lst_audio_src[i]"
fi
fi
if [[ "${alac_only}" = "1" ]] \
&& [[ "${lst_audio_src[i]##*.}" != "caf" \
&& "${lst_audio_src[i]##*.}" != "m4a" ]]; then
unset "lst_audio_src[i]"
fi
# Keep only ALAC codec in m4a
if [[ "${lst_audio_src[i]##*.}" = "caf" ]] \
|| [[ "${lst_audio_src[i]##*.}" = "m4a" ]]; then
codec_test=$(ffprobe -v error -select_streams a:0 \
-show_entries stream=codec_name -of csv=s=x:p=0 \
"${lst_audio_src[i]}" )
if [[ "$codec_test" != "alac" ]] \
&& [[ "$codec_test" != "pcm_"* ]]; then
unset "lst_audio_src[i]"
fi
fi
if [[ "${ape_only}" = "1" ]] \
&& [[ "${lst_audio_src[i]##*.}" != "ape" ]]; then
unset "lst_audio_src[i]"
fi
if [[ "${dsd_only}" = "1" ]] \
&& [[ "${lst_audio_src[i]##*.}" != "dsf" ]]; then
unset "lst_audio_src[i]"
fi
if [[ "${flac_only}" = "1" ]] \
&& [[ "${lst_audio_src[i]##*.}" != "flac" \
&& "${lst_audio_src[i]##*.}" != "ogg" ]]; then
unset "lst_audio_src[i]"
fi
# Keep only FLAC codec in ogg
if [[ "${lst_audio_src[i]##*.}" = "ogg" ]]; then
codec_test=$(ffprobe -v error -select_streams a:0 \
-show_entries stream=codec_name -of csv=s=x:p=0 \
"${lst_audio_src[i]}" )
if [[ "$codec_test" != "flac" ]]; then
unset "lst_audio_src[i]"
fi
fi
if [[ "${tta_only}" = "1" ]] \
&& [[ "${lst_audio_src[i]##*.}" != "tta" ]]; then
unset "lst_audio_src[i]"
fi
if [[ "${wav_only}" = "1" ]] \
&& [[ "${lst_audio_src[i]##*.}" != "wav" ]]; then
unset "lst_audio_src[i]"
fi
if [[ "${wavpack_only}" = "1" ]] \
&& [[ "${lst_audio_src[i]##*.}" != "wv" ]]; then
unset "lst_audio_src[i]"
fi
done
}
# Verify source integrity
test_source() {
local test_counter
test_counter="0"
# Test
for file in "${lst_audio_src[@]}"; do
# Progress
if ! [[ "$verbose" = "1" ]]; then
test_counter=$((test_counter+1))
if [[ "${#lst_audio_src[@]}" = "1" ]]; then
echo -ne "${test_counter}/${#lst_audio_src[@]} source file is being tested"\\r
else
echo -ne "${test_counter}/${#lst_audio_src[@]} source files are being tested"\\r
fi
fi
(
if [[ "${file##*.}" = "flac" ]]; then
flac $flac_test_arg "$file" 2>"${cache_dir}/${file##*/}.decode_error.log"
else
ffmpeg -v error -i "$file" \
-vn -sn -dn -max_muxing_queue_size 9999 \
-f null - 2>"${cache_dir}/${file##*/}.decode_error.log"
# Ignore ffmpeg non-blocking errors
if [ -s "${cache_dir}/${file##*/}.decode_error.log" ]; then
# [mjpeg @ ...] unable to decode APP fields...
if < "${cache_dir}/${file##*/}.decode_error.log" \
grep -E "mjpeg.*APP fields" &>/dev/null; then
rm "${cache_dir}/${file##*/}.decode_error.log"
# [... @ ...] Unknown attached ... image/webp.
elif < "${cache_dir}/${file##*/}.decode_error.log" \
grep -E "image/webp" &>/dev/null; then
rm "${cache_dir}/${file##*/}.decode_error.log"
fi
fi
fi
) &
if [[ $(jobs -r -p | wc -l) -ge $nproc ]]; then
wait -n
fi
done
wait
# Test if error generated
for file in "${lst_audio_src[@]}"; do
# FLAC - Special fix loop
if [[ "${file##*.}" = "flac" ]]; then
# Error log test
if [ -s "${cache_dir}/${file##*/}.decode_error.log" ]; then
# Try to fix file
flac $flac_fix_arg "$file"
# Re-test, if no valid 2 times exclude
flac $flac_test_arg "$file" 2>"${cache_dir}/${file##*/}.decode_error.log"
fi
fi
# Errors validation
if [ -s "${cache_dir}/${file##*/}.decode_error.log" ]; then
mv "${cache_dir}/${file##*/}.decode_error.log" "${file}.decode_error.log"
lst_audio_src_rejected+=( "$file" )
else
rm "${cache_dir}/${file##*/}.decode_error.log" 2>/dev/null
lst_audio_src_pass+=( "$file" )
fi
done
# Progress end
if ! [[ "$verbose" = "1" ]]; then
tput hpa 0; tput el
if (( "${#lst_audio_src_rejected[@]}" )); then
if [[ "${#lst_audio_src[@]}" = "1" ]]; then
echo "${test_counter} source file tested ~ ${#lst_audio_src_rejected[@]} in error (log generated)"
else
echo "${test_counter} source files tested ~ ${#lst_audio_src_rejected[@]} in error (log generated)"
fi
else
if [[ "${#lst_audio_src[@]}" = "1" ]]; then
echo "${test_counter} source file tested"
else
echo "${test_counter} source files tested"
fi
fi
fi
# All source files size record
total_source_files_size=$(calc_files_size "${lst_audio_src_pass[@]}")
# Individual source file size record
for file in "${lst_audio_src_pass[@]}"; do
file_source_files_size+=( "$(get_files_size_bytes "${file}")" )
done
}
# Decode source
decode_source() {
local decode_counter
local test_bit
local ffmpeg_arg_bit
decode_counter="0"
for file in "${lst_audio_src_pass[@]}"; do
(
if [[ "${file##*.}" = "ape" ]] \
|| [[ "${file##*.}" = "caf" ]] \
|| [[ "${file##*.}" = "m4a" ]] \
|| [[ "${file##*.}" = "tta" ]] \
|| [[ "${file##*.}" = "wv" ]]; then
# Bit test, for prevent bad convert in auto
test_bit=$(ffprobe -v panic -show_entries \
stream=sample_fmt -print_format csv=p=0 "$file")
# 8 & 16bit
if [[ "$test_bit" == "s8"* ]] \
|| [[ "$test_bit" == "u8"* ]] \
|| [[ "$test_bit" == "s16"* ]]; then
ffmpeg_arg_bit="-c:a pcm_s16le"
# 24bit
elif [[ "$test_bit" == "s24"* ]]; then
ffmpeg_arg_bit="-c:a pcm_s24le"
# 32 & 64bit
elif [[ "$test_bit" == "s32"* ]] \
|| [[ "$test_bit" = "f32" ]] \
|| [[ "$test_bit" = "fltp" ]] \
|| [[ "$test_bit" == "f64"* ]] \
|| [[ "$test_bit" == "s64"* ]] \
|| [[ "$test_bit" = "dblp" ]]; then
ffmpeg_arg_bit="-c:a pcm_s32le"
fi
ffmpeg $ffmpeg_log_lvl -y -i "$file" $ffmpeg_arg_bit "${cache_dir}/${file##*/}.wav"
elif [[ "${file##*.}" = "dsf" ]]; then
ffmpeg $ffmpeg_log_lvl -y -i "$file" \
-c:a pcm_s32le -ar 384000 \
"${cache_dir}/${file##*/}.wav"
fi
) &
if [[ $(jobs -r -p | wc -l) -ge $nproc ]]; then
wait -n
fi
# FLAC target array
if [[ "${file##*.}" = "flac" ]] \
|| [[ "${file##*.}" = "ogg" ]] \
|| [[ "${file##*.}" = "wav" ]]; then
lst_audio_wav_decoded+=( "$file" )
else
decode_counter=$((decode_counter+1))
lst_audio_wav_decoded+=( "${cache_dir}/${file##*/}.wav" )
fi
# Array of source at remove
if [[ "${file##*.}" != "flac" ]]; then
lst_audio_src_to_remove+=( "$file" )
fi
# Progress
if ! [[ "$verbose" = "1" ]]; then
if [[ "${#lst_audio_src_pass[@]}" = "1" ]]; then
echo -ne "${decode_counter}/${#lst_audio_src_pass[@]} source file decoded"\\r
else
echo -ne "${decode_counter}/${#lst_audio_src_pass[@]} source files decoded"\\r
fi
fi
done
wait
# Progress end
if [[ "$verbose" != "1" ]];then
tput hpa 0; tput el
if [[ "${#lst_audio_src_pass[@]}" = "1" ]]; then
echo "${decode_counter} source file decoded"
else
echo "${decode_counter} source files decoded"
fi
fi
}
# Convert tag to VORBIS
apev2_sub () {
source_tag[i]="${source_tag[i]//Album Artist=/ALBUMARTIST=}"
source_tag[i]="${source_tag[i]//Arranger=/ARRANGER=}"
source_tag[i]="${source_tag[i]//Barcode=/BARCODE=}"
source_tag[i]="${source_tag[i]//CatalogNumber=/CATALOGNUMBER=}"
source_tag[i]="${source_tag[i]//Comment=/COMMENT=}"
source_tag[i]="${source_tag[i]//Compilation=/COMPILATION=}"
source_tag[i]="${source_tag[i]//Composer=/COMPOSER=}"
source_tag[i]="${source_tag[i]//Conductor=/CONDUCTOR=}"
source_tag[i]="${source_tag[i]//Copyright=/COPYRIGHT=}"
source_tag[i]="${source_tag[i]//Year=/DATE=}"
source_tag[i]="${source_tag[i]//Director=/DIRECTOR=}"
source_tag[i]="${source_tag[i]//Disc=/DISCNUMBER=}"
source_tag[i]="${source_tag[i]//DiscSubtitle=/DISCSUBTITLE=}"
source_tag[i]="${source_tag[i]//DJMixer=/DJMIXER=}"
source_tag[i]="${source_tag[i]//Engineer=/ENGINEER=}"
source_tag[i]="${source_tag[i]//Genre=/GENRE=}"
source_tag[i]="${source_tag[i]//Grouping=/GROUPING=}"
source_tag[i]="${source_tag[i]//Label=/LABEL=}"
source_tag[i]="${source_tag[i]//Language=/LANGUAGE=}"
source_tag[i]="${source_tag[i]//Lyricist=/LYRICIST=}"
source_tag[i]="${source_tag[i]//Lyrics=/LYRICS=}"
source_tag[i]="${source_tag[i]//Media=/MEDIA=}"
source_tag[i]="${source_tag[i]//Mixer=/MIXER=}"
source_tag[i]="${source_tag[i]//Mood=/MOOD=}"
source_tag[i]="${source_tag[i]//Performer=/PERFORMER=}"
source_tag[i]="${source_tag[i]//MUSICBRAINZ_ALBUMSTATUS=/RELEASESTATUS=}"
source_tag[i]="${source_tag[i]//MUSICBRAINZ_ALBUMTYPE=/RELEASETYPE=}"
source_tag[i]="${source_tag[i]//MixArtist=/REMIXER=}"
source_tag[i]="${source_tag[i]//Script=/SCRIPT=}"
source_tag[i]="${source_tag[i]//Subtitle=/SUBTITLE=}"
source_tag[i]="${source_tag[i]//Title=/TITLE=}"
source_tag[i]="${source_tag[i]//Track=/TRACKNUMBER=}"
source_tag[i]="${source_tag[i]//Weblink=/WEBSITE=}"
source_tag[i]="${source_tag[i]//Writer=/WRITER=}"
}
id3v2_sub () {
source_tag[i]="${source_tag[i]//TALB=/ALBUM=}"
source_tag[i]="${source_tag[i]//TBPM=/BPM=}"
source_tag[i]="${source_tag[i]//TCMP=/COMPILATION=}"
source_tag[i]="${source_tag[i]//TCON=/GENRE=}"
source_tag[i]="${source_tag[i]//TCOP=/COPYRIGHT=}"
source_tag[i]="${source_tag[i]//TDOR=/ORIGINALDATE=}"
source_tag[i]="${source_tag[i]//TDRC=/DATE=}"
source_tag[i]="${source_tag[i]//TEXT=/LYRICIST=}"
source_tag[i]="${source_tag[i]//TIT1=/GROUPING=}"
source_tag[i]="${source_tag[i]//TIT2=/TITLE=}"
source_tag[i]="${source_tag[i]//TIT3=/SUBTITLE=}"
source_tag[i]="${source_tag[i]//TLAN=/LANGUAGE=}"
source_tag[i]="${source_tag[i]//TMED=/MEDIA=}"
source_tag[i]="${source_tag[i]//TMOO=/MOOD=}"
source_tag[i]="${source_tag[i]//TOFN=/ORIGINALFILENAME=}"
source_tag[i]="${source_tag[i]//TPOS=/DISCNUMBER=}"
source_tag[i]="${source_tag[i]//TPE1=/ARTIST=}"
source_tag[i]="${source_tag[i]//TPE2=/ALBUMARTIST=}"
source_tag[i]="${source_tag[i]//TPE3=/CONDUCTOR=}"
source_tag[i]="${source_tag[i]//TPE4=/REMIXER=}"
source_tag[i]="${source_tag[i]//TPUB=/LABEL=}"
source_tag[i]="${source_tag[i]//TRCK=/TRACKNUMBER=}"
source_tag[i]="${source_tag[i]//TSO2=/ALBUMARTISTSORT=}"
source_tag[i]="${source_tag[i]//TSOC=/COMPOSERSORT=}"
source_tag[i]="${source_tag[i]//TSOP=/ARTISTSORT=}"
source_tag[i]="${source_tag[i]//TSOT=/TITLESORT=}"
source_tag[i]="${source_tag[i]//TSRC=/ISRC=}"
source_tag[i]="${source_tag[i]//TSST=/DISCSUBTITLE=}"
source_tag[i]="${source_tag[i]//TXXX=Acoustid Id=/ACOUSTID_ID=}"
source_tag[i]="${source_tag[i]//TXXX=Acoustid Fingerprint=/ACOUSTID_FINGERPRINT=}"
source_tag[i]="${source_tag[i]//TXXX=ARTISTS=/ARTISTS=}"
source_tag[i]="${source_tag[i]//TXXX=ASIN=/ASIN=}"
source_tag[i]="${source_tag[i]//TXXX=BARCODE=/BARCODE=}"
source_tag[i]="${source_tag[i]//TXXX=CATALOGNUMBER=/CATALOGNUMBER=}"
source_tag[i]="${source_tag[i]//TXXX=COMPOSERSORT=/COMPOSERSORT=}"
source_tag[i]="${source_tag[i]//TXXX=DIRECTOR=/DIRECTOR=}"
source_tag[i]="${source_tag[i]//TXXX=LICENSE=/LICENSE=}"
source_tag[i]="${source_tag[i]//TXXX=MusicBrainz Album Artist Id=/MUSICBRAINZ_ALBUMARTISTID=}"
source_tag[i]="${source_tag[i]//TXXX=MusicBrainz Album Id=/MUSICBRAINZ_ALBUMID=}"
source_tag[i]="${source_tag[i]//TXXX=MusicBrainz Album Status=/RELEASESTATUS=}"
source_tag[i]="${source_tag[i]//TXXX=MusicBrainz Album Type=/RELEASETYPE=}"
source_tag[i]="${source_tag[i]//TXXX=MusicBrainz Artist Id=/MUSICBRAINZ_ARTISTID=}"
source_tag[i]="${source_tag[i]//TXXX=MusicBrainz Album Release Country=/RELEASECOUNTRY=}"
source_tag[i]="${source_tag[i]//TXXX=MusicBrainz Release Group Id=/MUSICBRAINZ_RELEASEGROUPID=}"
source_tag[i]="${source_tag[i]//TXXX=MusicBrainz Release Track Id=/MUSICBRAINZ_RELEASETRACKID=}"
source_tag[i]="${source_tag[i]//TXXX=MusicBrainz TRM Id=/MUSICBRAINZ_TRMID=}"
source_tag[i]="${source_tag[i]//TXXX=MusicBrainz Work Id=/MUSICBRAINZ_WORKID=}"
source_tag[i]="${source_tag[i]//TXXX=MusicIP PUID=/MUSICIP_PUID=}"
source_tag[i]="${source_tag[i]//TXXX=REPLAYGAIN_ALBUM_GAIN=/REPLAYGAIN_ALBUM_GAIN=}"
source_tag[i]="${source_tag[i]//TXXX=REPLAYGAIN_ALBUM_PEAK=/REPLAYGAIN_ALBUM_PEAK=}"
source_tag[i]="${source_tag[i]//TXXX=REPLAYGAIN_ALBUM_RANGE=/REPLAYGAIN_ALBUM_RANGE=}"
source_tag[i]="${source_tag[i]//TXXX=REPLAYGAIN_REFERENCE_LOUDNESS=/REPLAYGAIN_REFERENCE_LOUDNESS=}"
source_tag[i]="${source_tag[i]//TXXX=REPLAYGAIN_TRACK_GAIN=/REPLAYGAIN_TRACK_GAIN=}"
source_tag[i]="${source_tag[i]//TXXX=REPLAYGAIN_TRACK_PEAK=/REPLAYGAIN_TRACK_PEAK=}"
source_tag[i]="${source_tag[i]//TXXX=REPLAYGAIN_TRACK_RANGE=/REPLAYGAIN_TRACK_RANGE=}"
source_tag[i]="${source_tag[i]//TXXX=SCRIPT=/SCRIPT=}"
source_tag[i]="${source_tag[i]//TXXX=SHOWMOVEMENT=/SHOWMOVEMENT=}"
source_tag[i]="${source_tag[i]//TXXX=WORK=/WORK=}"
source_tag[i]="${source_tag[i]//TXXX=Writer=/WRITER=}"
source_tag[i]="${source_tag[i]//UFID=/MUSICBRAINZ_TRACKID=}"
source_tag[i]="${source_tag[i]//WCOP=/LICENSE=}"
source_tag[i]="${source_tag[i]//WOAR=/WEBSITE=}"
}
itune_sub () {
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:Acoustid Id=/ACOUSTID_ID=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:Acoustid Fingerprint=/ACOUSTID_FINGERPRINT=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:ARTISTS=/ARTISTS=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:ASIN=/ASIN=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:BARCODE=/BARCODE=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:CATALOGNUMBER=/CATALOGNUMBER=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:CONDUCTOR=/CONDUCTOR=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:DISCSUBTITLE=/DISCSUBTITLE=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:DJMIXER=/DJMIXER=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:ENGINEER=/ENGINEER=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:ISRC=/ISRC=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:LABEL=/LABEL=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:LANGUAGE=/LANGUAGE=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:LICENSE=/LICENSE=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:LYRICIST=/LYRICIST=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:MEDIA=/MEDIA=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:MIXER=/MIXER=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:MOOD=/MOOD=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:MusicBrainz Album Artist Id=/MUSICBRAINZ_ALBUMARTISTID=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:MusicBrainz Album Id=/MUSICBRAINZ_ALBUMID=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:MusicBrainz Album Release Country=/RELEASECOUNTRY=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:MusicBrainz Album Status=/RELEASESTATUS=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:MusicBrainz Album Type=/RELEASETYPE=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:MusicBrainz Artist Id=/MUSICBRAINZ_ARTISTID=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:MusicBrainz Release Group Id=/MUSICBRAINZ_RELEASEGROUPID=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:MusicBrainz Release Track Id=/MUSICBRAINZ_RELEASETRACKID=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:MusicBrainz Track Id=/MUSICBRAINZ_TRACKID=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:MusicBrainz TRM Id=/MUSICBRAINZ_TRMID=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:MusicBrainz Work Id=/MUSICBRAINZ_WORKID=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:MusicIP PUID=/MUSICIP_PUID=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:REMIXER=/REMIXER=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:REPLAYGAIN_ALBUM_GAIN=/REPLAYGAIN_ALBUM_GAIN=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:REPLAYGAIN_ALBUM_PEAK=/REPLAYGAIN_ALBUM_PEAK=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:REPLAYGAIN_ALBUM_RANGE=/REPLAYGAIN_ALBUM_RANGE=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:REPLAYGAIN_REFERENCE_LOUDNESS=/REPLAYGAIN_REFERENCE_LOUDNESS=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:REPLAYGAIN_TRACK_GAIN=/REPLAYGAIN_TRACK_GAIN=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:REPLAYGAIN_TRACK_PEAK=/REPLAYGAIN_TRACK_PEAK=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:REPLAYGAIN_TRACK_RANGE=/REPLAYGAIN_TRACK_RANGE=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:SCRIPT=/SCRIPT=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:SUBTITLE=/SUBTITLE=}"
source_tag[i]="${source_tag[i]//©alb=/ALBUM=}"
source_tag[i]="${source_tag[i]//©ART=/ARTIST=}"
source_tag[i]="${source_tag[i]//©cmt=/COMMENT=}"
source_tag[i]="${source_tag[i]//©day=/DATE=}"
source_tag[i]="${source_tag[i]//©dir=/DIRECTOR=}"
source_tag[i]="${source_tag[i]//©gen=/GENRE=}"
source_tag[i]="${source_tag[i]//©grp=/GROUPING=}"
source_tag[i]="${source_tag[i]//©mvn=/MOVEMENTNAME=}"
source_tag[i]="${source_tag[i]//©nam=/TITLE=}"
source_tag[i]="${source_tag[i]//©wrk=/WORK=}"
source_tag[i]="${source_tag[i]//aART=/ALBUMARTIST=}"
source_tag[i]="${source_tag[i]//cpil=/COMPILATION=}"
source_tag[i]="${source_tag[i]//cprt=/COPYRIGHT=}"
source_tag[i]="${source_tag[i]//disk=/DISCNUMBER=}"
source_tag[i]="${source_tag[i]//mvc=/MOVEMENTTOTAL=}"
source_tag[i]="${source_tag[i]//mvi=/MOVEMENT=}"
source_tag[i]="${source_tag[i]//shwm=/SHOWMOVEMENT=}"
source_tag[i]="${source_tag[i]//soaa=/ALBUMARTISTSORT=}"
source_tag[i]="${source_tag[i]//soar=/ARTISTSORT=}"
source_tag[i]="${source_tag[i]//soco=/COMPOSERSORT=}"
source_tag[i]="${source_tag[i]//sonm=/TITLESORT=}"
source_tag[i]="${source_tag[i]//trkn=/TRACKNUMBER=}"
}
other_sub () {
# MusicBrainz internal name
source_tag[i]="${source_tag[i]//albumartistsort=/ALBUMARTISTSORT=}"
source_tag[i]="${source_tag[i]//artistsort=/ARTISTSORT=}"
source_tag[i]="${source_tag[i]//musicbrainz_albumid=/MUSICBRAINZ_ALBUMID=}"
source_tag[i]="${source_tag[i]//musicbrainz_artistid=/MUSICBRAINZ_ARTISTID=}"
source_tag[i]="${source_tag[i]//musicbrainz_recordingid=/MUSICBRAINZ_TRACKID=}"
source_tag[i]="${source_tag[i]//musicbrainz_releasegroupid=/MUSICBRAINZ_RELEASEGROUPID=}"
source_tag[i]="${source_tag[i]//originalyear=/ORIGINALYEAR=}"
source_tag[i]="${source_tag[i]//replaygain_album_gain=/REPLAYGAIN_ALBUM_GAIN=}"
source_tag[i]="${source_tag[i]//replaygain_album_peak=/REPLAYGAIN_ALBUM_PEAK=}"
source_tag[i]="${source_tag[i]//replaygain_track_gain=/REPLAYGAIN_TRACK_GAIN=}"
source_tag[i]="${source_tag[i]//replaygain_track_peak=/REPLAYGAIN_TRACK_PEAK=}"
# Waste fix
shopt -s nocasematch
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:originaldate=/ORIGINALDATE=}"
source_tag[i]="${source_tag[i]//----:com.apple.iTunes:originalyear=/ORIGINALYEAR=}"
source_tag[i]="${source_tag[i]//date=/DATE=}"
source_tag[i]="${source_tag[i]//originaldate=/ORIGINALDATE=}"
source_tag[i]="${source_tag[i]//TXXX=originalyear=/ORIGINALYEAR=}"
shopt -u nocasematch
}
tags_2_flac() {
local exclude_from_tag_loop
local cover_test
local cover_image_type
local cover_ext
local tag_label
local grab_tag_counter
local tag_trick
local tag_trick_str
local flac_block_type
grab_tag_counter="0"
for file in "${lst_audio_flac_compressed[@]}"; do
# Reset
unset exclude_from_tag_loop
unset source_tag
unset source_tag_temp
unset source_tag_temp1
unset source_tag_temp2
unset tag_name
unset tag_label
unset tag_trick
unset tag_trick_str
# Target file
if [[ -s "${file%.*}.ape" ]]; then
file="${file%.*}.ape"
elif [[ -s "${file%.*}.dsf" ]]; then
file="${file%.*}.dsf"
elif [[ -s "${file%.*}.m4a" ]]; then
file="${file%.*}.m4a"
elif [[ -s "${file%.*}.tta" ]]; then
file="${file%.*}.tta"
elif [[ -s "${file%.*}.wav" ]]; then
file="${file%.*}.wav"
elif [[ -s "${file%.*}.wv" ]]; then
file="${file%.*}.wv"
else
exclude_from_tag_loop="1"
fi
# Get tag if src != wav or flac
if [[ "$exclude_from_tag_loop" != "1" ]]; then
# If ReplayGain or not
if [[ "$replay_gain" != "1" ]]; then
Vorbis_whitelist=("${Vorbis_whitelist[@]}" "${Vorbis_whitelist_replaygain[@]}")
fi
# Source file tags array
mapfile -t source_tag < <( mutagen-inspect "$file" )
# itune need clean
if [[ -s "${file%.*}.m4a" ]]; then
for i in "${!source_tag[@]}"; do
source_tag[i]="${source_tag[i]//MP4FreeForm(b\'/}"
source_tag[i]="${source_tag[i]//\', <AtomDataType.UTF8: 1>)/}"
if [[ "${source_tag[i]}" = "disk="* ]] \
|| [[ "${source_tag[i]}" = *"trkn="* ]]; then
source_tag[i]="${source_tag[i]//disk=(/disk=}"
source_tag[i]="${source_tag[i]//trkn=(/trkn=}"
source_tag[i]="${source_tag[i]//, //}"
source_tag[i]="${source_tag[i]//)/}"
fi
done
fi
# Remove empty tag label=
mapfile -t source_tag < <( printf '%s\n' "${source_tag[@]}" | grep "=" )
# Exclude no tag source; & FLAC, WAV
if (( "${#source_tag[@]}" )); then
# Substitution
for i in "${!source_tag[@]}"; do
# APEv2
apev2_sub
# ID3v2
id3v2_sub
# iTune
itune_sub
# MusicBrainz internal name & waste
other_sub
done
# Array tag name & label
mapfile -t tag_name < <( printf '%s\n' "${source_tag[@]}" | awk -F "=" '{print $1}' )
mapfile -t tag_label < <( printf '%s\n' "${source_tag[@]}" | cut -f2- -d'=' )
# Whitelist parsing
for i in "${!tag_name[@]}"; do
for tag in "${Vorbis_whitelist[@]}"; do
# Vorbis std
if [[ "${tag_name[i],,}" = "${tag,,}" ]] \
&& [[ -n "${tag_label[i]// }" ]]; then
# Picard std
if [[ "${tag}" = "TRACKNUMBER" ]] \
&& [[ "${tag_label[i]}" = *"/"* ]]; then
source_tag+=( "TOTALTRACKS=${tag_label[i]#*/}" )
fi
if [[ "${tag}" = "DISCNUMBER" ]] \
&& [[ "${tag_label[i]}" = *"/"* ]]; then
source_tag+=( "TOTALDISCS=${tag_label[i]#*/}" )
fi
if [[ "${tag}" = "TRACKNUMBER" ]] \
|| [[ "${tag}" = "DISCNUMBER" ]]; then
tag_label[i]="${tag_label[i]%/*}"
fi
if [[ "${tag}" = "LABEL" ]] \
&& [[ "${tag_label[i]}" = *"\xc"* ]]; then
tag_label[i]=$(printf "%b" "${tag_label[i]}")
fi
if [[ "${tag}" = "ARTISTS" ]] \
&& [[ "${tag_label[i]}" = *"/"* ]]; then
mapfile -t tag_trick < <( echo "${tag_label[i]//\//|}" \
| tr "|" "\n" \
| awk '$1=$1' )
for type in "${tag_trick[@]}"; do
source_tag+=( "ARTISTS=${type}" )
done
elif [[ "${tag}" = "MUSICBRAINZ_ARTISTID" ]] \
&& [[ "${tag_label[i]}" = *"/"* ]]; then
mapfile -t tag_trick < <( echo "${tag_label[i]//\//|}" \
| tr "|" "\n" \
| awk '$1=$1' )
for type in "${tag_trick[@]}"; do
source_tag+=( "MUSICBRAINZ_ARTISTID=${type}" )
done
elif [[ "${tag}" = "ISRC" ]] \
&& [[ "${tag_label[i]}" = *"/"* ]]; then
mapfile -t tag_trick < <( echo "${tag_label[i]//\//|}" \
| tr "|" "\n" \
| awk '$1=$1' )
for type in "${tag_trick[@]}"; do
source_tag+=( "ISRC=${type}" )
done
elif [[ "${tag}" = "LABEL" ]] \
&& [[ "${tag_label[i]}" = *"/"* ]]; then
mapfile -t tag_trick < <( echo "${tag_label[i]//\//|}" \
| tr "|" "\n" \
| awk '$1=$1' )
for type in "${tag_trick[@]}"; do
source_tag+=( "LABEL=${type}" )
done
elif [[ "${tag}" = "MUSICBRAINZ_TRACKID" ]] \
&& [[ "${tag_label[i]}" = *"'"* ]]; then
tag_trick_str=$(echo "${tag_label[i]}" \
| cut -d "'" -f2)
source_tag+=( "MUSICBRAINZ_TRACKID=${tag_trick_str}" )
elif [[ "${tag}" = "MUSICBRAINZ_ALBUMARTISTID" ]] \
&& [[ "${tag_label[i]}" = *"/"* ]]; then
mapfile -t tag_trick < <( echo "${tag_label[i]//\//|}" \
| tr "|" "\n" \
| awk '$1=$1' )
for type in "${tag_trick[@]}"; do
source_tag+=( "MUSICBRAINZ_ALBUMARTISTID=${type}" )
done
elif [[ "${tag}" = "RELEASETYPE" ]] \
&& [[ "${tag_label[i]}" = *"/"* ]]; then
mapfile -t tag_trick < <( echo "${tag_label[i]//\//|}" \
| tr "|" "\n" \
| awk '$1=$1' )
for type in "${tag_trick[@]}"; do
source_tag+=( "RELEASETYPE=${type}" )
done
elif [[ "${tag}" = "PERFORMER" ]] \
&& [[ "${tag_label[i]}" = *"/"* ]]; then
mapfile -t tag_trick < <( echo "${tag_label[i]//\//|}" \
| tr "|" "\n" \
| awk '$1=$1' )
for type in "${tag_trick[@]}"; do
source_tag+=( "PERFORMER=${type}" )
done
else
# Array of tag
source_tag[i]="${tag}=${tag_label[i]}"
fi
continue 2
# reject
else
unset "source_tag[i]"
fi
done
done
# Remove duplicate tags
mapfile -t source_tag < <( printf '%s\n' "${source_tag[@]}" | uniq -u )
# Tag FLAC
for i in "${!source_tag[@]}"; do
metaflac "${file%.*}.flac" --set-tag="${source_tag[i]}"
done
# Tag counter
grab_tag_counter=$((grab_tag_counter+1))
fi
# Progress
if ! [[ "$verbose" = "1" ]]; then
if [[ "${#lst_audio_flac_compressed[@]}" = "1" ]]; then
echo -ne "${grab_tag_counter}/${#lst_audio_flac_compressed[@]} flac file is being tagged"\\r
else
echo -ne "${grab_tag_counter}/${#lst_audio_flac_compressed[@]} flac files are being tagged"\\r
fi
fi
fi
# Try to extract cover
if [[ "$exclude_from_tag_loop" != "1" ]] \
&& [[ "$extract_cover_no" != "1" ]]; then
cover_test=$(ffprobe -v error -select_streams v:0 \
-show_entries stream=codec_name -of csv=s=x:p=0 \
"$file" 2>/dev/null)
if [[ -n "$cover_test" ]]; then
if [[ "$cover_test" = "png" ]]; then
cover_ext="png"
elif [[ "$cover_test" = *"jpeg"* ]]; then
cover_ext="jpg"
fi
ffmpeg $ffmpeg_log_lvl -n -i "$file" \
"${file%.*}.$cover_ext" 2>/dev/null
fi
elif [[ ! -s "${file%.*}.caf" ]] \
&& [[ ! -s "${file%.*}.wav" ]] \
&& [[ "$exclude_from_tag_loop" = "1" ]] \
&& [[ "$extract_cover_no" != "1" ]]; then
cover_test=$(metaflac --list "${file%.*}.flac" \
| grep -A 8 METADATA 2>/dev/null \
| grep -A 7 -B 1 PICTURE 2>/dev/null)
if [[ -n "$cover_test" ]]; then
# Image type
cover_image_type=$(echo "$cover_test" \
| grep "MIME type" \
| awk -F " " '{print $NF}' \
| awk -F "/" '{print $NF}' \
| head -1)
if [[ "$cover_image_type" = "png" ]]; then
cover_ext="png"
elif [[ "$cover_image_type" = "jpeg" ]]; then
cover_ext="jpg"
elif [[ "$cover_image_type" = "webp" ]]; then
cover_ext="webp"
fi
# Extract
metaflac "${file%.*}.flac" \
--export-picture-to="${file%.*}.$cover_ext"
fi
fi
done
# Delete ReplayGain if active
if [[ "$replay_gain" = "1" || "$rm_replay_gain" = "1" ]]; then
for file in "${lst_audio_flac_compressed[@]}"; do
(
metaflac "${file%.*}.flac" \
--remove-replay-gain
) &
if [[ $(jobs -r -p | wc -l) -ge $nproc ]]; then
wait -n
fi
done
fi
# FLAC->FLAC Delete embedded
if [[ "$extract_cover_no" != "1" ]]; then
for file in "${lst_audio_flac_compressed[@]}"; do
# Reset
unset exclude_from_tag_loop
# Target file
if [[ ! -s "${file%.*}.ape" ]] \
&& [[ ! -s "${file%.*}.caf" ]] \
&& [[ ! -s "${file%.*}.dsf" ]] \
&& [[ ! -s "${file%.*}.m4a" ]] \
&& [[ ! -s "${file%.*}.tta" ]] \
&& [[ ! -s "${file%.*}.wv" ]] \
&& [[ ! -s "${file%.*}.wav" ]]; then
exclude_from_tag_loop="1"
fi
(
if [[ "$exclude_from_tag_loop" = "1" ]]; then
metaflac "${file%.*}.flac" \
--remove --block-type=PICTURE \
--dont-use-padding
fi
) &
if [[ $(jobs -r -p | wc -l) -ge $nproc ]]; then
wait -n
fi
done
wait
fi
# Progress end
if ! [[ "$verbose" = "1" ]]; then
tput hpa 0; tput el
if [[ "${#lst_audio_flac_compressed[@]}" = "1" ]]; then
echo "${grab_tag_counter} flac file tagged"
else
echo "${grab_tag_counter} flac files tagged"
fi
fi
}
# FLAC - Compress
compress_flac() {
local compress_counter
compress_counter="0"
for i in "${!lst_audio_src_pass[@]}"; do
# Compress FLAC
(
if [[ "$verbose" = "1" ]]; then
flac $flac_compress_arg "${lst_audio_wav_decoded[i]}" \
-o "${lst_audio_src_pass[i]%.*}".flac
else
flac $flac_compress_arg "${lst_audio_wav_decoded[i]}" \
-o "${lst_audio_src_pass[i]%.*}".flac &>/dev/null
fi
) &
if [[ $(jobs -r -p | wc -l) -ge $nproc ]]; then
wait -n
fi
# Progress
if ! [[ "$verbose" = "1" ]]; then
compress_counter=$((compress_counter+1))
if [[ "${#lst_audio_wav_decoded[@]}" = "1" ]]; then
echo -ne "${compress_counter}/${#lst_audio_wav_decoded[@]} flac file is being compressed"\\r
else
echo -ne "${compress_counter}/${#lst_audio_wav_decoded[@]} flac files are being compressed"\\r
fi
fi
done
wait
# Progress end
if ! [[ "$verbose" = "1" ]]; then
tput hpa 0; tput el
if [[ "${#lst_audio_wav_decoded[@]}" = "1" ]]; then
echo "${compress_counter} flac file compressed"
else
echo "${compress_counter} flac files compressed"
fi
fi
# Clean + target array
for i in "${!lst_audio_wav_decoded[@]}"; do
# Array of FLAC target
lst_audio_flac_compressed+=( "${lst_audio_src_pass[i]%.*}.flac" )
# Remove temp wav files
if [[ "${lst_audio_src[i]##*.}" != "wav" ]] \
&& [[ "${lst_audio_src[i]##*.}" != "ogg" ]] \
&& [[ "${lst_audio_src[i]##*.}" != "flac" ]]; then
rm -f "${lst_audio_wav_decoded[i]%.*}.wav" 2>/dev/null
fi
done
}
# CD Resample 16/44.1
cd_format() {
local sox_counter
local source_hz
local source_bit
local source_channel
sox_counter="0"
if [[ "$cd_resample" = "1" ]]; then
for i in "${!lst_audio_wav_decoded[@]}"; do
# Test source
source_hz=$(sox --i -r "${lst_audio_wav_decoded[i]}")
source_bit=$(sox --i -b "${lst_audio_wav_decoded[i]}")
source_channel=$(sox --i -c "${lst_audio_wav_decoded[i]}")
# Counter
if [[ "$source_hz" != "44100" ]] \
|| [[ "$source_bit" != "16" ]] \
|| [[ "$source_channel" != "2" ]]; then
sox_counter=$((sox_counter+1))
fi
# Resample
(
if [[ "$verbose" = "1" ]]; then
if [[ "$source_hz" != "44100" \
&& "$source_bit" != "16" \
&& "$source_channel" = "2" ]]; then
sox -S "${lst_audio_wav_decoded[i]}" \
-C 0 -b 16 "${cache_dir}/${lst_audio_wav_decoded[i]##*/}.sox.flac" \
rate -v -L -s 44100 dither
elif [[ "$source_hz" != "44100" \
&& "$source_bit" != "16" \
&& "$source_channel" != "2" ]]; then
sox -S "${lst_audio_wav_decoded[i]}" \
-C 0 -c 2 -b 16 \
"${cache_dir}/${lst_audio_wav_decoded[i]##*/}.sox.flac" \
rate -v -L -s 44100 dither
elif [[ "$source_hz" != "44100" \
&& "$source_bit" = "16" \
&& "$source_channel" = "2" ]]; then
sox -S "${lst_audio_wav_decoded[i]}" \
-C 0 \
"${cache_dir}/${lst_audio_wav_decoded[i]##*/}.sox.flac" \
rate -v -L -s 44100 dither
elif [[ "$source_hz" != "44100" \
&& "$source_bit" = "16" \
&& "$source_channel" != "2" ]]; then
sox -S "${lst_audio_wav_decoded[i]}" \
-C 0 -c 2 \
"${cache_dir}/${lst_audio_wav_decoded[i]##*/}.sox.flac" \
rate -v -L -s 44100 dither
elif [[ "$source_hz" = "44100" \
&& "$source_bit" != "16" \
&& "$source_channel" = "2" ]]; then
sox -S "${lst_audio_wav_decoded[i]}" \
-C 0 -b 16 \
"${cache_dir}/${lst_audio_wav_decoded[i]##*/}.sox.flac"
elif [[ "$source_hz" = "44100" \
&& "$source_bit" != "16" \
&& "$source_channel" != "2" ]]; then
sox -S "${lst_audio_wav_decoded[i]}" \
-C 0 -c 2 -b 16 \
"${cache_dir}/${lst_audio_wav_decoded[i]##*/}.sox.flac"
fi
else
if [[ "$source_hz" != "44100" \
&& "$source_bit" != "16" \
&& "$source_channel" = "2" ]]; then
sox "${lst_audio_wav_decoded[i]}" \
-C 0 -b 16 "${cache_dir}/${lst_audio_wav_decoded[i]##*/}.sox.flac" \
rate -v -L -s 44100 dither &>/dev/null
elif [[ "$source_hz" != "44100" \
&& "$source_bit" != "16" \
&& "$source_channel" != "2" ]]; then
sox "${lst_audio_wav_decoded[i]}" \
-C 0 -c 2 -b 16 \
"${cache_dir}/${lst_audio_wav_decoded[i]##*/}.sox.flac" \
rate -v -L -s 44100 dither &>/dev/null
elif [[ "$source_hz" != "44100" \
&& "$source_bit" = "16" \
&& "$source_channel" = "2" ]]; then
sox "${lst_audio_wav_decoded[i]}" \
-C 0 \
"${cache_dir}/${lst_audio_wav_decoded[i]##*/}.sox.flac" \
rate -v -L -s 44100 dither &>/dev/null
elif [[ "$source_hz" != "44100" \
&& "$source_bit" = "16" \
&& "$source_channel" != "2" ]]; then
sox "${lst_audio_wav_decoded[i]}" \
-c 2 \
"${cache_dir}/${lst_audio_wav_decoded[i]##*/}.sox.flac" \
rate -v -L -s 44100 dither &>/dev/null
elif [[ "$source_hz" = "44100" \
&& "$source_bit" != "16" \
&& "$source_channel" = "2" ]]; then
sox "${lst_audio_wav_decoded[i]}" \
-C 0 -b 16 \
"${cache_dir}/${lst_audio_wav_decoded[i]##*/}.sox.flac" \
&>/dev/null
elif [[ "$source_hz" = "44100" \
&& "$source_bit" != "16" \
&& "$source_channel" != "2" ]]; then
sox "${lst_audio_wav_decoded[i]}" \
-C 0 -c 2 -b 16 \
"${cache_dir}/${lst_audio_wav_decoded[i]##*/}.sox.flac" \
&>/dev/null
fi
fi
# Test target & replace source
if flac $flac_test_arg "${cache_dir}/${lst_audio_wav_decoded[i]##*/}.sox.flac" 2>/dev/null; then
rm "${lst_audio_wav_decoded[i]}"
mv "${cache_dir}/${lst_audio_wav_decoded[i]##*/}.sox.flac" \
"${lst_audio_wav_decoded[i]}"
else
sox_counter=$((sox_counter-1))
fi
) &
if [[ $(jobs -r -p | wc -l) -ge $nproc ]]; then
wait -n
fi
# Progress
if ! [[ "$verbose" = "1" ]]; then
if [[ "${#lst_audio_wav_decoded[@]}" = "1" ]]; then
echo -ne "${sox_counter}/${#lst_audio_wav_decoded[@]} flac file is being resampled"\\r
else
echo -ne "${sox_counter}/${#lst_audio_wav_decoded[@]} flac files are being resampled"\\r
fi
fi
done
wait
# Progress end
if ! [[ "$verbose" = "1" ]]; then
tput hpa 0; tput el
if [[ "${#lst_audio_wav_decoded[@]}" = "1" ]]; then
echo "${sox_counter} flac file resampled"
else
echo "${sox_counter} flac files resampled"
fi
fi
fi
}
# Resample 48kHz
khz48_format() {
local sox_counter
local source_hz
sox_counter="0"
if [[ "$khz48_format" = "1" ]]; then
for i in "${!lst_audio_wav_decoded[@]}"; do
# Test source
source_hz=$(sox --i -r "${lst_audio_wav_decoded[i]}")
# Counter
if [[ "$source_hz" -gt "48000" ]]; then
sox_counter=$((sox_counter+1))
fi
# Resample