forked from Fmstrat/ffmkv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmkv
executable file
·1172 lines (1006 loc) · 31.4 KB
/
ffmkv
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
# Some conversion notes:
# https://trac.ffmpeg.org/wiki/Encode/H.265
# 1:44 in length, shooting for 6GB file size:
# (6144 MiB * 8192 [converts MiB to kBit]) / 6240 seconds = ~8066 kBit/s total bitrate
# 8066 - 640 kBit/s (desired audio bitrate) = 7426 kBit/s video bitrate
{
TIMESTAMP=$(date "+%Y%m%d-%H%M%S.%N")
#======== Based on https://raw.githubusercontent.com/tanhauhau/Inquirer.sh/master/dist/inquirer.sh ========
# store the current set options
OLD_SET=$-
set -e
arrow="$(echo -e '\xe2\x9d\xaf')"
checked="$(echo -e '\xe2\x97\x89')"
unchecked="$(echo -e '\xe2\x97\xaf')"
black="$(tput setaf 0)"
red="$(tput setaf 1)"
green="$(tput setaf 2)"
yellow="$(tput setaf 3)"
blue="$(tput setaf 4)"
magenta="$(tput setaf 5)"
cyan="$(tput setaf 6)"
white="$(tput setaf 7)"
bold="$(tput bold)"
normal="$(tput sgr0)"
dim=$'\e[2m'
print() {
echo "$1"
tput el
}
join() {
local IFS=$'\n'
local _join_list
eval _join_list=( '"${'${1}'[@]}"' )
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
}
function gen_env_from_options() {
local IFS=$'\n'
local _indices
local _env_names
local _checkbox_selected
eval _indices=( '"${'${1}'[@]}"' )
eval _env_names=( '"${'${2}'[@]}"' )
for i in $(gen_index ${#_env_names[@]}); do
_checkbox_selected[$i]=false
done
for i in ${_indices[@]}; do
_checkbox_selected[$i]=true
done
for i in $(gen_index ${#_env_names[@]}); do
printf "%s=%s\n" "${_env_names[$i]}" "${_checkbox_selected[$i]}"
done
}
on_default() {
true;
}
on_keypress() {
local OLD_IFS
local IFS
local key
OLD_IFS=$IFS
local on_up=${1:-on_default}
local on_down=${2:-on_default}
local on_space=${3:-on_default}
local on_enter=${4:-on_default}
local on_left=${5:-on_default}
local on_right=${6:-on_default}
local on_ascii=${7:-on_default}
local on_backspace=${8:-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') eval $on_up;;
'B') eval $on_down;;
'D') eval $on_left;;
'C') eval $on_right;;
esac
fi
;;
' ') eval $on_space ' ';;
[a-z0-9A-Z\!\#\$\&\+\,\-\.\/\;\=\?\@\[\]\^\_\{\}\~]) eval $on_ascii $key;;
$'\x7f') eval $on_backspace $key;;
'') eval $on_enter $key;;
esac
if [ $_break_keypress = true ]; then
break
fi
done
IFS=$OLD_IFS
}
gen_index() {
local k=$1
local l=0
if [ $k -gt 0 ]; then
for l in $(seq $k)
do
echo "$l-1" | bc
done
fi
}
cleanup() {
# Reset character attributes, make cursor visible, and restore
# previous screen contents (if possible).
tput sgr0
tput cnorm
stty echo
# Restore `set e` option to its orignal value
if [[ $OLD_SET =~ e ]]
then set -e
else set +e
fi
}
control_c() {
cleanup
exit $?
}
select_indices() {
local _select_list
local _select_indices
local _select_selected=()
eval _select_list=( '"${'${1}'[@]}"' )
eval _select_indices=( '"${'${2}'[@]}"' )
local _select_var_name=$3
eval $_select_var_name\=\(\)
for i in $(gen_index ${#_select_indices[@]}); do
eval $_select_var_name\+\=\(\""${_select_list[${_select_indices[$i]}]}"\"\)
done
}
on_checkbox_input_up() {
remove_checkbox_instructions
tput cub "$(tput cols)"
if [ "${_checkbox_selected[$_current_index]}" = true ]; then
printf " ${green}${checked}${normal} ${_checkbox_list[$_current_index]} ${normal}"
else
printf " ${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 "${cyan}${arrow}${green}${checked}${normal} ${_checkbox_list[$_current_index]} ${normal}"
else
printf "${cyan}${arrow}${normal}${unchecked} ${_checkbox_list[$_current_index]} ${normal}"
fi
}
on_checkbox_input_down() {
remove_checkbox_instructions
tput cub "$(tput cols)"
if [ "${_checkbox_selected[$_current_index]}" = true ]; then
printf " ${green}${checked}${normal} ${_checkbox_list[$_current_index]} ${normal}"
else
printf " ${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 "${cyan}${arrow}${green}${checked}${normal} ${_checkbox_list[$_current_index]} ${normal}"
else
printf "${cyan}${arrow}${normal}${unchecked} ${_checkbox_list[$_current_index]} ${normal}"
fi
}
on_checkbox_input_enter() {
local OLD_IFS
OLD_IFS=$IFS
_checkbox_selected_indices=()
_checkbox_selected_options=()
IFS=$'\n'
for i in $(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}+3))
printf "${cyan}$(join _checkbox_selected_options)${normal}"
tput el
tput cud1
tput cub "$(tput cols)"
tput el
_break_keypress=true
IFS=$OLD_IFS
}
on_checkbox_input_space() {
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 "${cyan}${arrow}${green}${checked}${normal} ${_checkbox_list[$_current_index]} ${normal}"
else
printf "${cyan}${arrow}${normal}${unchecked} ${_checkbox_list[$_current_index]} ${normal}"
fi
}
remove_checkbox_instructions() {
if [ $_first_keystroke = true ]; then
tput cuu $((${_current_index}+1))
tput cub "$(tput cols)"
tput cuf $((${#prompt}+3))
tput el
tput cud $((${_current_index}+1))
_first_keystroke=false
fi
}
# for vim movements
on_checkbox_input_ascii() {
local key=$1
case $key in
"j" ) on_checkbox_input_down;;
"k" ) on_checkbox_input_up;;
esac
}
_checkbox_input() {
local i
local j
prompt=$1
eval _checkbox_list=( '"${'${2}'[@]}"' )
_current_index=0
_first_keystroke=true
trap control_c SIGINT EXIT
stty -echo
tput civis
print "${normal}${green}?${normal} ${bold}${prompt}${normal} ${dim}(Press <space> to select, <enter> to finalize)${normal}"
for i in $(gen_index ${#_checkbox_list[@]}); do
_checkbox_selected[$i]=false
done
if [ -n "$3" ]; then
eval _selected_indices=( '"${'${3}'[@]}"' )
for i in ${_selected_indices[@]}; do
_checkbox_selected[$i]=true
done
fi
for i in $(gen_index ${#_checkbox_list[@]}); do
tput cub "$(tput cols)"
if [ $i = 0 ]; then
if [ "${_checkbox_selected[$i]}" = true ]; then
print "${cyan}${arrow}${green}${checked}${normal} ${_checkbox_list[$i]} ${normal}"
else
print "${cyan}${arrow}${normal}${unchecked} ${_checkbox_list[$i]} ${normal}"
fi
else
if [ "${_checkbox_selected[$i]}" = true ]; then
print " ${green}${checked}${normal} ${_checkbox_list[$i]} ${normal}"
else
print " ${unchecked} ${_checkbox_list[$i]} ${normal}"
fi
fi
tput el
done
for j in $(gen_index ${#_checkbox_list[@]}); do
tput cuu1
done
on_keypress on_checkbox_input_up on_checkbox_input_down on_checkbox_input_space on_checkbox_input_enter on_default on_default on_checkbox_input_ascii
}
checkbox_input() {
_checkbox_input "$1" "$2"
_checkbox_input_output_var_name=$3
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
cleanup
}
checkbox_input_indices() {
_checkbox_input "$1" "$2" "$3"
_checkbox_input_output_var_name=$3
eval $_checkbox_input_output_var_name\=\(\)
for i in $(gen_index ${#_checkbox_selected_indices[@]}); do
eval $_checkbox_input_output_var_name\+\=\(${_checkbox_selected_indices[$i]}\)
done
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
cleanup
}
on_list_input_up() {
remove_list_instructions
tput cub "$(tput cols)"
printf " ${_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]}"
}
on_list_input_down() {
remove_list_instructions
tput cub "$(tput cols)"
printf " ${_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]}"
}
on_list_input_enter_space() {
local OLD_IFS
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}+3))
printf "${cyan}${_list_options[$_list_selected_index]}${normal}"
tput el
tput cud1
tput cub "$(tput cols)"
tput el
_break_keypress=true
IFS=$OLD_IFS
}
remove_list_instructions() {
if [ $_first_keystroke = true ]; then
tput cuu $((${_list_selected_index}+1))
tput cub "$(tput cols)"
tput cuf $((${#prompt}+3))
tput el
tput cud $((${_list_selected_index}+1))
_first_keystroke=false
fi
}
_list_input() {
local i
local j
prompt=$1
eval _list_options=( '"${'${2}'[@]}"' )
_list_selected_index=0
_first_keystroke=true
trap control_c SIGINT EXIT
stty -echo
tput civis
print "${normal}${green}?${normal} ${bold}${prompt}${normal} ${dim}(Use arrow keys)${normal}"
for i in $(gen_index ${#_list_options[@]}); do
tput cub "$(tput cols)"
if [ $i = 0 ]; then
print "${cyan}${arrow} ${_list_options[$i]} ${normal}"
else
print " ${_list_options[$i]}"
fi
tput el
done
for j in $(gen_index ${#_list_options[@]}); do
tput cuu1
done
on_keypress on_list_input_up on_list_input_down on_list_input_enter_space on_list_input_enter_space
}
list_input() {
_list_input "$1" "$2"
local var_name=$3
eval $var_name=\'"${_list_options[$_list_selected_index]}"\'
unset _list_selected_index
unset _list_options
unset _break_keypress
unset _first_keystroke
cleanup
}
list_input_index() {
_list_input "$1" "$2"
local var_name=$3
eval $var_name=\'"$_list_selected_index"\'
unset _list_selected_index
unset _list_options
unset _break_keypress
unset _first_keystroke
cleanup
}
on_text_input_left() {
remove_regex_failed
if [ $_current_pos -gt 0 ]; then
tput cub1
_current_pos=$(($_current_pos-1))
fi
}
on_text_input_right() {
remove_regex_failed
if [ $_current_pos -lt ${#_text_input} ]; then
tput cuf1
_current_pos=$(($_current_pos+1))
fi
}
on_text_input_enter() {
remove_regex_failed
if [[ "$_text_input" =~ $_text_input_regex && "$(eval $_text_input_validator "$_text_input")" = true ]]; then
tput cub "$(tput cols)"
tput cuf $((${#_read_prompt}-19))
printf "${cyan}${_text_input}${normal}"
tput el
tput cud1
tput cub "$(tput cols)"
tput el
eval $var_name=\'"${_text_input}"\'
_break_keypress=true
else
_text_input_regex_failed=true
tput civis
tput cud1
tput cub "$(tput cols)"
tput el
printf "${red}>>${normal} $_text_input_regex_failed_msg"
tput cuu1
tput cub "$(tput cols)"
tput cuf $((${#_read_prompt}-19))
tput el
_text_input=""
_current_pos=0
tput cnorm
fi
}
on_text_input_ascii() {
remove_regex_failed
local c=$1
if [ "$c" = '' ]; then
c=' '
fi
local rest="${_text_input:$_current_pos}"
_text_input="${_text_input:0:$_current_pos}$c$rest"
_current_pos=$(($_current_pos+1))
tput civis
printf "$c$rest"
tput el
if [ ${#rest} -gt 0 ]; then
tput cub ${#rest}
fi
tput cnorm
}
on_text_input_backspace() {
remove_regex_failed
if [ $_current_pos -gt 0 ]; then
local start="${_text_input:0:$(($_current_pos-1))}"
local rest="${_text_input:$_current_pos}"
_current_pos=$(($_current_pos-1))
tput cub 1
tput el
tput sc
printf "$rest"
tput rc
_text_input="$start$rest"
fi
}
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
}
text_input_default_validator() {
echo true;
}
text_input() {
local prompt=$1
local var_name=$2
local _text_input_regex="${3:-"\.+"}"
local _text_input_regex_failed_msg=${4:-"Input validation failed"}
local _text_input_validator=${5:-text_input_default_validator}
local _read_prompt_start=$'\e[32m?\e[39m\e[1m'
local _read_prompt_end=$'\e[22m'
local _read_prompt="$( echo "$_read_prompt_start ${prompt} $_read_prompt_end")"
local _current_pos=0
local _text_input_regex_failed=false
local _text_input=""
printf "$_read_prompt"
trap control_c SIGINT EXIT
stty -echo
tput cnorm
on_keypress on_default on_default on_text_input_ascii on_text_input_enter on_text_input_left on_text_input_right on_text_input_ascii on_text_input_backspace
eval $var_name=\'"${_text_input}"\'
cleanup
}
# === Originally based on https://gist.githubusercontent.com/pruperting/397509/raw/7f9e2335189a3baa0255cd562853d382f3624201/ffmpeg-progress.sh
progress() {
sleep 10
LASTLINE=$(cat -v /tmp/ffmkv/${TIMESTAMP}/ffmpeg_${1}.log | tr '^M' '\n' | grep frame= | tail -n 1)
CFRAME=${LASTLINE##*frame=}
CFRAME=$(echo $CFRAME)
CFRAME=${CFRAME%% *}
CPERCENT=$(round 100*${CFRAME}/${FRAMES} 0)
CFPS=${LASTLINE##*fps=}
CFPS=$(echo $CFPS)
CFPS=${CFPS%% *}
TOTAL=$(round ${FRAMES}+${CFRAME}+${CPERCENT}+${CFPS} 0)
# Check to make sure all are numbers
if [ $TOTAL -eq $TOTAL 2> /dev/null ]; then
if [ "$CFPS" = "0" ]; then
echo -ne "\rffmpeg: $CFRAME of $FRAMES frames at $CFPS fps, progress: $CPERCENT"%" and ETA: error "
else
REMAINING=$(( FRAMES - CFRAME ))
SECONDS=$(round ${REMAINING}/${CFPS} 0)
H=$(round ${SECONDS}/3600 0)
M=$(round ${SECONDS}/60 0)
M=$(( M % 60 ))
S=$(( SECONDS % 60 ))
echo -ne "\rffmpeg: $CFRAME of $FRAMES frames at $CFPS fps, progress: $CPERCENT"%" and ETA: "$H"h "$M"m "$S"s "
fi
else
echo "Error, one of the values wasn't a number, trying again in 10s..."
fi
}
function convertFile() {
echo ffmpeg conversion of "$1" started on `date "+%m/%d/%y %l:%M:%S %p"`
echo ""$1" has $FRAMES frames, now converting"
trans "$1" "$2" &>> /tmp/ffmkv/${TIMESTAMP}/ffmpeg_${3}.log &
PID=$!
CFRAME=1
while [ -e /proc/$PID ]; do
progress $3
done
echo ""
echo ffmpeg stopped on `date "+%m/%d/%y %l:%M:%S %p"`
}
# =============================================================
function menuFromCmd() {
local mLOCALRESULT=$1
local mRESULT=''
read -r -a ARRAY <<< $3
list_input "$2" ARRAY mRESULT
eval $mLOCALRESULT="'$mRESULT'"
}
function menuFromArr() {
local mLOCALRESULT=$1
shift
local PROMPT=$1
shift
local ARRAY=("$@")
list_input "$PROMPT" ARRAY mRESULT
eval $mLOCALRESULT="'$mRESULT'"
}
# =============================================================
mkdir -p ~/.ffmkv/presets
function mkPreset() {
echo "
# The video stream
VIDEO=${1}
# The audio stream
AUDIO=${2}
# How to handle the video conversion
VIDEOFORMAT='${3}'
# How to handle the audio conversion
AUDIOBITRATE='${4}'
# Type of Full or Sample
TYPE=${5}
# If sample, the sample length in seconds
SAMPLELENGTH=${6}
# How to handle subtitles
SUB=${7}
# The video resolution by width. This will scale the video down to
# the specified resolution width, keeping the aspect ration. It will
# not scale the video up.
# Can be one of: existing, 3840, 1920, 1280
MAXWIDTH=${8}
# How to target the file size
CONVERTTYPE='${9}'
# How big should the target be in GB
GBSIZE=${10}
# How to handle HDR if it exists in the source
COLORTYPE='${11}'
# How many threads would you like to use? (Auto = 0)
THREADS=${12}
" > "${13}"
}
if [ -f "${HOME}/.ffmkv/presets/Up to 1080p, 192k audio, 1GB per hour CBR, SDR" ]; then
mkPreset \
"0:0" \
"0:1" \
"Transcode to x265 with a 1-pass constant bitrate" \
"192" \
"Full" \
"15" \
"Yes" \
"1920" \
"Target by gigs per hour of video" \
"1" \
"Convert to SDR color" \
"0" \
"${HOME}/.ffmkv/presets/Up to 1080p, 192k audio, 1GB per hour CBR, SDR"
mkPreset \
"0:0" \
"0:1" \
"Transcode to x265 with a 2-pass variable bitrate" \
"Stream copy" \
"Full" \
"15" \
"Yes" \
"1920" \
"Target by gigs per hour of video" \
"2" \
"Preserve HDR color" \
"0" \
"${HOME}/.ffmkv/presets/Up to 1080p, original audio, 2GB per hour VBR, HDR"
mkPreset \
"0:0" \
"0:1" \
"Transcode to x265 with a 2-pass variable bitrate" \
"Stream copy" \
"Full" \
"15" \
"Yes" \
"3840" \
"Target by gigs per hour of video" \
"4" \
"Preserve HDR color" \
"0" \
"${HOME}/.ffmkv/presets/Up to 4K, original audio, 4GB per hour VBR, HDR"
fi
function usage() {
echo "Usage: ffmkv [--preset '<preset>'] <input> <ouput>"
echo "Examples:"
echo " ffmkv input.mkv output.mkv"
echo " ffmkv --preset 'Up to 4K, original audio, 4GB per hour VBR, HDR' input.mkv output.mkv"
exit 1
}
if [ -z "$(which ffmpeg)" ]; then
echo "ffmpeg is required, exiting."
exit 1
fi
if [ -z "$(which ffprobe)" ]; then
echo "ffprobe is required, exiting."
exit 1
fi
if [ -z "$(ffmpeg -filters 2> /dev/null |grep zscale)" ]; then
echo "Your ffmpeg does not support zscale, please update from https://ffmpeg.org if you need to use HDR to SDR conversion or resolution changes."
fi
if [ "$#" != "2" ] && [ "$#" != "4" ]; then
usage
fi
if [ "$1" = "--preset" ]; then
PRESET="$2"
shift
shift
if [ "$#" != "2" ]; then
usage
fi
fi
round() {
echo $(printf %.$2f $(echo "scale=$2;(((10^$2)*$1)+0.5)/(10^$2)" | bc))
}
if [ -z "${PRESET}" ]; then
USEPRESETS=(Yes No)
menuFromArr USEPRESET "Use a preset?" "${USEPRESETS[@]}"
if [ "${USEPRESET}" = "Yes" ]; then
cd ~/.ffmkv/presets
PRESETS=(*)
cd -
menuFromArr PRESET "Which preset?" "${PRESETS[@]}"
fi
fi
if [ -n "${PRESET}" ]; then
. "${HOME}/.ffmkv/presets/${PRESET}"
fi
if [ -z "${VIDEO}" ]; then
IFS=$'\r\n' GLOBIGNORE='*' command eval 'VIDEOS=($(ffmpeg -i "$1" 2>&1 |grep Stream |grep Video |sed "s/^[ \t]*//"))'
menuFromArr VIDEO "Which video stream?" "${VIDEOS[@]}"
VIDEO=${VIDEO##*#}
VIDEO=${VIDEO%%\: *}
VIDEO=${VIDEO%%(*}
fi
CURVIDEOBITRATE=$(ffprobe -show_streams -select_streams ${VIDEO#*:} "${1}" 2> /dev/null |grep "^bit_rate=")
CURVIDEOBITRATE=${CURVIDEOBITRATE##*=}
if [ "${CURVIDEOBITRATE}" != "N\/A" ]; then
CURVIDEOBITRATE=${CURVIDEOBITRATE::-3}
if [ -n "${CURVIDEOBITRATE}" ]; then
SC="Stream copy (${CURVIDEOBITRATE}kbps)"
else
SC="Stream copy (VBR)"
fi
else
SC="Stream copy (VBR)"
CURVIDEOBITRATE=""
fi
if [ -z "${VIDEOFORMAT}" ]; then
VIDEOFORMATS=("Transcode to x265 with a 2-pass variable bitrate" \
"Transcode to x265 with a 1-pass constant bitrate" \
"Transcode to x264 with a 2-pass variable bitrate" \
"Transcode to x264 with a 1-pass constant bitrate" \
"${SC}")
menuFromArr VIDEOFORMAT "How would you like to handle video conversion?" "${VIDEOFORMATS[@]}"
fi
if [ -z "${AUDIO}" ]; then
IFS=$'\r\n' GLOBIGNORE='*' command eval 'AUDIOS=($(ffmpeg -i "$1" 2>&1 |grep Stream |grep Audio |sed "s/^[ \t]*//"))'
menuFromArr AUDIO "Which audio stream?" "${AUDIOS[@]}"
AUDIO=${AUDIO##*#}
AUDIO=${AUDIO%%\: *}
AUDIO=${AUDIO%%(*}
fi
CHANNELLAYOUT=$(ffprobe -show_streams -select_streams ${AUDIO#*:} "${1}" 2> /dev/null |grep "^channel_layout=")
if [ "$CHANNELLAYOUT" != "unknown" ]; then
CHANNELLAYOUT=${CHANNELLAYOUT##*=}
CHANNELLAYOUT=${CHANNELLAYOUT%%(*}
fi
CURAUDIOBITRATE=$(ffprobe -show_streams -select_streams ${AUDIO#*:} "${1}" 2> /dev/null |grep "^bit_rate=")
CURAUDIOBITRATE=${CURAUDIOBITRATE##*=}
if [ "${CURAUDIOBITRATE}" != "N\/A" ]; then
CURAUDIOBITRATE=${CURAUDIOBITRATE::-3}
if [ -n "${CURAUDIOBITRATE}" ]; then
SC="Stream copy (${CURAUDIOBITRATE}kbps)"
else
SC="Stream copy (VBR)"
fi
else
SC="Stream copy (VBR)"
CURAUDIOBITRATE=""
fi
if [ -z "${AUDIOBITRATE}" ]; then
AUDIOBITRATES=("128 (kbps ${CHANNELLAYOUT} channel)" "160 (kbps ${CHANNELLAYOUT} channel)" "192 (kbps ${CHANNELLAYOUT} channel)" "384 (kbps ${CHANNELLAYOUT} channel)" "${SC}")
menuFromArr AUDIOBITRATE "How would you like to handle audio conversion?" "${AUDIOBITRATES[@]}"
AUDIOBITRATE=${AUDIOBITRATE%% (*}
fi
if [ "Stream copy" = "${AUDIOBITRATE}" ]; then
AUDIOCMD="-c:a copy"
else
if [ "$CHANNELLAYOUT" != "unknown" ]; then
AUDIOCMD="-c:a aac -b:a ${AUDIOBITRATE}k -af 'channelmap=channel_layout=${CHANNELLAYOUT}'"
else
AUDIOCMD="-c:a aac -b:a ${AUDIOBITRATE}k"
fi
fi
SECONDS=$(ffmpeg -i "$1" 2>&1 |grep Duration | cut -d ' ' -f 4 | sed s/,// | sed 's@\..*@@g' | awk '{ split($1, A, ":"); split(A[3], B, "."); print 3600*A[1] + 60*A[2] + B[1] }')
FPS=( $(ffmpeg -i "$1" 2>&1 | sed -n "s/.*, \(.*\) tbr.*/\1/p") )
FRAMES=$(round $SECONDS*$FPS 0)
if [ -z "${TYPE}" ]; then
TYPES=(Sample Full)
menuFromArr TYPE "Make a sample or process the full file?" "${TYPES[@]}"
fi
if [ "${TYPE}" = "Sample" ]; then
SAMPLELENGTHS=(15 30 60 90 120)
if [ -z "${SAMPLELENGTH}" ]; then
SAMPLELENGTHS=(15 30 60 90 120)
menuFromArr SAMPLELENGTH "What sample length (in seconds)?" "${SAMPLELENGTHS[@]}"
fi
FRAMES=$(round ${FPS}*${SAMPLELENGTH} 0)
START="-ss 00:10:00"
LENGTH="-t ${SAMPLELENGTH}"
fi
if [ -z "${SUB}" ]; then
SUBS=(Yes No)
menuFromArr SUB "Copy subtitles if they exist?" "${SUBS[@]}"
fi
if [ "$SUB" = "Yes" ]; then
SUB="-map 0:s? -c:s copy"
else
SUB=""
fi
rm -rf /tmp/ffmkv/${TIMESTAMP}
mkdir -p /tmp/ffmkv/${TIMESTAMP}
if [[ "${VIDEOFORMAT}" == "Transcode"* ]]; then
WIDTH=$(ffprobe -show_streams -select_streams ${VIDEO#*:} "${1}" 2> /dev/null |grep "^width=")
WIDTH=${WIDTH##*=}
HEIGHT=$(ffprobe -show_streams -select_streams ${VIDEO#*:} "${1}" 2> /dev/null |grep "^height=")
HEIGHT=${HEIGHT##*=}
ASPECT=$(ffprobe -show_streams -select_streams ${VIDEO#*:} "${1}" 2> /dev/null |grep "^display_aspect_ratio=")
ASPECT=${ASPECT##*=}
ASPECTWIDTH=${ASPECT%:*}
ASPECTHEIGHT=${ASPECT#*:}
if [ -z "${MAXWIDTH}" ]; then
RESOLUTIONS=()
if (( WIDTH >= 7680 )); then
NEWHEIGHT=$(round 7680/${ASPECTWIDTH}*${ASPECTHEIGHT} 0)
RESOLUTIONS+=("7680x${NEWHEIGHT}")
if [ "${WIDTH}" = 7680 ] && [ "${HEIGHT}" = "${NEWHEIGHT}" ]; then
FOUNDRES=true
fi
fi
if (( WIDTH >= 3840 )); then
NEWHEIGHT=$(round 3840/${ASPECTWIDTH}*${ASPECTHEIGHT} 0)
RESOLUTIONS+=("3840x${NEWHEIGHT}")
if [ "${WIDTH}" = 3840 ] && [ "${HEIGHT}" = "${NEWHEIGHT}" ]; then
FOUNDRES=true
fi
fi
if (( WIDTH >= 2560 )); then
NEWHEIGHT=$(round 2560/${ASPECTWIDTH}*${ASPECTHEIGHT} 0)
RESOLUTIONS+=("2560x${NEWHEIGHT}")
if [ "${WIDTH}" = 2560 ] && [ "${HEIGHT}" = "${NEWHEIGHT}" ]; then
FOUNDRES=true