-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge.sh
1173 lines (1022 loc) · 29.6 KB
/
merge.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
#!/bin/bash
printarr() { declare -n __p="$1"; for k in "${!__p[@]}"; do printf "%s=%s\n" "$k" "${__p[$k]}" ; done ; }
# Create the array
declare -A jsons
declare -a folders
declare -A files
declare -a templates
declare -A fileTemplateMap
oldIFS="$IFS"
# Color definition
COLOR_CURRENT_LINE='\e[47;30m'
COLOR_RESET='\e[0m'
COLOR_WARNING='\e[38;5;202m'
COLOR_ERROR='\e[41;37;1m\a'
COLOR_DISABLED_NON_CURRENT='\e[90m'
COLOR_DISABLED_CURRENT='\e[100;37m'
COLOR_HINT='\e[38;5;99m'
# Column sizes used for the display
# Because we print each line one by one, for now I will use fixed lengths. Maybe one day I'll implement dynamic sizing
SIZE_ID=4
SIZE_TYPE=3
SIZE_DEFAULT=2
SIZE_LANG=3
SIZE_CODEC=25
SIZE_NAME=35
SIZE_SHIFT=6
# Separator used for displaying columns
COLUMN_SEPARATOR="|"
HEADER_SEPARATOR="-"
COLUMN_HEADER_INTERSECTION="+"
# Newline constant used to add '\n' to strings
# See https://stackoverflow.com/a/64938613
nl="$(printf '\nq')"
nl=${nl%q}
# Parse a template line ($1) into separate variables using read
# In the past, I used an array like array=($line) with the IFS set at ';' however, that messed up with certain characters like '*'
function parseLine() {
IFS=";"
read currentID currentType currentDefault currentLang currentCodec currentName currentShift currentDisabled<<<$1
}
# If a "multi value" is used (different value for different files), the variable $1 is in the form of "*( [file]=value ...)"
# Here, if $1 begins with '*', we want to extract the right value from the file $2
function parseMVal() {
eval "mValLine=\$$1"
# Checks if $1 begins with '*'
[[ "$(cut -c -1 <<<$mValLine)" != "*" ]] && return
declare -A mValArray
IFS=" "
eval "mValArray=$(cut -c 2- <<<$mValLine)"
mValLine="${mValArray[$2]}"
eval "$1=\$mValLine"
}
# Loads the configuration from settings.sh if it exists
if [[ -f "./settings.sh" ]]
then
source ./settings.sh
else
echo "WARNING: Config file not found. Using default values"
DEFAULT_FILE_REGEX=('([sS][0-9][0-9][eE])([0-9][0-9])' '([0-9][0-9])')
DEFAULT_REGEX_MATCH_NB=(2 1)
DEFAULT_OUTPUT_NAME='${regex_match}'
MKVFONTMAN_LIB_FILE="mkvfontman/lib.sh"
fi
# Loads the MKVFontMan lib if existing
if [[ -f "$MKVFONTMAN_LIB_FILE" ]]
then
source "$MKVFONTMAN_LIB_FILE"
MKVFONTMAN_LOADED=true
else
MKVFONTMAN_LOADED=false
fi
# printf the $1 exactly as the size $2. Used to display the columns
function printfAsSize() {
cutText=$(cut -c -$2 <<<$1)
# Unfortunately printf count unicode characters such as é as 2 characters which breaks the padding
# This can be fixed by counting the difference between the displayed characters and the true characters count
# https://unix.stackexchange.com/a/609135
bytes=$(printf '%s' "$1" | wc -c)
chars=$(printf '%s' "$1" | wc -m)
n=$(($2+bytes-chars))
printf "%-${n}s" "$cutText"
}
# calls pintfAsSize but checks ifor the sepcial case of multiple values (begins with '*') and don't display the technical data
# Takes the same parameters as printfAsSize
function checkAndPrintfAsSize() {
cutText=$(cut -c -1 <<<$1)
if [[ "$cutText" = "*" ]]
then
printfAsSize "*" "$2"
else
printfAsSize "$1" "$2"
fi
}
# printf the same pattern ($1) N ($2) times. Used to display the line below the header
function nprintf() {
for n in $(seq $2)
do
printf "$1"
done
}
# Prints a line ($1) of the screen. $2 sets if the line is selected or not
function printLine() {
# We parse the current line with the IFS as ; for printing
parseLine "${currentTemplate[$1]}"
if [[ "$2" = true ]]
then
if [[ "$currentDisabled" = "D" ]]
then
printf "$COLOR_DISABLED_CURRENT"
else
printf "$COLOR_CURRENT_LINE"
fi
else
if [[ "$currentDisabled" = "D" ]]
then
printf "$COLOR_DISABLED_NON_CURRENT"
fi
fi
printfAsSize "$currentID" "$SIZE_ID"
printf "$COLUMN_SEPARATOR"
printfAsSize "$currentType" "$SIZE_TYPE"
printf "$COLUMN_SEPARATOR"
printfAsSize "$currentDefault" "$SIZE_DEFAULT"
printf "$COLUMN_SEPARATOR"
checkAndPrintfAsSize "$currentLang" "$SIZE_LANG"
printf "$COLUMN_SEPARATOR"
printfAsSize "$currentCodec" "$SIZE_CODEC"
printf "$COLUMN_SEPARATOR"
checkAndPrintfAsSize "$currentName" "$SIZE_NAME"
printf "$COLUMN_SEPARATOR"
checkAndPrintfAsSize "$currentShift" "$SIZE_SHIFT"
printf "$COLOR_RESET\n"
}
function printHeader() {
# Prints the header
printfAsSize "ID" "$SIZE_ID"
printf "$COLUMN_SEPARATOR"
printfAsSize "T" "$SIZE_TYPE"
printf "$COLUMN_SEPARATOR"
printfAsSize "D" "$SIZE_DEFAULT"
printf "$COLUMN_SEPARATOR"
printfAsSize "Lan" "$SIZE_LANG"
printf "$COLUMN_SEPARATOR"
printfAsSize "Codec" "$SIZE_CODEC"
printf "$COLUMN_SEPARATOR"
printfAsSize "Track name" "$SIZE_NAME"
printf "$COLUMN_SEPARATOR"
printfAsSize "Shift" "$SIZE_SHIFT"
printf "\n"
# Prints the line
nprintf "$HEADER_SEPARATOR" "$SIZE_ID"
printf "$COLUMN_HEADER_INTERSECTION"
nprintf "$HEADER_SEPARATOR" "$SIZE_TYPE"
printf "$COLUMN_HEADER_INTERSECTION"
nprintf "$HEADER_SEPARATOR" "$SIZE_DEFAULT"
printf "$COLUMN_HEADER_INTERSECTION"
nprintf "$HEADER_SEPARATOR" "$SIZE_LANG"
printf "$COLUMN_HEADER_INTERSECTION"
nprintf "$HEADER_SEPARATOR" "$SIZE_CODEC"
printf "$COLUMN_HEADER_INTERSECTION"
nprintf "$HEADER_SEPARATOR" "$SIZE_NAME"
printf "$COLUMN_HEADER_INTERSECTION"
nprintf "$HEADER_SEPARATOR" "$SIZE_SHIFT"
printf "\n"
}
function printFooter() {
# Prints the keyboard shortcuts
printf " \n" # The space is needed because the array don't work with empty lines
echo "UP/DOWN: Select track"
echo "-/PG UP: shift track upwards"
echo "+/PG DOWN: shift track downards"
echo "D: Set/unset as default track"
echo "F: Set/unset as forced track (for subtitles)"
echo "L: Set track language"
echo "N: Set track name"
echo "S: Set track shift (in ms)"
echo "SPACE: Enable/disable track (disabled track won't be copied)"
echo "RETURN: Apply the template and set the next one"
}
function printAllLines() {
for i in "${!currentTemplate[@]}"
do
printLine $i $1
done
}
# Regenerate the whole screen in memory
function redraw() {
printHeader
for i in "${!currentTemplate[@]}"
do
printLine $i false
done
printFooter
}
# Prints the $file array in a pretty format
function printFiles() {
# First cell
printf "File\\Folder\t"
# Print folders
for i in "${folders[@]}"
do
printf "$i\t"
done
printf "Regex\tTemplate\n"
IFS=$nl
# For each file
for i in $(seq 0 $(($fileNb - 1)))
do
printf "$i\t"
# For each folder
for j in "${folders[@]}"
do
if [[ -v files["$j $i"] ]]
then # File exists in array
printf "${files[$j $i]}\t"
else # File don't exist in array
printf "[none]\t"
fi
done
# Prints the end of the line
printf "${files[regex $i]}\t${fileTemplateMap[$i]}\n"
done
}
# Generate the json for mkvmerge. The file number $1 must be specified
# The $trackOrder must also be set
# WARNING: This will permanently edit the template, by sorting it and removing disabled tracks
function generateJson() {
printf "[\n"
# Output file
printf '\t"-o",\n'
# To set the output file name, we set ${regex_match} and ${file_id}
regex_match="${files[regex $file]}"
file_id="$file"
printf '\t"out/'$(eval "echo $DEFAULT_OUTPUT_NAME")'.mkv",\n'
# Remove disabled tracks
for i in "${!currentTemplate[@]}"
do
parseLine "${currentTemplate[$i]}"
[[ "$currentDisabled" = "D" ]] && unset currentTemplate[$i]
done
# Sort the template so each file is in order
IFS=$nl
sorted=($(sort <<<"${currentTemplate[*]}"))
# Get the last file ID in the template
# NOTE: The file ID in the template (FID:TID) is different from the file ID in the folders
# A file may also be skipped if no track are selected from the file (which is dumb, but that can happen)
maxFile=$(cut -d: -f1<<<${sorted[-1]})
declare -i currentFile=0
# For each file in the template
for i in $(seq 0 $maxFile)
do
declare -a tracks=()
# We find the next file in the folder structure
while ! [[ -v files["$currentFile $1"] ]]
do
currentFile=$currentFile+1
done
# We extract only the tracks that belong to this file
for j in "${!sorted[@]}"
do
[[ "$(cut -d: -f1<<<${sorted[$j]})" = "$i" ]] && tracks+=(${sorted[$j]})
done
# We separate tracks by their types beacause mkvmerge needs that separation to select tracks
# Since we will be parsing each track, we also print parameters for each track on the way
videoTracks=""
audioTracks=""
subsTracks=""
copyChapters="false"
copyAttachments="false"
declare -a missingFonts
IFS=$nl
for track in "${tracks[@]}"
do
parseLine "${track}"
currentID=$(cut -d: -f2<<<${currentID})
currentFilepath="$currentFile/${files[$currentFile $1]}"
# Apply the right "multi value" if needed
parseMVal "currentLang" "$currentFilepath"
parseMVal "currentName" "$currentFilepath"
parseMVal "currentShift" "$currentFilepath"
case "$currentType" in
"(V)")
[[ "$videoTracks" = "" ]] || videoTracks="${videoTracks},"
videoTracks="${videoTracks}${currentID}"
;;
"(A)")
[[ "$audioTracks" = "" ]] || audioTracks="${audioTracks},"
audioTracks="${audioTracks}${currentID}"
;;
"(S)")
[[ "$subsTracks" = "" ]] || subsTracks="${subsTracks},"
subsTracks="${subsTracks}${currentID}"
;;
"Ch.")
copyChapters="true"
;;
"At.")
copyAttachments="true"
;;
esac
# If we use MKVFontMan, we don't copy already existing attachments, we will add them later
if [[ "$MKVFONTMAN_LOADED" == "true" ]]
then
copyAttachments="false"
fi
# Special "tracks"
# TODO: Special case to copy only specific attachments from a file
if [[ "$currentType" = "Ch." ]]
then
# Chapter language
if ! [[ "$currentLang" = "" ]]
then
printf '\t"--chapter-language",\n'
printf '\t"'$currentLang'",\n'
fi
# Chapter shift if needed
if ! [[ "$currentShift" = "" ]]
then
printf '\t"--chapter-sync",\n'
printf '\t"'$currentShift'",\n'
fi
continue # We don't want to follow the classical tracks process
fi
if [[ "$currentType" = "At." ]]
then
# Well, we don't have anything to do here... Yet (see TODO above)
continue # We don't want to follow the classical tracks process
fi
# Default and forced track
defaultStatus=$(cut -c -1 <<<$currentDefault)
forcedStatus=$(cut -c 2- <<<$currentDefault)
printf '\t"--default-track",\n'
if [[ "$defaultStatus" = "D" ]]
then
printf '\t"'"$currentID"':true",\n'
else
printf '\t"'"$currentID"':false",\n'
fi
printf '\t"--forced-track",\n'
if [[ "$forcedStatus" = "F" ]]
then
printf '\t"'"$currentID"':true",\n'
else
printf '\t"'"$currentID"':false",\n'
fi
# Track language
if ! [[ "$currentLang" = "" ]]
then
printf '\t"--language",\n'
printf '\t"'"$currentID"':'"$currentLang"'",\n'
fi
# Track name
if ! [[ "$currentName" = "" ]]
then
printf '\t"--track-name",\n'
printf '\t"'"$currentID"':'"$currentName"'",\n'
fi
# Track shift if needed
if ! [[ "$currentShift" = "" ]]
then
printf '\t"--sync",\n'
printf '\t"'"$currentID"':'"$currentShift"'",\n'
fi
done
# Tracks
if [[ "$videoTracks" = "" ]]
then
printf '\t"--no-video",\n'
else
printf '\t"--video-tracks",\n'
printf '\t"'$videoTracks'",\n'
fi
if [[ "$audioTracks" = "" ]]
then
printf '\t"--no-audio",\n'
else
printf '\t"--audio-tracks",\n'
printf '\t"'$audioTracks'",\n'
fi
if [[ "$subsTracks" = "" ]]
then
printf '\t"--no-subtitles",\n'
else
printf '\t"--subtitle-tracks",\n'
printf '\t"'$subsTracks'",\n'
fi
# Special "Tracks"
if [[ "$copyChapters" = "false" ]]
then
printf '\t"--no-chapters",\n'
fi
if [[ "$copyAttachments" = "false" ]]
then
printf '\t"--no-attachments",\n'
fi
# File
printf '\t"'"$currentFilepath"'",\n'
# Extra attachments in the "attachments" folder (if not usink MKVFontMan)
IFS=$nl
if [[ -d "attachments" ]] && [[ "$MKVFONTMAN_LOADED" == "false" ]]
then
for attach in $(ls "attachments")
do
extension=$(echo "${attach##*.}" | awk '{print tolower($0)}')
if [[ -v MIME_OVERRIDES["$extension"] ]]
then
mime="${MIME_OVERRIDES[$extension]}"
else
mime="$(file --mime-type -b "attachments/$attach")"
fi
printf '\t"--attachment-name",\n'
printf '\t"'"$attach"'",\n'
printf '\t"--attachment-mime-type",\n'
printf '\t"'"$mime"'",\n'
printf '\t"--attach-file",\n'
printf '\t"attachments/'"$attach"'",\n'
done
fi
# Parse all subtitles to list needed fonts
if [[ "$MKVFONTMAN_LOADED" == "true" ]]
then
case "${currentFilepath: -3}" in
"mkv" | "mks" | "mka")
json="${jsons[$currentFile $1]}"
missingFonts+=($(printUsedFontsMatroska "$currentFilepath"))
;;
"ass" | "ssa")
missingFonts+=("$(printFontsAss "$currentFilepath")")
;;
esac 2>/dev/null # Remove error messages when there are no subtitles in the file
fi
currentFile=$currentFile+1
IFS=$nl
done
IFS="$nl"
if [[ "$MKVFONTMAN_LOADED" == "true" ]]
then
missingFonts=( $(printf '%s\n' "${missingFonts[@]}" | sort | uniq ) )
declare -a fontsToAdd
parseFontStore
if [[ "${!missingFonts[@]}" != "" ]]
then
printf "$COLOR_WARNING">&2
echo "WARNING: The following fonts are missing: ${missingFonts[@]}">&2
printf "$COLOR_RESET">&2
fi
for attach in "${fontsToAdd[@]}"
do
extension=$(echo "${attach##*.}" | awk '{print tolower($0)}')
if [[ -v MIME_OVERRIDES["$extension"] ]]
then
mime="${MIME_OVERRIDES[$extension]}"
else
mime="$(file --mime-type -b "$FONTSTORE/$attach")"
fi
printf '\t"--attachment-name",\n'
printf '\t"'"$attach"'",\n'
printf '\t"--attachment-mime-type",\n'
printf '\t"'"$mime"'",\n'
printf '\t"--attach-file",\n'
printf '\t"'"$FONTSTORE"'/'"$attach"'",\n'
done
fi
# Finally, we add the track order
printf '\t"--track-order",\n'
printf '\t"'$trackOrder'"\n'
# And we clone the json array
printf "]"
}
# Swaps line $1 with line $2
function swapLines() {
swapedLine="${currentTemplate[$2]}"
currentTemplate[$2]="${currentTemplate[$1]}"
currentTemplate[$1]="$swapedLine"
swapedLine="${screenLinesSelected[$2]}"
screenLinesSelected[$2]="${screenLinesSelected[$1]}"
screenLinesSelected[$1]="$swapedLine"
swapedLine="${screenLinesUnselected[$2]}"
screenLinesUnselected[$2]="${screenLinesUnselected[$1]}"
screenLinesUnselected[$1]="$swapedLine"
}
# Handle settings imput
# $1: prompt
# $2: type ("string" or "integer")
# $3: variable to set
# $4: enable multiple values (defaults to true)
function valueInput() {
errorPrinted="false"
stty echo
while :
do
printf "\e[2K"
# If no error is printed and multiple values are enabled, print the hint below
if [[ "$4" != "false" ]] && [[ "$errorPrinted" = "false" ]]
then
printf '\n'
printf "${COLOR_HINT}You can use \'*\' to set different values for different files${COLOR_RESET}"
# Go one line up
printf '\e[1A\e[0G'
fi
eval 'read -e -p "'$1'" -i "$'$3'" newvalue'
errorPrinted="false"
# If the input is "*", we enter multi values mode if enabled
if [[ "$4" != "false" ]] && [[ "$newvalue" = "*" ]]
then
multiValueInput "$1" "$2" "newvalue" "false"
break
fi
# If we want a string, we break immediatly to skip integer check
[[ "$2" = "string" ]] && break
# If the filed is empty
if [ -z "$newvalue" ]
then
newvalue=""
break
fi
# Else, we check that it is a correct integer https://stackoverflow.com/a/19116862
if [ "$newvalue" -eq "$newvalue" ] 2>/dev/null
then
break
else
printf "\e[2K${COLOR_ERROR}Please input a correct integer!${COLOR_RESET}\e[1A\e[0G"
errorPrinted="true"
fi
done
printf "\e[2K\e[1A\e[2K"
eval "$3=\$newvalue"
}
# Handle different inputs for different files
# $1: prompt
# $2: type (see above)
# $3: variable to set
# $t (must be set): current template
function multiValueInput() {
# Get the file ID from the selectd track
currentFID=$(cut -d: -f1<<<${currentID})
declare -a mValFilename
declare -a mValIds
declare -a mValIsSelected
declare -a mValValues
declare -i mValFilesNb=0
# Look for all the files
IFS=$nl
for file in $(seq 0 $(($fileNb - 1)))
do
# If the file don't belong to this template, we skip it
[[ "${fileTemplateMap[$file]}" = "$t" ]] || continue
# Now we look for the Nth folder containing the file, with N being the FID
declare -i filesFound=-1
declare -i iFolder=0
while ! [[ "$filesFound" = "$currentFID" ]]
do
if [[ -v files["$iFolder $file"] ]]
then
filesFound=$filesFound+1
fi
[[ "$filesFound" = "$currentFID" ]] && break
iFolder=$iFolder+1
done
mValFilename+=("$iFolder/${files[$iFolder $file]}")
mValIds+=("$file")
mValIsSelected+=("false")
mValFilesNb=$mValFilesNb+1
mValValues+=("")
done
declare -i mValPos=0
# Interface loop
while :
do
stty -echo
printf '\e[?25l'
declare -i mValPrintedLines=0
# Print all files
for mValI in "${!mValFilename[@]}"
do
printf "\e[2K"
[[ "$mValPos" = "$mValI" ]] && printf "$COLOR_CURRENT_LINE"
if [[ "${mValIsSelected[$mValI]}" = "true" ]]
then
printf "> "
else
printf " "
fi
printf "${mValFilename[$mValI]}: ${mValValues[$mValI]} ${COLOR_RESET}\n"
mValPrintedLines=$mValPrintedLines+1
done
printf "\n"
echo "UP/DOWN: Highlight file"
echo "SPACE: select/unselect file for bulk editing"
echo "E: edit values for all selected files (if any) or the current highlited file if none are selected"
echo "A: select all files"
echo "U: unselect all files"
echo "RETURN: Apply values"
mValPrintedLines=$mValPrintedLines+7
# Flush keyboard buffer
while read -t 0.01; do :; done
# https://stackoverflow.com/a/46481173
read -rsn1 mode
if [[ $mode == $(printf "\u1b") ]]
then
read -rsn2 mode # read 2 more chars
fi
printf "\e[K"
case $mode in
'[A') # UP
mValPos=$mValPos-1
[ "$mValPos" = "-1" ] && mValPos=${mValFilesNb}-1
;;
'[B') # DOWN
mValPos=$mValPos+1
[ "$mValPos" = "$mValFilesNb" ] && mValPos=0
;;
'e' | 'E') # Change value
valueInput "$1" "$2" "mValNewVal" "false"
mValHasSelection="false"
for mValI in "${!mValIsSelected[@]}"
do
if [[ "${mValIsSelected[$mValI]}" = "true" ]]
then
mValHasSelection="true"
mValValues[$mValI]="$mValNewVal"
mValIsSelected[$mValI]="false" # After changing, we unselect since the selection is not needed anymore
fi
done
# If no file have bee selected
[[ "$mValHasSelection" = "false" ]] && mValValues[$mValPos]="$mValNewVal"
;;
'a' | 'A') # Select all
for mValI in "${!mValIsSelected[@]}"
do
mValIsSelected[$mValI]="true"
done
;;
'u' | 'U') # Unselect all
for mValI in "${!mValIsSelected[@]}"
do
mValIsSelected[$mValI]="false"
done
;;
" ") #SPACE
if [[ "${mValIsSelected[$mValPos]}" = "true" ]]
then
mValIsSelected[$mValPos]="false"
else
mValIsSelected[$mValPos]="true"
fi
;;
"") # RETURN
break
;;
esac
# Go up top
printf "\e[${mValPrintedLines}A\e[0G"
done
# Now, set the variable
mValFinalValue="*( "
for mValI in "${!mValFilename[@]}"
do
mValFinalValue="$mValFinalValue [\"${mValFilename[mValI]}\"]=\"${mValValues[mValI]}\""
done
mValFinalValue="$mValFinalValue )"
eval "$3=\$mValFinalValue"
# Remove all the mess in the console
nprintf "\e[2K\e[1A" $mValPrintedLines
# Since a lot of files could be printed, it is preferable to redraw the main interface
mustRedraw=true
unset mValFinalValue
unset mValFilename
unset mValIds
unset mValPos
unset mValFilesNb
unset mValI
unset mValIsSelected
unset mValPrintedLines
unset mValValues
unset mValNewVal
unset mValHasSelection
}
# ==============================================
# Loads file information
declare -i fileNb=0
# Loops through each numbered folders
for folder in $(find . -type d -regex \.\\/[0-9]* | sort -n)
do
trimmedFolder=$(echo $folder | cut -c 3-) # (cut -c 3-) return N instead of ./N
echo Parsing folder $trimmedFolder:
folders+=($trimmedFolder)
# Loops trough each file in $folder
IFS=$nl
for file in $(ls $folder)
do
echo \>Parsing file $file...
# Add $file into $files only if $file does not already exists
# Based on https://stackoverflow.com/a/47541882
# if ! printf '%s\n' "${files[@]}" | grep -q -P $(eval echo "'^$file\$'")
# then
# files+=($file)
# fi
# Add the $file to the $files array and its json
found=false
json="$(mkvmerge -J $folder/$file)"
regexFound=false
for regex in "${!DEFAULT_FILE_REGEX[@]}"
do
if [[ ${file%.*} =~ ${DEFAULT_FILE_REGEX[$regex]} ]]
then
regex_match="${BASH_REMATCH[${DEFAULT_REGEX_MATCH_NB[$regex]}]}"
regexFound=true
break
fi
done
if [[ "$regexFound" = "false" ]]
then
regex_match="${file%.*}"
fi
for i in $(seq 0 $(($fileNb - 1)))
do
if [[ "${files[regex $i]}" = "$regex_match" ]]
then
found=true
files["$trimmedFolder $i"]="$file"
jsons["$trimmedFolder $i"]="$json"
break
fi
[[ "$found" = true ]] && break;
done
# If the file don't exist at all, we add it at a new id
if [[ "$found" = false ]]
then
files["$trimmedFolder $fileNb"]="$file"
files["regex $fileNb"]="$regex_match"
jsons["$trimmedFolder $fileNb"]="$json"
fileNb=$fileNb+1
fi
done
done
# ==============================================
# Parses the jsons to make the templates
# For each file
declare -i fileID
declare -i lastIndex=0
for file in $(seq 0 $(($fileNb - 1)))
do
currentTemplate=""
fileID=0
for folder in "${folders[@]}"
do
# If the file don't exist, we skip
[[ -v files["$folder $file"] ]] || continue;
currentTemplate+=$(echo ${jsons[$folder $file]} | jq -r '.tracks | map( "'$fileID':" + (.id | tostring) + ";" + (if .type == "video" then "(V)" elif .type == "audio" then "(A)" elif .type == "subtitles" then "(S)" else "(?)" end)+ ";" + (if .properties.default_track == true then "D" else "-" end) + (if .properties.forced_track == true then "D" else "-" end) + ";"+ .properties.language + ";" + .codec + ";" + .properties.track_name +";;") | join("\n") ')
currentTemplate+=$nl
# Add chapters if they exists
fileChapters=$(echo ${jsons[$folder $file]} | jq -r '.chapters | map (.num_entries | tostring) | join("\n")')
if [[ ! "$fileChapters" = "" ]]
then
currentTemplate+="${fileID}:C;Ch.;--;;Chapters;${fileChapters} chapter(s);;"
currentTemplate+=$nl
fi
# Same thing with attachments
fileAttach=$(echo ${jsons[$folder $file]} | jq -r '.attachments | length')
if [[ ! "$fileAttach" = "0" ]]
then
currentTemplate+="${fileID}:A;At.;--;;Attachments;${fileAttach} attachment(s);;"
currentTemplate+=$nl
fi
fileID=$fileID+1
done
currentTemplate=${currentTemplate%$nl} # Remove the trailing \n
# Add the current template to the templates list (without duplicates) and add the matching index to $fileTemplateMap
found=false
#Since the IFS typically contains \n, it messes up when $currentTemplate is added to the array by removing all the \n.
IFS=""
for i in "${!templates[@]}"
do
template="${templates[$i]}"
if [ "$template" = "$currentTemplate" ]
then
found=true
fileTemplateMap[$file]=$i
break
fi
done
if [ "$found" = false ]
then
templates+=("$currentTemplate")
fileTemplateMap[$file]=$lastIndex
lastIndex=$lastIndex+1
fi
done
# ==============================================
# Prints the templates and the file names
printFiles | column -ts "$(printf "\t")"
# ==============================================
# Asks the user the final settings for each template and generate the json with the arguments for mkvmerge
echo "======================"
echo "${#templates[@]} template(s) found"
declare -i headerSize=$(wc -l <<<$(printHeader))
declare -i footerSize=$(wc -l <<<$(printFooter))
for t in "${!templates[@]}"
do
declare -i pos=0
declare -a currentTemplate
IFS=$nl
# We put each line of $template in an array element. We use the IFS as \n to do that
currentTemplate=(${templates[$t]})
arraySize=${#currentTemplate[@]}
declare -a screenLinesUnselected
declare -a screenLinesSelected
screenLinesUnselected=($(printAllLines false))
screenLinesSelected=($(printAllLines true))
stty -echo
printf '\e[?25l'
printHeader
# Go down (tracks number) lines to print the footer
nprintf "\n" ${arraySize}
printFooter
while :
do
# We print the template
IFS=$nl
# Disable keyboard echo and hide terminal cursor
stty -echo
printf '\e[?25l'
# Go up (footer size+array size) lines to print the array
printf "\e[${arraySize}A\e[${footerSize}A\e[0G"
for i in "${!screenLinesUnselected[@]}"
do
if [[ $i -eq $pos ]]
then
printf -- "${screenLinesSelected[$i]}\n"
else
printf -- "${screenLinesUnselected[$i]}\n"
fi
done
# Go down (footer size) lines
printf "\e[${footerSize}B\e[0G"
# Show terminal cursor
printf '\e[?25h'
# We parse the current line with the IFS as ; for editing the line
parseLine "${currentTemplate[$pos]}"
# We process user input
# Flush keyboard buffer
while read -t 0.01; do :; done
# https://stackoverflow.com/a/46481173
read -rsn1 mode
if [[ $mode == $(printf "\u1b") ]]
then
read -rsn2 mode # read 2 more chars
fi
printf "\e[K"
IFS=$nl
mustRedraw=false
mustRegenerateLine=false
case $mode in
'[A') # UP
pos=$pos-1
[ "$pos" = "-1" ] && pos=${#currentTemplate[@]}-1
;;
'[B') # DOWN
pos=$pos+1
[ "$pos" = "${#currentTemplate[@]}" ] && pos=0
;;
'-' | '[5') #PGUP - Shift track upwards
if [[ "$pos" = "0" ]]
then
printf "${COLOR_ERROR}You are already at the top!${COLOR_RESET}"