forked from woniuzfb/iptv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·1569 lines (1402 loc) · 45.8 KB
/
build.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
set -euo pipefail
green="\033[32m"
red="\033[31m"
normal="\033[0m"
info="${green}[信息]${normal}"
error="${red}[错误]${normal}"
Println()
{
printf '%b' "\n$1\n"
}
inquirer()
{
if [[ ! -x $(command -v tput) ]]
then
if apt-get -y install ncurses-bin >/dev/null 2>&1
then
Println "$info 依赖 tput 安装成功..."
else
Println "$error 依赖 tput 安装失败..."
exit 1
fi
fi
local arrow checked unchecked red green blue cyan bold normal dim
arrow=$(echo -e '\xe2\x9d\xaf')
checked=$(echo -e '\xe2\x97\x89')
unchecked=$(echo -e '\xe2\x97\xaf')
red=$(tput setaf 1)
green=$(tput setaf 2)
blue=$(tput setaf 4)
cyan=$(tput setaf 6)
bold=$(tput bold)
normal=$(tput sgr0)
dim=$'\e[2m'
inquirer:print() {
echo "$1"
tput el
}
inquirer:join() {
local IFS=$'\n'
local var=("$1"[@])
local _join_list=("${!var}")
local first=true
for item in "${_join_list[@]}"
do
if [ "$first" = true ]
then
printf "%s" "$item"
first=false
else
printf "${2-, }%s" "$item"
fi
done
}
inquirer:gen_env_from_options() {
local IFS=$'\n'
local var=("$1"[@])
local _indices=("${!var}")
var=("$2"[@])
local _env_names=("${!var}")
local _checkbox_selected
for i in $(inquirer:gen_index ${#_env_names[@]})
do
_checkbox_selected[i]=false
done
for i in "${_indices[@]}"
do
_checkbox_selected[i]=true
done
for i in $(inquirer:gen_index ${#_env_names[@]})
do
printf "%s=%s\n" "${_env_names[i]}" "${_checkbox_selected[i]}"
done
}
inquirer:on_default() {
true;
}
inquirer:on_keypress() {
local OLD_IFS=$IFS
local key
local on_up=${1:-inquirer:on_default}
local on_down=${2:-inquirer:on_default}
local on_space=${3:-inquirer:on_default}
local on_enter=${4:-inquirer:on_default}
local on_left=${5:-inquirer:on_default}
local on_right=${6:-inquirer:on_default}
local on_ascii=${7:-inquirer:on_default}
local on_backspace=${8:-inquirer:on_default}
local on_not_ascii=${9:-inquirer:on_default}
_break_keypress=false
while IFS="" read -rsn1 key
do
case "$key" in
$'\x1b')
read -rsn1 key
if [ "$key" == "[" ]
then
read -rsn1 key
case "$key" in
'A') $on_up;;
'B') $on_down;;
'D') $on_left;;
'C') $on_right;;
esac
fi
;;
$'\x20') $on_space;;
$'\x7f') $on_backspace "$key";;
'') $on_enter "$key";;
*[$'\x80'-$'\xFF']*) $on_not_ascii "$key";;
# [^ -~]
*) $on_ascii "$key";;
esac
if [ "$_break_keypress" = true ]
then
break
fi
done
IFS=$OLD_IFS
}
inquirer:gen_index() {
local k=$1
local l=0
for((l=0;l<k;l++));
do
echo $l
done
}
inquirer:cleanup() {
# Reset character attributes, make cursor visible, and restore
# previous screen contents (if possible).
tput sgr0
tput cnorm
stty echo
}
inquirer:control_c() {
inquirer:cleanup
exit $?
}
inquirer:select_indices() {
local var=("$1"[@])
local _select_list
read -r -a _select_list <<< "${!var}"
var=("$2"[@])
local _select_indices
read -r -a _select_indices <<< "${!var}"
local _select_var_name=$3
declare -a new_array
for i in $(inquirer:gen_index ${#_select_indices[@]})
do
new_array+=("${_select_list[${_select_indices[i]}]}")
done
read -r -a ${_select_var_name?} <<< "${new_array[@]}"
unset new_array
}
inquirer:on_checkbox_input_up() {
inquirer:remove_checkbox_instructions
tput cub "$(tput cols)"
if [ "${_checkbox_selected[$_current_index]}" = true ]
then
printf '%s' " ${green}${checked}${normal} ${_checkbox_list[$_current_index]} ${normal}"
else
printf '%s' " ${unchecked} ${_checkbox_list[$_current_index]} ${normal}"
fi
tput el
if [ $_current_index = 0 ]
then
_current_index=$((${#_checkbox_list[@]}-1))
tput cud $((${#_checkbox_list[@]}-1))
tput cub "$(tput cols)"
else
_current_index=$((_current_index-1))
tput cuu1
tput cub "$(tput cols)"
tput el
fi
if [ "${_checkbox_selected[$_current_index]}" = true ]
then
printf '%s' "${cyan}${arrow}${green}${checked}${normal} ${_checkbox_list[$_current_index]} ${normal}"
else
printf '%s' "${cyan}${arrow}${normal}${unchecked} ${_checkbox_list[$_current_index]} ${normal}"
fi
}
inquirer:on_checkbox_input_down() {
inquirer:remove_checkbox_instructions
tput cub "$(tput cols)"
if [ "${_checkbox_selected[$_current_index]}" = true ]
then
printf '%s' " ${green}${checked}${normal} ${_checkbox_list[$_current_index]} ${normal}"
else
printf '%s' " ${unchecked} ${_checkbox_list[$_current_index]} ${normal}"
fi
tput el
if [ $_current_index = $((${#_checkbox_list[@]}-1)) ]
then
_current_index=0
tput cuu $((${#_checkbox_list[@]}-1))
tput cub "$(tput cols)"
else
_current_index=$((_current_index+1))
tput cud1
tput cub "$(tput cols)"
tput el
fi
if [ "${_checkbox_selected[$_current_index]}" = true ]
then
printf '%s' "${cyan}${arrow}${green}${checked}${normal} ${_checkbox_list[$_current_index]} ${normal}"
else
printf '%s' "${cyan}${arrow}${normal}${unchecked} ${_checkbox_list[$_current_index]} ${normal}"
fi
}
inquirer:on_checkbox_input_enter() {
local OLD_IFS=$IFS
_checkbox_selected_indices=()
_checkbox_selected_options=()
IFS=$'\n'
for i in $(inquirer:gen_index ${#_checkbox_list[@]})
do
if [ "${_checkbox_selected[i]}" = true ]
then
_checkbox_selected_indices+=("$i")
_checkbox_selected_options+=("${_checkbox_list[i]}")
fi
done
tput cud $((${#_checkbox_list[@]}-_current_index))
tput cub "$(tput cols)"
for i in $(seq $((${#_checkbox_list[@]}+1)))
do
tput el1
tput el
tput cuu1
done
tput cub "$(tput cols)"
tput cuf $((prompt_width+3))
printf '%s' "${cyan}$(inquirer:join _checkbox_selected_options)${normal}"
tput el
tput cud1
tput cub "$(tput cols)"
tput el
_break_keypress=true
IFS=$OLD_IFS
}
inquirer:on_checkbox_input_space() {
inquirer:remove_checkbox_instructions
tput cub "$(tput cols)"
tput el
if [ "${_checkbox_selected[$_current_index]}" = true ]
then
_checkbox_selected[$_current_index]=false
else
_checkbox_selected[$_current_index]=true
fi
if [ "${_checkbox_selected[$_current_index]}" = true ]
then
printf '%s' "${cyan}${arrow}${green}${checked}${normal} ${_checkbox_list[$_current_index]} ${normal}"
else
printf '%s' "${cyan}${arrow}${normal}${unchecked} ${_checkbox_list[$_current_index]} ${normal}"
fi
}
inquirer:remove_checkbox_instructions() {
if [ "$_first_keystroke" = true ]
then
tput cuu $((_current_index+1))
tput cub "$(tput cols)"
tput cuf $((prompt_width+3))
tput el
tput cud $((_current_index+1))
_first_keystroke=false
fi
}
inquirer:on_checkbox_input_ascii() {
local key=$1
case "$key" in
"w" ) inquirer:on_checkbox_input_up;;
"s" ) inquirer:on_checkbox_input_down;;
esac
}
inquirer:_checkbox_input() {
local i j var=("$2"[@])
_checkbox_list=("${!var}")
_current_index=0
_first_keystroke=true
trap inquirer:control_c SIGINT EXIT
stty -echo
tput civis
inquirer:print "${green}?${normal} ${bold}${prompt}${normal} ${dim}(按 <space> 选择, <enter> 确认)${normal}"
for i in $(inquirer:gen_index ${#_checkbox_list[@]})
do
_checkbox_selected[i]=false
done
if [ -n "${3:-}" ]
then
var=("$3"[@])
_selected_indices=("${!var}")
for i in "${_selected_indices[@]}"
do
_checkbox_selected[i]=true
done
fi
for i in $(inquirer:gen_index ${#_checkbox_list[@]})
do
tput cub "$(tput cols)"
if [ $i = 0 ]
then
if [ "${_checkbox_selected[i]}" = true ]
then
inquirer:print "${cyan}${arrow}${green}${checked}${normal} ${_checkbox_list[i]} ${normal}"
else
inquirer:print "${cyan}${arrow}${normal}${unchecked} ${_checkbox_list[i]} ${normal}"
fi
else
if [ "${_checkbox_selected[i]}" = true ]
then
inquirer:print " ${green}${checked}${normal} ${_checkbox_list[i]} ${normal}"
else
inquirer:print " ${unchecked} ${_checkbox_list[i]} ${normal}"
fi
fi
tput el
done
for j in $(inquirer:gen_index ${#_checkbox_list[@]})
do
tput cuu1
done
inquirer:on_keypress inquirer:on_checkbox_input_up inquirer:on_checkbox_input_down inquirer:on_checkbox_input_space inquirer:on_checkbox_input_enter inquirer:on_default inquirer:on_default inquirer:on_checkbox_input_ascii
}
inquirer:checkbox_input() {
inquirer:_checkbox_input "$1" "$2"
_checkbox_input_output_var_name=$3
inquirer:select_indices _checkbox_list _checkbox_selected_indices $_checkbox_input_output_var_name
unset _checkbox_list
unset _break_keypress
unset _first_keystroke
unset _current_index
unset _checkbox_input_output_var_name
unset _checkbox_selected_indices
unset _checkbox_selected_options
inquirer:cleanup
}
inquirer:checkbox_input_indices() {
inquirer:_checkbox_input "$1" "$2" "$3"
_checkbox_input_output_var_name=$3
declare -a new_array
for i in $(inquirer:gen_index ${#_checkbox_selected_indices[@]})
do
new_array+=("${_checkbox_selected_indices[i]}")
done
read -r -a ${_checkbox_input_output_var_name?} <<< "${new_array[@]}"
unset new_array
unset _checkbox_list
unset _break_keypress
unset _first_keystroke
unset _current_index
unset _checkbox_input_output_var_name
unset _checkbox_selected_indices
unset _checkbox_selected_options
inquirer:cleanup
}
inquirer:on_list_input_up() {
inquirer:remove_list_instructions
tput cub "$(tput cols)"
printf '%s' " ${_list_options[$_list_selected_index]}"
tput el
if [ $_list_selected_index = 0 ]
then
_list_selected_index=$((${#_list_options[@]}-1))
tput cud $((${#_list_options[@]}-1))
tput cub "$(tput cols)"
else
_list_selected_index=$((_list_selected_index-1))
tput cuu1
tput cub "$(tput cols)"
tput el
fi
printf "${cyan}${arrow} %s ${normal}" "${_list_options[$_list_selected_index]}"
}
inquirer:on_list_input_down() {
inquirer:remove_list_instructions
tput cub "$(tput cols)"
printf '%s' " ${_list_options[$_list_selected_index]}"
tput el
if [ $_list_selected_index = $((${#_list_options[@]}-1)) ]
then
_list_selected_index=0
tput cuu $((${#_list_options[@]}-1))
tput cub "$(tput cols)"
else
_list_selected_index=$((_list_selected_index+1))
tput cud1
tput cub "$(tput cols)"
tput el
fi
printf "${cyan}${arrow} %s ${normal}" "${_list_options[$_list_selected_index]}"
}
inquirer:on_list_input_enter_space() {
local OLD_IFS=$IFS
IFS=$'\n'
tput cud $((${#_list_options[@]}-_list_selected_index))
tput cub "$(tput cols)"
for i in $(seq $((${#_list_options[@]}+1)))
do
tput el1
tput el
tput cuu1
done
tput cub "$(tput cols)"
tput cuf $((prompt_width+3))
printf '%s' "${cyan}${_list_options[$_list_selected_index]}${normal}"
tput el
tput cud1
tput cub "$(tput cols)"
tput el
_break_keypress=true
IFS=$OLD_IFS
}
inquirer:on_list_input_input_ascii()
{
local key=$1
case "$key" in
"w" ) inquirer:on_list_input_up;;
"s" ) inquirer:on_list_input_down;;
esac
}
inquirer:remove_list_instructions() {
if [ "$_first_keystroke" = true ]
then
tput cuu $((_list_selected_index+1))
tput cub "$(tput cols)"
tput cuf $((prompt_width+3))
tput el
tput cud $((_list_selected_index+1))
_first_keystroke=false
fi
}
inquirer:_list_input() {
local i j var=("$2"[@])
_list_options=("${!var}")
_list_selected_index=0
_first_keystroke=true
trap inquirer:control_c SIGINT EXIT
stty -echo
tput civis
inquirer:print "${green}?${normal} ${bold}${prompt}${normal} ${dim}(使用上下箭头选择)${normal}"
for i in $(inquirer:gen_index ${#_list_options[@]})
do
tput cub "$(tput cols)"
if [ $i = 0 ]
then
inquirer:print "${cyan}${arrow} ${_list_options[i]} ${normal}"
else
inquirer:print " ${_list_options[i]}"
fi
tput el
done
for j in $(inquirer:gen_index ${#_list_options[@]})
do
tput cuu1
done
inquirer:on_keypress inquirer:on_list_input_up inquirer:on_list_input_down inquirer:on_list_input_enter_space inquirer:on_list_input_enter_space inquirer:on_default inquirer:on_default inquirer:on_list_input_input_ascii
}
inquirer:list_input() {
inquirer:_list_input "$1" "$2"
var_name=$3
read -r ${var_name?} <<< "${_list_options[$_list_selected_index]}"
unset _list_selected_index
unset _list_options
unset _break_keypress
unset _first_keystroke
inquirer:cleanup
}
inquirer:list_input_index() {
inquirer:_list_input "$1" "$2"
var_name=$3
read -r ${var_name?} <<< "$_list_selected_index"
unset _list_selected_index
unset _list_options
unset _break_keypress
unset _first_keystroke
inquirer:cleanup
}
inquirer:on_text_input_left() {
inquirer:remove_regex_failed
if [[ $_current_pos -gt 0 ]]
then
local current=${_text_input:$_current_pos:1} current_width
current_width=$(inquirer:display_length "$current")
tput cub $current_width
_current_pos=$((_current_pos-1))
fi
}
inquirer:on_text_input_right() {
inquirer:remove_regex_failed
if [[ $((_current_pos+1)) -eq ${#_text_input} ]]
then
tput cuf1
_current_pos=$((_current_pos+1))
elif [[ $_current_pos -lt ${#_text_input} ]]
then
local next=${_text_input:$((_current_pos+1)):1} next_width
next_width=$(inquirer:display_length "$next")
tput cuf $next_width
_current_pos=$((_current_pos+1))
fi
}
inquirer:on_text_input_enter() {
inquirer:remove_regex_failed
_text_input=${_text_input:-$_text_default_value}
if [[ $($_text_input_validator "$_text_input") = true ]]
then
tput cuu 1
tput cub "$(tput cols)"
tput cuf $((prompt_width+3))
printf '%s' "${cyan}${_text_input}${normal}"
tput el
tput cud1
tput cub "$(tput cols)"
tput el
read -r ${var_name?} <<< "$_text_input"
_break_keypress=true
else
_text_input_regex_failed=true
tput civis
tput cuu1
tput cub "$(tput cols)"
tput cuf $((prompt_width+3))
tput el
tput cud1
tput cub "$(tput cols)"
tput el
tput cud1
tput cub "$(tput cols)"
printf '%b' "${red}$_text_input_regex_failed_msg${normal}"
tput el
_text_input=""
_current_pos=0
tput cnorm
fi
}
inquirer:on_text_input_ascii() {
inquirer:remove_regex_failed
local c=${1:- }
local rest=${_text_input:$_current_pos} rest_width
local current=${_text_input:$_current_pos:1} current_width
rest_width=$(inquirer:display_length "$rest")
current_width=$(inquirer:display_length "$current")
_text_input="${_text_input:0:$_current_pos}$c$rest"
_current_pos=$((_current_pos+1))
tput civis
[[ $current_width -gt 1 ]] && tput cub $((current_width-1))
printf '%s' "$c$rest"
tput el
if [[ $rest_width -gt 0 ]]
then
tput cub $((rest_width-current_width+1))
fi
tput cnorm
}
inquirer:display_length() {
local display_length=0 byte_len
local oLC_ALL=${LC_ALL:-} oLANG=${LANG:-} LC_ALL=${LC_ALL:-} LANG=${LANG:-}
while IFS="" read -rsn1 char
do
case "$char" in
'')
;;
*[$'\x80'-$'\xFF']*)
LC_ALL='' LANG=C
byte_len=${#char}
LC_ALL=$oLC_ALL LANG=$oLANG
if [[ $byte_len -eq 2 ]]
then
display_length=$((display_length+1))
else
display_length=$((display_length+2))
fi
;;
*)
display_length=$((display_length+1))
;;
esac
done <<< "$1"
echo "$display_length"
}
inquirer:on_text_input_not_ascii() {
inquirer:remove_regex_failed
local c=$1
local rest="${_text_input:$_current_pos}" rest_width
local current=${_text_input:$_current_pos:1} current_width
rest_width=$(inquirer:display_length "$rest")
current_width=$(inquirer:display_length "$current")
_text_input="${_text_input:0:$_current_pos}$c$rest"
_current_pos=$((_current_pos+1))
tput civis
[[ $current_width -gt 1 ]] && tput cub $((current_width-1))
printf '%s' "$c$rest"
tput el
if [[ $rest_width -gt 0 ]]
then
tput cub $((rest_width-current_width+1))
fi
tput cnorm
}
inquirer:on_text_input_backspace() {
inquirer:remove_regex_failed
if [ $_current_pos -gt 0 ] || { [ $_current_pos -eq 0 ] && [ "${#_text_input}" -gt 0 ]; }
then
local start rest rest_width del del_width next next_width offset
local current=${_text_input:$_current_pos:1} current_width
current_width=$(inquirer:display_length "$current")
tput civis
if [ $_current_pos -eq 0 ]
then
rest=${_text_input:$((_current_pos+1))}
next=${_text_input:$((_current_pos+1)):1}
rest_width=$(inquirer:display_length "$rest")
next_width=$(inquirer:display_length "$next")
offset=$((current_width-1))
[[ $offset -gt 0 ]] && tput cub $offset
printf '%s' "$rest"
tput el
offset=$((rest_width-next_width+1))
[[ $offset -gt 0 ]] && tput cub $offset
_text_input=$rest
else
rest=${_text_input:$_current_pos}
start=${_text_input:0:$((_current_pos-1))}
del=${_text_input:$((_current_pos-1)):1}
rest_width=$(inquirer:display_length "$rest")
del_width=$(inquirer:display_length "$del")
_current_pos=$((_current_pos-1))
if [[ $current_width -gt 1 ]]
then
tput cub $((del_width+current_width-1))
printf '%s' "$rest"
tput el
tput cub $((rest_width-current_width+1))
else
tput cub $del_width
printf '%s' "$rest"
tput el
[[ $rest_width -gt 0 ]] && tput cub $((rest_width-current_width+1))
fi
_text_input="$start$rest"
fi
tput cnorm
fi
}
inquirer:remove_regex_failed() {
if [ "$_text_input_regex_failed" = true ]
then
_text_input_regex_failed=false
tput sc
tput cud1
tput el1
tput el
tput rc
fi
}
inquirer:text_input_default_validator() {
echo true;
}
inquirer:text_input() {
var_name=$2
if [ -n "$_text_default_value" ]
then
_text_default_tip=" $dim($_text_default_value)"
else
_text_default_tip=""
fi
_text_input_regex_failed_msg=${4:-"输入验证错误"}
_text_input_validator=${5:-inquirer:text_input_default_validator}
_text_input_regex_failed=false
inquirer:print "${green}?${normal} ${bold}${prompt}$_text_default_tip${normal}"
trap inquirer:control_c SIGINT EXIT
stty -echo
tput cnorm
inquirer:on_keypress inquirer:on_default inquirer:on_default inquirer:on_text_input_ascii inquirer:on_text_input_enter inquirer:on_text_input_left inquirer:on_text_input_right inquirer:on_text_input_ascii inquirer:on_text_input_backspace inquirer:on_text_input_not_ascii
read -r ${var_name?} <<< "$_text_input"
inquirer:cleanup
}
local option=$1
shift
local var_name prompt=${1:-} prompt_width _text_default_value=${3:-} _current_pos=0 _text_input="" _text_input_regex_failed_msg _text_input_validator _text_input_regex_failed
prompt_width=$(inquirer:display_length "$prompt")
inquirer:$option "$@"
}
CompileFFmpeg()
{
echo
tls_options=( 'gnutls' 'openssl' )
inquirer list_input "选择 tls" tls_options tls_option
nproc="-j$(nproc 2> /dev/null)" || nproc=""
export CMAKE_PREFIX_PATH="$HOME/ffmpeg_build"
export PATH="$HOME/ffmpeg_build/bin:$PATH"
export LDFLAGS="-L$HOME/ffmpeg_build/lib"
export DYLD_LIBRARY_PATH="$HOME/ffmpeg_build/lib"
export PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig"
export CFLAGS="-I$HOME/ffmpeg_build/include $LDFLAGS"
# zlib
cd ~/ffmpeg_sources
if [ ! -d zlib-1.2.11 ]
then
[ ! -f zlib-1.2.11.tar.gz ] && curl -L "https://www.zlib.net/zlib-1.2.11.tar.gz" -o zlib-1.2.11.tar.gz
tar xzf zlib-1.2.11.tar.gz
fi
cd zlib-1.2.11
if [ -f Makefile ]
then
make distclean || true
fi
./configure --prefix="$HOME/ffmpeg_build" --static
make $nproc
make install
# CMake
cmake_install=1
if [[ -x $(command -v cmake) ]]
then
cmake_ver=$(cmake --version | awk '{print $3}' | head -1)
if [[ $cmake_ver =~ ([^.]+).([^.]+).([^.]+) ]] && [[ ${BASH_REMATCH[1]} -lt 14 ]]
then
apt-get -y remove cmake
hash -r
else
cmake_install=0
fi
fi
if [ "$cmake_install" -eq 1 ]
then
cd ~/ffmpeg_sources
if [ ! -d CMake-3.18.4 ]
then
[ ! -f cmake-3.18.4.tar.gz ] && curl -L "https://github.com/Kitware/CMake/archive/v3.18.4.tar.gz" -o cmake-3.18.4.tar.gz
tar xzf cmake-3.18.4.tar.gz
fi
cd CMake-3.18.4
if [ -f Makefile ]
then
make distclean || true
fi
./bootstrap
make $nproc
make install
fi
# libbz2
cd ~/ffmpeg_sources
if [ ! -d bzip2-1.0.6 ]
then
[ ! -f bzip2-1.0.6.tar.gz ] && curl -L "https://downloads.sourceforge.net/bzip2/bzip2-1.0.6.tar.gz" -o bzip2-1.0.6.tar.gz
tar xzf bzip2-1.0.6.tar.gz
fi
cd bzip2-1.0.6
if [ -f Makefile ]
then
make distclean || true
fi
make
make install PREFIX="$HOME/ffmpeg_build"
echo "prefix=$HOME/ffmpeg_build
exec_prefix=\${prefix}
libdir=\${prefix}/lib
includedir=\${prefix}/include
Name: bzip2
Description: bzip2
Version: 1.0.6
Requires:
Libs: -L\${libdir} -lbz2
Cflags: -I\${includedir}
" > "$HOME/ffmpeg_build/lib/pkgconfig/bzip2.pc"
# yasm
cd ~/ffmpeg_sources
if [ ! -d yasm-1.3.0 ]
then
[ ! -f yasm-1.3.0.tar.gz ] && curl -L "http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz" -o yasm-1.3.0.tar.gz
tar xzf yasm-1.3.0.tar.gz
fi
cd yasm-1.3.0
if [ -f Makefile ]
then
make distclean || true
fi
./configure --prefix="$HOME/ffmpeg_build"
make $nproc
make install
# nasm
cd ~/ffmpeg_sources
if [ ! -d nasm-2.15.05 ]
then
[ ! -f nasm-2.15.05.tar.gz ] && curl -L "https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.gz" -o nasm-2.15.05.tar.gz
tar xzf nasm-2.15.05.tar.gz
fi
cd nasm-2.15.05
if [ -f Makefile ]
then
make distclean || true
fi
./autogen.sh
./configure --prefix="$HOME/ffmpeg_build"
make $nproc
make install
# x264
cd ~/ffmpeg_sources
git -C x264 pull 2> /dev/null || git clone --depth 1 https://code.videolan.org/videolan/x264.git
cd x264
if [ -f Makefile ]
then
make distclean || true
fi
./configure --prefix="$HOME/ffmpeg_build" --enable-static --enable-pic
make $nproc
make install
# libnuma (for x265)
cd ~/ffmpeg_sources
git -C numactl pull 2> /dev/null || git clone --depth 1 https://github.com/numactl/numactl.git
cd numactl
if [ -f Makefile ]
then
make distclean || true
fi
./autogen.sh
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make $nproc
make install
# x265
cd ~/ffmpeg_sources
rm -rf x265_git
git clone https://bitbucket.org/multicoreware/x265_git
cd x265_git/build/linux
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED=OFF -DSTATIC_LINK_CRT=ON -DENABLE_CLI=OFF ../../source
make $nproc
sed -i 's/-lgcc_s/-lgcc_eh/g' x265.pc
make install
# liblzma (for libxml2, ffmpeg)
cd ~/ffmpeg_sources
if [ ! -d xz-5.2.5 ]
then
[ ! -f xz-5.2.5.tar.xz ] && curl -L "https://downloads.sourceforge.net/lzmautils/xz-5.2.5.tar.xz" -o xz-5.2.5.tar.xz
tar xJf xz-5.2.5.tar.xz
fi
cd xz-5.2.5
if [ -f Makefile ]
then
make distclean || true
fi
./configure --prefix="$HOME/ffmpeg_build" --disable-shared --enable-static
make $nproc
make install
# libiconv (for libxml2)
cd ~/ffmpeg_sources
if [ ! -d libiconv-1.16 ]
then
[ ! -f libiconv-1.16.tar.gz ] && curl -L "https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz" -o libiconv-1.16.tar.gz
tar xzf libiconv-1.16.tar.gz
fi
cd libiconv-1.16
if [ -f Makefile ]
then
make distclean || true
fi
./configure --prefix="$HOME/ffmpeg_build" --disable-shared --enable-static
make $nproc
make install
# libxml2
cd ~/ffmpeg_sources
rm -rf libxml2
git clone https://github.com/GNOME/libxml2.git
mkdir -p libxml2/build
cd libxml2/build
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DBUILD_SHARED_LIBS=OFF ..
make $nproc
make install
printf '%s' "prefix=$HOME/ffmpeg_build
exec_prefix=\${prefix}