-
Notifications
You must be signed in to change notification settings - Fork 0
/
2opus.sh
executable file
·1284 lines (1184 loc) · 37.9 KB
/
2opus.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
# 2opus
# Various lossless to OPUS while keeping the tags.
# \(^o^)/
#
# Author : Romain Barbarot
# https://github.com/Jocker666z/2opus/
# Licence : unlicense
# Search & populate array with source files
search_source_files() {
local codec_test
if [[ "${re_opus}" = "1" ]]; then
mapfile -t lst_audio_src < <(find "$PWD" -maxdepth 3 -type f -iname '*.opus' 2>/dev/null \
| sort)
else
mapfile -t lst_audio_src < <(find "$PWD" -maxdepth 3 -type f -regextype posix-egrep \
-iregex '.*\.('$input_ext')$' 2>/dev/null | sort)
fi
# Only clean
for i in "${!lst_audio_src[@]}"; do
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]%.*}.ogg" )
if [[ "$codec_test" != "flac" ]]; then
unset "lst_audio_src[i]"
fi
fi
if [[ "${M4A_only}" = "1" ]] \
&& [[ "${lst_audio_src[i]##*.}" != "m4a" ]]; then
unset "lst_audio_src[i]"
fi
if [[ "${MP3_only}" = "1" ]] \
&& [[ "${lst_audio_src[i]##*.}" != "mp3" ]]; 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
if [[ -z "$no_test_source" ]]; then
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
(
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
) &
if [[ $(jobs -r -p | wc -l) -ge $nproc ]]; then
wait -n
fi
done
wait
fi
# Test if error generated
for file in "${lst_audio_src[@]}"; do
if [[ -z "$no_test_source" ]]; then
# 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" )
if [[ "${re_opus}" = "1" ]]; then
lst_audio_reopus_src_pass+=( "${file}.old" )
fi
fi
else
lst_audio_src_pass+=( "$file" )
if [[ "${re_opus}" = "1" ]]; then
lst_audio_reopus_src_pass+=( "${file}.old" )
fi
fi
done
# Progress end
if [[ "$verbose" != "1" ]] \
&& [[ -z "$no_test_source" ]]; 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
}
# Decode source
decode_source() {
local decode_counter
decode_counter="0"
for file in "${lst_audio_src_pass[@]}"; do
(
if [[ "${file##*.}" != "flac" ]] \
&& [[ "${file##*.}" != "ogg" ]] \
&& [[ "${file##*.}" != "wav" ]] \
&& [[ "$verbose" = "1" ]]; then
if [[ "${file##*.}" = "dsf" ]]; then
ffmpeg -y -i "$file" \
-c:a pcm_s24le -ar 384000 "${cache_dir}/${file##*/}.wav"
else
ffmpeg -y -i "$file" "${cache_dir}/${file##*/}.wav"
fi
elif [[ "${file##*.}" != "flac" ]] \
&& [[ "${file##*.}" != "ogg" ]] \
&& [[ "${file##*.}" != "wav" ]]; then
if [[ "${file##*.}" = "dsf" ]]; then
ffmpeg $ffmpeg_log_lvl -y -i "$file" \
-c:a pcm_s24le -ar 384000 "${cache_dir}/${file##*/}.wav"
else
ffmpeg $ffmpeg_log_lvl -y -i "$file" "${cache_dir}/${file##*/}.wav"
fi
fi
) &
if [[ $(jobs -r -p | wc -l) -ge $nproc ]]; then
wait -n
fi
# OPUS 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
# 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
# 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}")" )
if [[ "${re_opus}" = "1" ]]; then
mv "${file}" "${file}.old"
fi
done
}
# 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_opus() {
local cover_test
local cover_ext
local tag_label
local grab_tag_counter
local tag_trick
local tag_trick_str
grab_tag_counter="0"
for file in "${lst_audio_opus_encoded[@]}"; do
# Reset
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%.*}.flac" ]]; then
file="${file%.*}.flac"
elif [[ -s "${file%.*}.m4a" ]]; then
file="${file%.*}.m4a"
elif [[ -s "${file%.*}.mp3" ]]; then
file="${file%.*}.mp3"
elif [[ -s "${file%.*}.ogg" ]]; then
file="${file%.*}.ogg"
elif [[ -s "${file%.*}.wav" ]]; then
file="${file%.*}.wav"
elif [[ -s "${file%.*}.wv" ]]; then
file="${file%.*}.wv"
elif [[ "${re_opus}" = "1" ]] \
&& [[ -s "${file}.old" ]]; then
file="${file}.old"
fi
# 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
# Try to extract cover, if no cover in directory
if [[ ! -e "${file%/*}"/cover.jpg ]] \
&& [[ ! -e "${file%/*}"/cover.png ]]; 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."$cover_ext" 2>/dev/null
fi
fi
# Remove empty tag label= & sort
mapfile -t source_tag < <( printf '%s\n' "${source_tag[@]}" \
| grep "=" )
# 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
# Prevent double quote error
tag_label[i]="${tag_label[i]//\"/\\\"}"
# 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 argument
target_tags_construct=$(printf '%s\n' "${source_tag[@]}" \
| awk 1 ORS=' -s ')
target_tags_construct="-s ${target_tags_construct% -s }"
lst_audio_opus_target_tags+=( "$target_tags_construct" )
# Progress
if ! [[ "$verbose" = "1" ]]; then
grab_tag_counter=$((grab_tag_counter+1))
if [[ "${#lst_audio_opus_encoded[@]}" = "1" ]]; then
echo -ne "${grab_tag_counter}/${#lst_audio_opus_encoded[@]} tag is being converted to vorbis comment"\\r
else
echo -ne "${grab_tag_counter}/${#lst_audio_opus_encoded[@]} tags is being converted to vorbis comment"\\r
fi
fi
done
# Progress end
if ! [[ "$verbose" = "1" ]]; then
tput hpa 0; tput el
if [[ "${#lst_audio_opus_encoded[@]}" = "1" ]]; then
echo "${grab_tag_counter} tag is being converted to vorbis comment"
else
echo "${grab_tag_counter} tags is being converted to vorbis comment"
fi
fi
}
# OPUS - Encode
encode_opus() {
local compress_counter
compress_counter="0"
# Encode OPUS
for i in "${!lst_audio_src_pass[@]}"; do
(
if [[ "$verbose" = "1" ]]; then
opusenc \
--bitrate "$opus_bitrate" --vbr \
"${lst_audio_wav_decoded[i]}" \
"${lst_audio_src_pass[i]%.*}.opus"
else
opusenc \
--bitrate "$opus_bitrate" --vbr \
"${lst_audio_wav_decoded[i]}" \
"${lst_audio_src_pass[i]%.*}.opus" &>/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[@]} opus file is being encoded"\\r
else
echo -ne "${compress_counter}/${#lst_audio_wav_decoded[@]} opus files are being encoded"\\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} opus file encoded"
else
echo "${compress_counter} opus files encoded"
fi
fi
# Clean + target array
for i in "${!lst_audio_src_pass[@]}"; do
# Array of OPUS target
lst_audio_opus_encoded+=( "${lst_audio_src_pass[i]%.*}.opus" )
# 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
}
# OPUS - Tag
tag_opus() {
local tag_counter
tag_counter="0"
for i in "${!lst_audio_opus_encoded[@]}"; do
(
if [[ "$verbose" = "1" ]]; then
eval opustags -D \
-i "\"${lst_audio_opus_encoded[i]%.*}.opus\"" \
"${lst_audio_opus_target_tags[i]}"
else
eval opustags -D \
-i "\"${lst_audio_opus_encoded[i]%.*}.opus\"" \
"${lst_audio_opus_target_tags[i]}" &>/dev/null
fi
) &
if [[ $(jobs -r -p | wc -l) -ge $nproc ]]; then
wait -n
fi
# Progress
if ! [[ "$verbose" = "1" ]]; then
tag_counter=$((tag_counter+1))
if [[ "${#lst_audio_wav_decoded[@]}" = "1" ]]; then
echo -ne "${tag_counter}/${#lst_audio_opus_encoded[@]} opus file is being tagged"\\r
else
echo -ne "${tag_counter}/${#lst_audio_opus_encoded[@]} opus files are being tagged"\\r
fi
fi
done
wait
# Progress end
if ! [[ "$verbose" = "1" ]]; then
tput hpa 0; tput el
if [[ "${#lst_audio_wav_decoded[@]}" = "1" ]]; then
echo "${tag_counter} opus file tagged"
else
echo "${tag_counter} opus files tagged"
fi
fi
}
# Replay gain
replay_gain() {
local rsgain_counter
rsgain_counter="0"
if [[ "$replay_gain" = "1" ]]; then
for file in "${lst_audio_opus_encoded[@]}"; do
(
rsgain custom -q -c a -s i "$file"
) &
if [[ $(jobs -r -p | wc -l) -ge $nproc ]]; then
wait -n
fi
# Progress
rsgain_counter=$((rsgain_counter+1))
if ! [[ "$verbose" = "1" ]]; then
echo -ne "${rsgain_counter}/${#lst_audio_opus_encoded[@]} replay gain applied"\\r
fi
done
wait
# Progress end
if ! [[ "$verbose" = "1" ]]; then
tput hpa 0; tput el
echo "${rsgain_counter} replay gain applied"
fi
fi
}
# Total size calculation in MB - Input must be in bytes
calc_files_size() {
local files
local size
local size_in_mb
files=("$@")
if (( "${#files[@]}" )); then
# Get size in bytes
if ! [[ "${files[-1]}" =~ ^[0-9]+$ ]]; then
size=$(wc -c "${files[@]}" | tail -1 | awk '{print $1;}')
else
size="${files[-1]}"
fi
# Mb convert
size_in_mb=$(bc <<< "scale=1; $size / 1024 / 1024" | sed 's!\.0*$!!')
else
size_in_mb="0"
fi
# If string start by "." add lead 0
if [[ "${size_in_mb:0:1}" == "." ]]; then
size_in_mb="0$size_in_mb"
fi
# If GB not display float
size_in_mb_integer="${size_in_mb%%.*}"
if [[ "${#size_in_mb_integer}" -ge "4" ]]; then
size_in_mb="$size_in_mb_integer"
fi
echo "$size_in_mb"
}
# Get file size in bytes
get_files_size_bytes() {
local files
local size
files=("$@")
if (( "${#files[@]}" )); then
# Get size in bytes
size=$(wc -c "${files[@]}" | tail -1 | awk '{print $1;}')
fi
echo "$size"
}
# Percentage calculation
calc_percent() {
local total
local value
local perc
value="$1"
total="$2"
if [[ "$value" = "$total" ]]; then
echo "00.00"
else
# Percentage calculation
perc=$(bc <<< "scale=4; ($total - $value)/$value * 100")
# If string start by "." or "-." add lead 0
if [[ "${perc:0:1}" == "." ]] || [[ "${perc:0:2}" == "-." ]]; then
if [[ "${perc:0:2}" == "-." ]]; then
perc="${perc/-./-0.}"
else
perc="${perc/./+0.}"
fi
fi
# If string start by integer add lead +
if [[ "${perc:0:1}" =~ ^[0-9]+$ ]]; then
perc="+${perc}"
fi
# Keep only 5 first digit
perc="${perc:0:5}"
echo "$perc"
fi
}
# Display trick - print term tuncate
display_list_truncate() {
local list
local term_widh_truncate
list=("$@")
term_widh_truncate=$(stty size | awk '{print $2}' | awk '{ print $1 - 8 }')
for line in "${list[@]}"; do
if [[ "${#line}" -gt "$term_widh_truncate" ]]; then
echo -e " $line" \
| cut -c 1-"$term_widh_truncate" \
| awk '{print $0"..."}'
else
echo -e " $line"
fi
done
}
# Summary of processing
summary_of_processing() {
local time_formated
local file_target_files_size
local file_diff_percentage
local file_path_truncate
local total_target_files_size
local total_diff_size
local total_diff_percentage
if (( "${#lst_audio_src[@]}" )); then
time_formated="$((SECONDS/3600))h$((SECONDS%3600/60))m$((SECONDS%60))s"
# All files pass size stats & label
if (( "${#lst_audio_src_pass[@]}" )); then
for i in "${!lst_audio_src_pass[@]}"; do
# Make statistics of indidual processed files
file_target_files_size=$(get_files_size_bytes "${lst_audio_opus_encoded[i]}")
file_diff_percentage=$(calc_percent "${file_source_files_size[i]}" "$file_target_files_size")
filesPassSizeReduction+=( "$file_diff_percentage" )
file_path_truncate=$(echo "${lst_audio_opus_encoded[i]}" | rev | cut -d'/' -f-2 | rev)
if [[ "$replay_gain" = "1" ]]; then
file_replaygain=$(mutagen-inspect "${lst_audio_opus_encoded[i]}" \
| grep "REPLAYGAIN_TRACK_GAIN")
file_replaygain="${file_replaygain//REPLAYGAIN_TRACK_GAIN=/}"
file_replaygain="${file_replaygain//[[:blank:]]/} ~ "
if [[ "${file_replaygain:0:1}" =~ ^[0-9]+$ ]]; then
file_replaygain="+${file_replaygain}"
fi
fi
filesPassLabel+=( "${filesPassSizeReduction[i]}% ~ ${file_replaygain}.${file_path_truncate}" )
done
fi
# All files rejected size label
if (( "${#lst_audio_src_rejected[@]}" )); then
for i in "${!lst_audio_src_rejected[@]}"; do
file_path_truncate=$(echo "${lst_audio_src_rejected[i]}" | rev | cut -d'/' -f-2 | rev)
filesRejectedLabel+=( ".${file_path_truncate}" )
done
fi
# Total files size stats
total_target_files_size=$(calc_files_size "${lst_audio_opus_encoded[@]}")
total_diff_size=$(bc <<< "scale=0; ($total_target_files_size - $total_source_files_size)" \
| sed -r 's/^(-?)\./\10./')
total_diff_percentage=$(calc_percent "$total_source_files_size" "$total_target_files_size")
# Print list of files stats
if (( "${#lst_audio_src_pass[@]}" )); then
echo
echo "File(s) created:"
display_list_truncate "${filesPassLabel[@]}"
fi
# Print list of files reject
if (( "${#lst_audio_src_rejected[@]}" )); then
echo
echo "File(s) in error:"
display_list_truncate "${filesRejectedLabel[@]}"
fi
# Print all files stats
echo
echo "${#lst_audio_opus_encoded[@]}/${#lst_audio_src[@]} file(s) encoded to OPUS for a total of ${total_target_files_size}MB."
echo "${total_diff_percentage}% difference with the source files, ${total_diff_size}MB on ${total_source_files_size}MB."
echo "Processing end: $(date +%D\ at\ %Hh%Mm) - Duration: ${time_formated}."
echo
fi
}
# Remove source files
remove_source_files() {
if [ "${#lst_audio_opus_encoded[@]}" -gt 0 ] ; then
read -r -p "Remove source files? [y/N]:" qarm
case $qarm in
"Y"|"y")
# Remove source files
if [[ "${re_opus}" = "1" ]]; then
for file in "${lst_audio_reopus_src_pass[@]}"; do
rm -f "$file" 2>/dev/null
done
else
for file in "${lst_audio_src_pass[@]}"; do
rm -f "$file" 2>/dev/null
done
fi
;;
*)
source_not_removed="1"
;;
esac
fi
}
# Remove target files
remove_target_files() {
if [ "$source_not_removed" = "1" ] ; then
read -r -p "Remove target files? [y/N]:" qarm
case $qarm in
"Y"|"y")
# Remove source files
for file in "${lst_audio_opus_encoded[@]}"; do
rm -f "$file" 2>/dev/null
if [[ "${re_opus}" = "1" ]]; then
mv "${file}.old" "$file"
fi
done
;;
esac