Files
q-tools/av/video-compress
2021-12-27 16:57:39 +02:00

219 lines
5.1 KiB
Bash
Executable File

#!/bin/bash
. qolop
get_toggles() {
toggles=$(
echo "$@" | \
smenu -m "Toggle with t" -n 10 -t 1 -a m:7,bu i:8 c:2 t:7/0 ct:2/0 -T ' '
# -m title
# -n rows
# -t columns
# -a styless: m title, i items, c cursor, t tagged, ct cursor on tagged
# -T multiselect
)
}
get_choice() {
choice=$(
echo "$@" | \
smenu -n 10 -t 1 -a c:10 -s "$lastchoice"
# -m title
# -n rows
# -t columns
# -a styless: m title, i items, c cursor, t tagged, ct cursor on tagged
# -T multiselect
)
}
get_resize() {
title Resize
echo "$resize"
echo ""
get_choice no-resize hd480 hd720 hd1080 640x360 356x200 manual
case $choice in
no-resize) resize="";;
manual)
echo "Type resolution WxH"
read -e size
resize="-s $size"
;;
*) resize="-s $choice";;
esac
}
get_compression() {
title Compression
echo "$compression"
echo ""
get_choice x264 copy similar YIFY
case $choice in
x264)
echo Video quality: 23 = good, 18 = insane, 30 = poor
read -e -i "$crf" crf
compression="-g 250 -c:v libx264 -crf $crf"
;;
copy) compression="-vcodec copy";;
similar) compression="-c:v libx264 -q:v 0";;
YIFY)
compression="$yify_compression"
audio=""
;;
*) return;;
esac
}
get_audio() {
title Audio
echo "$audio"
echo ""
get_choice aac copy similar no-audio
lastchoice="$choice"
case $choice in
aac)
echo Audio quality: 64 poor, 128 good
read -e -i "$abit" abit
audio="-c:a aac -ar 44100 -b:a ${abit}k -strict -2"
;;
copy) audio="-acodec copy";;
similar) audio="-q:a 0";;
no-audio) audio="-an";;
*) return;;
esac
}
title() {
_qCol Y S; echo ""; echo "$1":; _qCol z
}
helpexit() {
echo "
echo convert with ffmpeg: [preset] inputfile [outputfile]
Pressing q will stop, and you can use the command from the screen.
Presets do no ask questions:
-low crf:27 abit:64k size:hd480
-medium crf:23 abit:96k
-high crf:18 abit:128k
-yify
-custom:crf:abit:[resolution]
-y overwrite existing files
"
exit
}
# Defaults
resize=""
crf=23
abit=128
yify_compression="-c:v libx264 -crf 26 -x264-params cabac=1:ref=5:analyse=0x133:me=umh:subme=9:chroma-me=1:deadzone-inter=21:deadzone-intra=11:b-adapt=2:rc-lookahead=60:vbv-maxrate=10000:vbv-bufsize=10000:qpmax=69:bframes=5:b-adapt=2:direct=auto:crf-max=51:weightp=2:merange=24:chroma-qp-offset=-1:sync-lookahead=2:psy-rd=1.00,0.15:trellis=2:min-keyint=23:partitions=all -c:a aac -ar 44100 -b:a 128k -map 0"
preset="compress"
overwrite=""
if [[ -z "$1" ]]; then helpexit; fi
argcount=0
for (( i=1; i<=$#; i++ )); do
[[ ${!i} = "-h" ]] && helpexit
[[ ${!i} = "-low" ]] && {
FORCE=1
crf=27
abit=64
resize="-s hd480"
preset="low"
argcount=$(( argcount + 1 ))
}
[[ ${!i} = "-med"* ]] && {
FORCE=1
crf=23
abit=96
preset="med"
argcount=$(( argcount + 1 ))
}
[[ ${!i} = "-high" ]] && {
FORCE=1
crf=18
abit=128
preset="high"
argcount=$(( argcount + 1 ))
}
[[ ${!i} = "-yify" ]] && {
FORCE=1
YIFY_PRESET=1
preset="yify"
argcount=$(( argcount + 1 ))
}
[[ ${!i} = "-custom"* ]] && {
FORCE=1
options="${!i}"
option_array=( ${options//:/ } )
crf=${option_array[1]}
abit=${option_array[2]}
resize=${option_array[3]}
if [[ -n "$resize" ]]; then
resize="-s $resize"
fi
argcount=$(( argcount + 1 ))
}
[[ ${!i} = "-y" ]] && {
overwrite="-y"
argcount=$(( argcount + 1 ))
}
done
shift $argcount
compression="-g 250 -c:v libx264 -crf $crf"
audio="-c:a aac -ar 44100 -b:a ${abit}k -strict -2"
if [[ "$YIFY_PRESET" -eq 1 ]]; then
compression="$yify_compression"
audio=""
fi
inputfile="$1"
outputfile="$2"
inputname="${inputfile%.*}"
if [[ -z "$outputfile" ]]; then outputfile="${inputname}.${preset}.mp4";fi
if [[ ! -e "$inputfile" ]]; then echo "Inputfile '$inputfile' missing"; exit 1; fi
ffprobe -hide_banner "$inputfile"
while true; do
command="ffmpeg-parser -i \"$inputfile\" $resize $compression $audio -movflags faststart $overwrite \"$outputfile\""
if [[ $FORCE -eq 1 ]]; then
choice=EXECUTE
else
title Command
echo "# $command"
echo ""
lastchoice=EXECUTE
get_choice EXECUTE resize compression audio
fi
case $choice in
EXECUTE)
title Execute
echo "# $command"
echo ""
echo -n "Frames: "
ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=nb_read_packets -of csv=p=0 "$inputfile"
echo ""
setterm --linewrap off
trap "setterm --linewrap on" 0 1 9 15
eval "$command"
ec=$?
setterm --linewrap on
echo "# $command"
exit $?
;;
resize) get_resize;;
compression) get_compression;;
audio) get_audio;;
*) exit;;
esac
done