/
cue2tracks
executable file
·1469 lines (1323 loc) · 36 KB
/
cue2tracks
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
#!/bin/bash
PATH=${PATH}:/bin:/usr/bin:/usr/local/bin
# --== ERRORS ==--
chk_error=0
NO_ERR=0
E_UNK=1
E_TOOL_NOT_FOUND=2
E_UNK_OUT_CODEC=3
E_UNK_IN_CODEC=4
E_IN_FILE_NOT_FOUND=5
E_TOOL_CBP_NOT_FOUND=6
E_TOOL_CP_NOT_FOUND=7
E_TOOL_ST_NOT_FOUND=8
E_TOOL_F_NOT_FOUND=9
E_TOOL_IC_NOT_FOUND=10
E_TAG_APE_NOT_FOUND=11
E_TAG_FLAC_NOT_FOUND=12
E_TAG_MP3_NOT_FOUND=13
E_TAG_OGG_NOT_FOUND=14
E_CODEC_FLAC_NOT_FOUND=15
E_CODEC_FLAKE_NOT_FOUND=16
E_CODEC_APE_NOT_FOUND=17
E_CODEC_WVp_NOT_FOUND=18
E_CODEC_WVu_NOT_FOUND=19
E_CODEC_OFR_NOT_FOUND=20
E_CODEC_SHN_NOT_FOUND=21
E_CODEC_TTA_NOT_FOUND=22
E_CODEC_MP3_NOT_FOUND=23
E_CODEC_OGGd_NOT_FOUND=24
E_CODEC_OGGe_NOT_FOUND=25
E_UNK_CODEPAGE=26
E_CODEPAGE_NOT_SET=27
E_CANT_CONVERT_CUE=28
E_CANT_SPLIT=29
E_CANT_READ_CUE=30
E_CANT_TAG_FLAC=31
E_CANT_TAG_APE=32
E_CANT_TAG_MP3=33
E_CANT_TAG_OGG=34
E_CANT_MKDIR=35
E_CANT_MOVE_FILE=36
E_WRONG_NUM_TRACKS=37
E_NOT_CUE_FILE=38
E_CANT_FIX_CUE=39
E_OPTERROR=65
# --== VARIABLES ==--
NO_ARGS=0
NAME="CUE 2 Tracks"
DESC="Tool for spliting audio CD image to tracks with cue sheet info."
VER="0.2.9.4"
AUTHOR="Sergey (sergey.dryabzhinsky@gmail.com)"
# (tool|tag|codec)_* - set to <file> if its exists, "" otherwise
MIN_SHNTOOL_VERSION=300
tool_CBP=""
tool_CP=""
tool_ST=""
tool_F=""
tool_IC=""
tag_APE=""
tag_FLAC=""
tag_MP3=""
tag_OGG=""
codec_FLAC=""
codec_FLAKE=""
codec_APE=""
codec_WVp=""
codec_WVu=""
codec_OFR=""
codec_SHN=""
codec_TTA=""
codec_MP3=""
codec_OGGd=""
codec_OGGe=""
codec_IN=""
codec_OUT=""
# (tool|tag|codec)_*_needed - set to 1 if its needed, 0 otherwise
tool_CBP_needed=1
tool_CP_needed=1
tool_ST_needed=1
tool_F_needed=1
tool_IC_needed=0
tag_APE_needed=0
tag_FLAC_needed=0
tag_MP3_needed=0
tag_OGG_needed=0
codec_FLAC_needed=0
codec_FLAKE_needed=0
codec_APE_needed=0
codec_WVp_needed=0
codec_WVu_needed=0
codec_OFR_needed=0
codec_SHN_needed=0
codec_TTA_needed=0
codec_MP3_needed=0
codec_OGGd_needed=0
codec_OGGe_needed=0
cueFile=""
inFile=""
inFileDir=""
file_splitlog=""
file_splitlogwork=""
outCodec="flac"
toolCodec="flac"
outCodecLevel=0 # best
outCodecParam=""
outCodecQuality="4"
outCodecBitRate=128
outCodecMode="V"
outExt="flac"
inCodec=""
inCodecParam=""
inFile2WAV=0
fromCP=""
locCP="ASCII"
outFormatStr="%N"
outFileName=""
tempDirName="."
scriptName=`basename "$0"`
onlyTest=1
quiteMode=0
splitInTest=0
niceness=0
putTags=1
useColors=0
useXTitle=0
pre_ALBUM_DATE=""
pre_ALBUM_GENRE=""
pre_ALBUM_DISCID=""
pre_ALBUM_NUMBER=""
tags_ALBUM_TITLE=""
tags_ALBUM_PERFORMER=""
tags_ALBUM_COMPOSER=""
tags_ALBUM_GENRE=""
tags_ALBUM_DATE=""
tags_ALBUM_TRACKS=0
tags_ALBUM_DISCID=""
tags_ALBUM_NUMBER=""
tags_TRACK_TITLE=""
tags_TRACK_PERFORMER=""
tags_TRACK_COMPOSER=""
tags_TRACK_GENRE=""
tags_TRACK_NUMBER=0
tags_TRACK_ZNUMBER=0
# --== COLORS ==--
color_default='\033[00m'
color_red='\033[01;31m'
color_green='\033[01;32m'
color_yellow='\033[01;33m'
color_magenta='\033[01;35m'
color_cyan='\033[01;36m'
# --== FUNCTIONS ==--
set_xterm_title() {
[ ${useXTitle} -ne 0 ] && echo -ne "\033]0;${NAME} v${VER} -= $1 =- \007"
}
print_error() {
echo -e "${color_red}Error${color_default}: $1" >&2
}
print_message() {
if [[ "$1" == "-n" ]]
then
[ ${quiteMode} -eq 0 ] && echo -e -n "$2"
else
[ ${quiteMode} -eq 0 ] && echo -e "$1"
fi
}
# function for checking existance of tools
checktool() {
tool="$1"
pk="$2"
print_message -n "\t${color_yellow}*${color_default} Checking for '${color_cyan}${tool}${color_default}'..." >&2
ctool=`which "${tool}" 2>/dev/null`
if [ ! -n "${ctool}" ]
then
print_message "\t[${color_red}failed${color_default}]! Install '${pk}'!\n" >&2
return ${E_TOOL_NOT_FOUND}
else
print_message "\t[${color_green}ok${color_default}]" >&2
echo "${ctool}"
fi
}
# check shntool version
check_shntool_version() {
print_message -n "\t${color_yellow}*${color_default} Checking '${color_cyan}shntool${color_default}' version..."
ver=`shntool -v 2>&1 | grep shntool | awk '{print $2}'`
ver_clean=`echo ${ver} | sed -e 's:\.::g'`
if ((${ver_clean}>=${MIN_SHNTOOL_VERSION}))
then
print_message "\t[${color_green}${ver}${color_default}]"
return 0
else
print_message "\t[${color_red}${ver}${color_default}]! Install version 3.0 or higher!\n"
return 1
fi
}
# function for printing codecs
print_codecs() {
echo -e "\tCodecs may be:\n\
\t\twav : no encode, raw sound wave,\n\
\t\tflac : (default) Free Lossless Audio Codec,\n\
\t\tflake : FLAC realisation via FFmpeg (beta),\n\
\t\tape : Monkey's Audio Codec,\n\
\t\twv : WavPack,\n\
\t\tofr : OptimFrog,\n\
\t\tshn : shorten,\n\
\t\tmp3 : mpeg 1 layer 3 via lame,\n\
\t\togg : ogg vorbis\n\
" >&2
}
# function for printing levels
print_levels() {
echo -e "\tLevels may be:\n\
\t\t'best' or 0 : Best for store (default),\n\
\t\t'fast' or 1 : Fastest processing,\n\
\t\t'mid' or 2 : Fairly good for portable hardware\n\
" >&2
}
# function for printing naming scheme
print_naming_scheme() {
echo -e "\tNaming scheme is:\n\
\t\t%A : Album title\n\
\t\t%a : Album disc number\n\
\t\t%P : Album performer\n\
\t\t%D : Album date\n\
\t\t%G : Album genre\n\
\t\t%t : Track title\n\
\t\t%p : Track performer\n\
\t\t%g : Track genre\n\
\t\t%n : Track number\n\
\t\t%N : Track number with leading zero\n\
" >&2
}
# function for print some program info
print_info() {
echo -e "\n${color_yellow}${NAME}, ${DESC}${color_default}\n\tVersion: ${color_cyan}${VER}${color_default}\n\tAuthor : ${color_cyan}${AUTHOR}${color_default}\n" >&2
}
# function for printing some help info
print_help() {
print_info
echo -e "\
Usage: ${scriptName} [options] <cue file>\n\
Options:\n\
\t-R : Disable testing and doing nothing - starts Real work\n\
\t-C : Use colored messages\n\
\t-X : Set XTerm title\n\
\t-i <image file> : Set CD image file. If not set - read from cue\n\
\t-c <codec> : Set output codec" >&2
print_codecs
echo -e "\
\t-l <level> : Set level of output codec compression rate.\
" >&2
print_levels
echo -e "\
\t-f <codepage> : Convert to UTF-8 from this 'codepage'\n\
\t-d : Disable taging of output files with cue info\n\
\t-A <album> : Set album title\n\
\t-P <performer> : Set album performer\n\
\t-K <composer> : Set album composer\n\
\t-D <date> : Set album date\n\
\t-G <genre> : Set album genre\n\
\t-I <id> : Set album disk ID\n\
\t-N <number> : Set album disc number\n\
\t-o <format string> : Set naming scheme for output files\
" >&2
print_naming_scheme
echo -e "\
\t-V : Print version and exit\n\
\t-h : Print this help and exit\n\
\t-q : Quite mode - only errors to stderr\n\
\t-s : Start spliting even in testing mode (to /dev/null)\n\
\t-n <level> : Nice level (process scheduling priority):\n\
\t From high (-19) to low (19)\n\
\n\
\tOptions only for mp3, ogg:\n\
\t-Q <quality> : Set quality of codec compression (4 - default)\n\
\tQuality may be:\n\
\t\tMP3 : 0 - high, 9 - low\n\
\t\tOGG : -1 - low, 10 - high\n\
\t-B <bitrate> : Set compression bitrate in kbps (128 default)\n\
\t-M <bitrate mode> : C - Constant, V - Variable (default)\n\
\t\tNote: If choosen V - then -B specifies maximum bitrate (only mp3)\n\
\n\
To get some action run:\n\
\t${scriptName} -c flac -f cp1251 -o \"/path/to/music/%P/%D - %A/%N\" CDimage.cue\n\
" >&2
}
# function for print program version
print_version() {
echo "${VER}"
}
# function for checking output codecs
check_outCodec() {
[ ! -n "${outCodec}" ] && outCodec="flac"
outCodec=`echo ${outCodec} | tr [:upper:] [:lower:]`
case "${outCodec}" in
"flac" )
codec_FLAC_needed=1
tag_FLAC_needed=1
outExt="flac"
toolCodec="${outCodec}"
outCodecParam=" flac -8 -o %f -"
[ ${outCodecLevel} -eq 1 ] && outCodecParam=" flac -1 -o %f -"
[ ${outCodecLevel} -eq 2 ] && outCodecParam=" flac -4 -o %f -"
codec_OUT="flac"
;;
"flake" )
codec_FLAKE_needed=1
tag_FLAC_needed=1
outExt="flac"
toolCodec="${outExt}"
outCodecParam=" flake -12 - %f"
[ ${outCodecLevel} -eq 1 ] && outCodecParam=" flake -1 - %f"
[ ${outCodecLevel} -eq 2 ] && outCodecParam=" flake -4 - %f"
codec_OUT="flake"
;;
"ape" )
codec_APE_needed=1
tag_APE_needed=1
outExt="ape"
toolCodec="${outCodec}"
outCodecParam=" mac - %f -c5000"
[ ${outCodecLevel} -eq 1 ] && outCodecParam=" mac - %f -c1000"
[ ${outCodecLevel} -eq 2 ] && outCodecParam=" mac - %f -c3000"
codec_OUT="mac"
;;
"wv" )
codec_WVp_needed=1
tag_APE_needed=1
outExt="wv"
toolCodec="${outCodec}"
outCodecParam=" wavpack -hh -x6 - -o %f"
[ ${outCodecLevel} -eq 1 ] && outCodecParam=" wavpack -f - -o %f"
[ ${outCodecLevel} -eq 2 ] && outCodecParam=" wavpack -h - -o %f"
codec_OUT="wavpack"
;;
"ofr" )
codec_OFR_needed=1
toolCodec="${outCodec}"
outExt="ofr"
codec_OUT="optimfrog"
;;
"shn" )
codec_SHN_needed=1
toolCodec="${outCodec}"
outExt="shn"
codec_OUT="shorten"
;;
"mp3" )
codec_MP3_needed=1
tag_MP3_needed=1
outExt="mp3"
toolCodec="cust"
outCodecParam=" ext=mp3 lame -S -m j -q ${outCodecQuality}"
if [[ "${outCodecMode}" == "C" ]]
then
outCodecParam=" ${outCodecParam} --cbr -b ${outCodecBitRate}"
else
outCodecParam=" ${outCodecParam} -v --vbr-new -B ${outCodecBitRate} -V ${outCodecQuality}"
fi
outCodecParam=" ${outCodecParam} - %f"
codec_OUT="lame"
;;
"ogg" )
codec_OGGe_needed=1
tag_OGG_needed=1
outExt="ogg"
toolCodec="cust"
outCodecParam=" ext=ogg oggenc -Q"
if [[ "${outCodecMode}" == "C" ]]
then
outCodecParam=" ${outCodecParam} -b ${outCodecBitRate}"
else
outCodecParam=" ${outCodecParam} -q ${outCodecQuality}"
fi
outCodecParam=" ${outCodecParam} -o %f -"
codec_OUT="oggenc"
;;
"wav" )
outExt="wav"
toolCodec="wav"
putTags=0
;;
* )
print_error "Output codec '${color_cyan}${outCodec}${color_default}' not supported."
[ ${quiteMode} -eq 0 ] && print_codecs
return ${E_UNK_OUT_CODEC}
;;
esac
print_message "\tSetting output codec to '${color_cyan}${outCodec}${color_default}'"
}
check_inCodec() {
[ ! -n "${inFile}" ] && inFile=`grep -m 1 FILE "${cueFile}" | sed -r 's/(.?*)\"(.?*)\"(.?*)/\2/g'`
print_message "\tFile to split: '${color_cyan}${inFileDir}/${inFile}${color_default}'"
if [ ! -e "${inFileDir}/${inFile}" ]
then
print_error "File '${color_cyan}${inFileDir}/${inFile}${color_default}' not founded!"
[ ${onlyTest} -eq 0 ] && return ${E_IN_FILE_NOT_FOUND}
fi
inCodec="${inFile##*.}"
inCodec=`echo ${inCodec} | tr [:upper:] [:lower:]`
case "${inCodec}" in
"flac" )
codec_FLAC_needed=1
codec_IN="flac"
;;
"ape" )
codec_APE_needed=1
codec_IN="mac"
;;
"wv" )
codec_WVu_needed=1
codec_IN="wvunpack"
;;
"ofr" )
codec_OFR_needed=1
codec_IN="optimfrog"
;;
"shn" )
codec_SHN_needed=1
codec_IN="shorten"
;;
"tta" )
codec_TTA_needed=1
codec_IN="ttaenc"
;;
"mp3" )
codec_MP3_needed=1
codec_IN="wav"
inFile2WAV=1
;;
"ogg" )
codec_OGGe_needed=1
codec_IN="wav"
inFile2WAV=1
;;
"wav" )
;;
* )
print_error "Input codec '${color_cyan}${inCodec}${color_default}' not supported."
return ${E_UNK_IN_CODEC}
;;
esac
print_message "\tSetting input codec to '${color_default}${inCodec}${color_default}'"
}
# function for search tools
search_tools() {
if [ ${tool_CBP_needed} -eq 1 ]
then
tool_CBP=`checktool cuebreakpoints cuetools`
[ -n "${tool_CBP}" ] || return ${E_TOOL_CBP_NOT_FOUND}
fi
if [ ${tool_CP_needed} -eq 1 ]
then
tool_CP=`checktool cueprint cuetools`
[ -n "${tool_CP}" ] || return ${E_TOOL_CP_NOT_FOUND}
fi
if [ ${tool_ST_needed} -eq 1 ]
then
tool_ST=`checktool shntool shntool`
[ -n "${tool_ST}" ] || return ${E_TOOL_ST_NOT_FOUND}
check_shntool_version || return ${E_TOOL_ST_NOT_FOUND}
fi
if [ ${tool_F_needed} -eq 1 ]
then
tool_F=`checktool file file`
[ -n "${tool_F}" ] || return ${E_TOOL_F_NOT_FOUND}
fi
if [ ${tool_IC_needed} -eq 1 ]
then
tool_IC=`checktool iconv glibc`
[ -n "${tool_IC}" ] || return ${E_TOOL_IC_NOT_FOUND}
fi
}
# function for searching (de|en)coders
search_dencoders() {
if [ ${codec_WVp_needed} -eq 1 ]
then
codec_WVp=`checktool wavpack wavpack`
[ -n "${codec_WVp}" ] || return ${E_CODEC_WVp_NOT_FOUND}
fi
if [ ${codec_WVu_needed} -eq 1 ]
then
codec_WVu=`checktool wvunpack wavpack`
[ -n "${codec_WVu}" ] || return ${E_CODEC_WVu_NOT_FOUND}
fi
if [ ${codec_SHN_needed} -eq 1 ]
then
codec_SHN=`checktool shorten shorten`
[ -n "${codec_SHN}" ] || return ${E_CODEC_SHN_NOT_FOUND}
fi
if [ ${codec_APE_needed} -eq 1 ]
then
codec_APE=`checktool mac mac`
[ -n "${codec_APE}" ] || return ${E_CODEC_APE_NOT_FOUND}
fi
if [ ${codec_FLAC_needed} -eq 1 ]
then
codec_FLAC=`checktool flac flac`
[ -n "${codec_FLAC}" ] || return ${E_CODEC_FLAC_NOT_FOUND}
fi
if [ ${codec_FLAKE_needed} -eq 1 ]
then
codec_FLAKE=`checktool flake flake`
[ -n "${codec_FLAKE}" ] || return ${E_CODEC_FLAKE_NOT_FOUND}
fi
if [ ${codec_OFR_needed} -eq 1 ]
then
codec_OFR=`checktool ofr optimfrog`
[ -n "${codec_OFR}" ] || return ${E_CODEC_OFR_NOT_FOUND}
fi
if [ ${codec_TTA_needed} -eq 1 ]
then
codec_TTA=`checktool ttaenc ttaenc`
[ -n "${codec_TTA}" ] || return ${E_CODEC_TTA_NOT_FOUND}
fi
if [ ${codec_MP3_needed} -eq 1 ]
then
codec_MP3=`checktool lame lame`
[ -n "${codec_MP3}" ] || return ${E_CODEC_MP3_NOT_FOUND}
fi
if [ ${codec_OGGd_needed} -eq 1 ]
then
codec_OGGd=`checktool oggdec oggdec`
[ -n "${codec_OGGd}" ] || return ${E_CODEC_OGGd_NOT_FOUND}
fi
if [ ${codec_OGGe_needed} -eq 1 ]
then
codec_OGGe=`checktool oggenc oggenc`
[ -n "${codec_OGGe}" ] || return ${E_CODEC_OGGe_NOT_FOUND}
fi
}
# function for search taging tools
search_tagers() {
if [ ${tag_APE_needed} -eq 1 ]
then
tag_APE=`checktool apetag apetag`
[ -n "${tag_APE}" ] || return ${E_TAG_APE_NOT_FOUND}
fi
if [ ${tag_FLAC_needed} -eq 1 ]
then
tag_FLAC=`checktool metaflac flac`
[ -n "${tag_FLAC}" ] || return ${E_TAG_FLAC_NOT_FOUND}
fi
if [ ${tag_MP3_needed} -eq 1 ]
then
tag_MP3=`checktool id3v2 id3v2`
[ -n "${tag_MP3}" ] || return ${E_TAG_MP3_NOT_FOUND}
fi
if [ ${tag_OGG_needed} -eq 1 ]
then
tag_OGG=`checktool vorbiscomment vorbis-tools`
[ -n "${tag_OGG}" ] || return ${E_TAG_OGG_NOT_FOUND}
fi
}
#function for trying codepage
try_codepage() {
tcp=$1
if [ -n "${tcp}" ]
then
trycdp=`${tool_IC} -l | grep -i "${tcp}"`
if [ ! -n "$trycdp" ]
then
print_error "Unknown codepage: '${color_cyan}${tcp}${color_default}..."
return ${E_UNK_CODEPAGE}
fi
else
return ${E_CODEPAGE_NOT_SET}
fi
}
# function for geting ocale codepage
get_local_codepage() {
if [ -n "${LANG}" ]
then
locCP=`echo ${LANG} | sed -r 's/(.?*)\.(.?*)/\2/g'`
[ ! -n "${locCP}" ] && locCP="ASCII"
else
locCP="ASCII"
fi
}
is_file_unicode() {
# Check for BOM simbol
[ ! -f "$1" ] && return -1
bom=`head -c 3 "$1"`
[ ${bom} = $'\357\273\277' ] && return 1
return 0
}
prepare_and_fix_cue() {
is_file_unicode "${cueFile}"
# Remove BOM record?
if [ $? -eq 1 ]
then
print_message "\t${color_yellow}Fix${color_default}: Now we try to remove BOM record..."
bnc=`basename "${cueFile}" .cue`
ncn="${tempDirName}/${bnc}.nobom.cue"
symbols=`cat "${cueFile}" | wc -c`
((symbols-=3))
tail -c ${symbols} "${cueFile}" > "${ncn}"
[ $? -ne 0 ] && return ${E_CANT_FIX_CUE}
cueFile="${ncn}"
print_message "\tNew cue sheet file name: '${color_cyan}${cueFile}${color_default}'"
fi
lastline=`tail -n 1 "${cueFile}" | tr -d [:blank:] | tr -d [:cntrl:]`
if [[ "${lastline}" != "" ]]
then
print_message "\t${color_yellow}Fix${color_default}: Last empty line missing..."
bnc=`basename "${cueFile}" .cue`
ncn="${tempDirName}/${bnc}.line.cue"
cp "${cueFile}" "${ncn}"
echo -e "\n" >> "${ncn}"
[ $? -ne 0 ] && return ${E_CANT_FIX_CUE}
cueFile="${ncn}"
print_message "\tNew cue sheet file name: '${color_cyan}${cueFile}${color_default}'"
fi
}
# function for recode cue sheet file
recode_cue() {
isutf=""
if [ -n "${tool_F}" ]
then
isutf=`${tool_F} "${cueFile}" | grep "UTF-8"`
else
isutf=""
fi
isascii=""
if [ -n "${tool_F}" ]
then
isascii=`${tool_F} "${cueFile}" | grep "ASCII"`
else
isascii=""
fi
if [ -n "${isutf}" ]
then
print_message "\tSeems like cue sheet already in UTF-8 encoding. ${color_green}Good${color_default}! ;)"
else
if [ -n "${isascii}" ]
then
print_message "\tSeems like cue sheet in ASCII encoding. ${color_yellow}Not bad${color_default}... :|"
[ ! -n "${fromCP}" ] && return 0
else
print_message "\tSeems like cue sheet not in UTF-8 encoding. ${color_red}Bad${color_default}! :("
fi
if [ -n "${tool_IC}" ]
then
if [ -n "${fromCP}" ]
then
get_local_codepage
chk_error=0
try_codepage ${fromCP}
chk_error=$?
[ ${chk_error} -ne 0 ] && fromCP=${locCP}
print_message "\tNow we try to recode cue sheet to ${color_green}UTF-8${color_default} from ${color_yellow}${fromCP}${color_default}..."
bnc=`basename "${cueFile}" .cue`
cat "${cueFile}" | ${tool_IC} -f "${fromCP}" -t "UTF-8" > "${tempDirName}/${bnc}.utf8.cue"
[ $? -ne 0 ] && return ${E_CANT_CONVERT_CUE}
cueFile="${tempDirName}/${bnc}.utf8.cue"
print_message "\tNew cue sheet file name: '${color_cyan}${cueFile}${color_default}'"
fi
fi
fi
}
recode_to_wav() {
inFileWAV="${inFile}.wav"
[ ${onlyTest} -ne 0 ] && return
errors=0
FullPathIN="${inFileDir}/${inFile}"
FullPathOUT="${tempDirName}/${inFileWAV}"
cmd=""
case "${inCodec}" in
"flac" )
flac -d -f -o "${FullPathOUT}" "${FullPathIN}"
errors=$?
;;
"ape" )
mac "${FullPathIN}" "${FullPathOUT}" -d
errors=$?
;;
"wv" )
wvunpack -y -o "${FullPathOUT}" "${FullPathIN}"
errors=$?
;;
"ofr" )
;;
"shn" )
shorten -x "${FullPathIN}" "${FullPathOUT}"
errors=$?
;;
"tta" )
ttaenc -d -o "${FullPathOUT}" "${FullPathIN}"
errors=$?
;;
"mp3" )
lame -S --decode "${FullPathIN}" "${FullPathOUT}"
errors=$?
;;
"ogg" )
oggdec -Q --decode -o "${FullPathOUT}" "${FullPathIN}"
errors=$?
;;
"wav" )
;;
* )
print_error "Decode '${color_cyan}${inCodec}${color_default}' to WAV not supported."
return ${E_UNK_IN_CODEC}
;;
esac
[ ${errors} -ne 0 ] && return -1
inFile="${inFileWAV}"
inFileDir="${tempDirName}"
inCodec="wav"
codec_IN="wav"
print_message "\tSetting new input file to '${color_default}${inFile}${color_default}'"
}
# function for split condition
split_cond() {
tlc=$1
ttl="$2"
(( tlc < tags_ALBUM_TRACKS )) && return 0
[ ! -n "${ttl}" ] && return 0
return 1
}
# function for image splitting
split_image() {
runopt=""
[ ${quiteMode} -ne 0 ] && runopt="-q"
file_splitlog="${tempDirName}/split.log"
file_splitlogwork="${file_splitlog}.work"
err=0
_outCodecOpts=""
if [ ${onlyTest} -eq 0 ]
then
_outCodecOpts="${toolCodec}${outCodecParam}"
else
[ ${splitInTest} -eq 0 ] && return 0
_outCodecOpts="null"
fi
# ${tool_CBP} "${cueFile}" | ${tool_ST} split ${runopt} -d "${tempDirName}" -O always -t %n ${inCodecParam} -o "${_outCodecOpts}" "${inFileDir}/${inFile}" &> "${file_splitlog}" &
cd "${inFileDir}"
${tool_CBP} "${cueFile}" | ${tool_ST} split ${runopt} -d "${tempDirName}" -O always -t %n ${inCodecParam} -o "${_outCodecOpts}" "./${inFile}" &> "${file_splitlog}" &
disown %1
cd "${OLDPWD}"
# nifd=`echo ${inFileDir} | sed -r -e 's/([\\\ \[\(\.\:\%\&\$\#\?\!])/\\\\\1/g' -e 's/\]/\\\]/g' -e 's/\)/\\\)/g'`
lc=0
tl=""
cl=""
llc=${lc}
lw=0
split_cond ${lc} "${tl}"
sc=$?
while [ ${sc} -eq 0 ]
do
if [ -e "${file_splitlog}" ]
then
cp "${file_splitlog}" "${file_splitlogwork}"
cl=`tail -n 1 "${file_splitlogwork}" | awk 'BEGIN {FS="\b\b\b\b\b"} { if ( NF > 1 ) print $1, $NF; else print $1}' | sed -e 's/\ \ 0%\ \ //g' -e 's/Splitting\ //g'`
if [ -n "${cl}" ]
then
llc=${lc}
lc=`cat "${file_splitlogwork}" | wc -l`
((lc++))
# spl=`echo ${cl} | tr -d [:cntrl:] | awk 'BEGIN {FS=" : "} {print $1}' | sed -e "s:${tempDirName}/::g" -e "s:${nifd}/::g"`
spl=`echo ${cl} | tr -d [:cntrl:] | awk 'BEGIN {FS=" : "} {print $1}' | sed -e "s:${tempDirName}/::g" -e "s:\./::g"`
prc=`echo ${cl} | awk 'BEGIN {FS=" : "} {print $2}'`
if (( lc <= tags_ALBUM_TRACKS ))
then
bs="\b"
for ((i=1;i<lw;i++))
do
bs="${bs}\b"
done
if (( llc != lc ))
then
if (( lc > 1 ))
then
print_message "${bs}${color_green}100%${color_default}"
else
print_message ""
fi
print_message -n "Track ${color_green}${lc}${color_default}: ${spl} : ${color_yellow}${prc}${color_default}"
lw=${#prc}
else
print_message -n "${bs}${color_yellow}${prc}${color_default}"
lw=${#prc}
fi
set_xterm_title "Splitting Track ${lc} of ${tags_ALBUM_TRACKS}, Progress: ${prc}"
else
print_message "${bs}${color_green}100%${color_default}"
fi
fi
tl=`echo "${cl}" | grep "100% OK"`
split_cond ${lc} "${tl}"
sc=$?
fi
str=`ps ax | grep shntool | grep -v grep`
if [[ "${str}" == "" ]]
then
if [ ${sc} -eq 0 ]
then
# err=1
sc=1
fi
fi
sleep 1s
done
if [ ${err} -ne 0 ]
then
print_error "Some error occured while spliting file '${color_cyan}${inFileDir}/${inFile}${color_default}'!"
return ${E_CANT_SPLIT}
fi
}
# function for reading album info
read_album_info() {
test=`${tool_CP} -d %N "${cueFile}" 2>&1 | grep error`
if [ -n "${test}" ]
then
print_error "Some error occured while reading CUE sheet!"
return ${E_CANT_READ_CUE}
fi
if [ ! -n "${pre_ALBUM_PERFORMER}" ]
then
tags_ALBUM_PERFORMER=`${tool_CP} -d %P "${cueFile}"`
else
tags_ALBUM_PERFORMER=${pre_ALBUM_PERFORMER}
fi
print_message "\tAlbum Performer = ${color_cyan}${tags_ALBUM_PERFORMER}${color_default}"
if [ ! -n "${pre_ALBUM_COMPOSER}" ]
then
tags_ALBUM_COMPOSER=`${tool_CP} -d %C "${cueFile}"`
else
tags_ALBUM_COMPOSER=${pre_ALBUM_COMPOSER}
fi
[ -n "${tags_ALBUM_COMPOSER}" ] && print_message "\tAlbum Performer = ${color_cyan}${tags_ALBUM_PERFORMER}${color_default}"
if [ ! -n "${pre_ALBUM_TITLE}" ]
then
tags_ALBUM_TITLE=`${tool_CP} -d %T "${cueFile}"`
else
tags_ALBUM_TITLE=${pre_ALBUM_TITLE}
fi
print_message "\tAlbum Title = ${color_cyan}${tags_ALBUM_TITLE}${color_default}"
if [ ! -n "${pre_ALBUM_GENRE}" ]
then
tags_ALBUM_GENRE=`${tool_CP} -d %G "${cueFile}"`
else
tags_ALBUM_GENRE=${pre_ALBUM_GENRE}
fi
[ -n "${tags_ALBUM_GENRE}" ] || tags_ALBUM_GENRE=`grep -m 1 GENRE "${cueFile}" | sed -r -e 's/.?*REM\ GENRE\ (.?*)/\1/g' -e 's:"::g' | tr -d [:cntrl:]`
[ -n "${tags_ALBUM_GENRE}" ] || tags_ALBUM_GENRE="Other"
print_message "\tAlbum Genre = ${color_cyan}${tags_ALBUM_GENRE}${color_default}"
[ -n "${pre_ALBUM_NUMBER}" ] && tags_ALBUM_NUMBER=${pre_ALBUM_NUMBER}
[ -n "${tags_ALBUM_NUMBER}" ] && print_message "\tAlbum Disc Number = ${color_cyan}${tags_ALBUM_NUMBER}${color_default}"
[ -n "${pre_ALBUM_DISCID}" ] && tags_ALBUM_DISCID=${pre_ALBUM_DISCID}
[ -n "${tags_ALBUM_DISCID}" ] || tags_ALBUM_DISCID=`grep -m 1 DISCID "${cueFile}" | sed -r -e 's/.?*REM\ DISCID\ (.?*)/\1/g' -e 's:"::g' | tr -d [:cntrl:]`
[ -n "${tags_ALBUM_DISCID}" ] && print_message "\tAlbum Disc ID = ${color_cyan}${tags_ALBUM_DISCID}${color_default}"
if [ ! -n "${pre_ALBUM_DATE}" ]
then
tags_ALBUM_DATE=`grep -m 1 DATE "${cueFile}" | sed -r -e 's/.?*REM\ DATE\ (\d)/\1/g' -e 's:"::g' | tr -d [:cntrl:] | tr -d " " | tr -d [:alpha:]`
else
tags_ALBUM_DATE=${pre_ALBUM_DATE}
fi
[ -n "${tags_ALBUM_DATE}" ] || tags_ALBUM_DATE=0000
print_message "\tAlbum Date = ${color_cyan}${tags_ALBUM_DATE}${color_default}"
tags_ALBUM_TRACKS=`${tool_CP} -d %N "${cueFile}"`
print_message "\tNumber of Tracks = ${color_cyan}${tags_ALBUM_TRACKS}${color_default}"
}
# function for track taging
tag_track() {
case "${outCodec}" in
"flac" | "flake" )
if [ ${onlyTest} -eq 0 ]
then
optional=""
[ -n "${tags_ALBUM_DISCID}" ] && optional="${optional} --set-tag=DISCID=${tags_ALBUM_DISCID}"
[ -n "${tags_ALBUM_NUMBER}" ] && optional="${optional} --set-tag=DISCNUMBER=${tags_ALBUM_NUMBER}"
[ -n "${tags_TRACK_COMPOSER}" ] && optional="${optional} --set-tag=COMPOSER=${tags_TRACK_COMPOSER}"
${tag_FLAC} \
--set-tag="TITLE=${tags_TRACK_TITLE}" \
--set-tag="ALBUM=${tags_ALBUM_TITLE}" \
--set-tag="ARTIST=${tags_TRACK_PERFORMER}" \
--set-tag="DATE=${tags_ALBUM_DATE}" \
--set-tag="TRACKNUMBER=${tags_TRACK_ZNUMBER}" \
--set-tag="GENRE=${tags_TRACK_GENRE}" \
--set-tag="COMMENT=${NAME} v${VER}" \
${optional} \
"${tempDirName}/${tags_TRACK_ZNUMBER}.${outExt}" &>/dev/null
if [ $? -ne 0 ]
then
print_error "Cant tag '${color_cyan}${tempDirName}/${tags_TRACK_ZNUMBER}.${outExt}${color_default}'"
return ${E_CANT_TAG_FLAC}
fi
fi
;;
"ape" | "wv" )
if [ ${onlyTest} -eq 0 ]
then
[ -n "${tags_ALBUM_DISCID}" ] && optional="${optional} -p DISCID=${tags_ALBUM_DISCID}"