update efg to use print_opt_map

This commit is contained in:
2026-05-31 16:40:27 -05:00
parent 4e1aac1284
commit b1afb83db4
3 changed files with 124 additions and 89 deletions
+30
View File
@@ -600,6 +600,18 @@ parse_opt_map() {
local longestOpt=0
for opt in "${optMap[@]}"; do
read -r short long desc <<<"${opt}"
# check for duplicate/overwrites
if line_contains "${shortOpt[*]}" "${short}"; then
echo_fail "${short} already defined"
return 1
fi
if line_contains "${longOpt[*]}" "${long}"; then
echo_fail "${long} already defined"
return 1
fi
# --flag -> flag
index="${long//--/}"
optIndex+=("${index}")
@@ -622,3 +634,21 @@ parse_opt_map() {
descOpt["${ind}"]="${padding}${tab}${descOpt[${ind}]}"
done
}
print_opt_map() {
local optionMap=("$@")
local OPT_INDS
declare -A SHORT_OPTS LONG_OPTS DESC_OPTS
parse_opt_map \
OPT_INDS \
SHORT_OPTS \
LONG_OPTS \
DESC_OPTS \
"${optionMap[@]}" || return 1
echo -e "\nOPTIONS:"
for ind in "${OPT_INDS[@]}"; do
echo -e " ${SHORT_OPTS[${ind}]}, ${LONG_OPTS[${ind}]}${DESC_OPTS[${ind}]}"
done
echo
}
+80 -61
View File
@@ -2,99 +2,114 @@
efg_usage() {
echo "efg -i input [options]"
echo -e "\t[-P NUM] set preset (default: ${PRESET})"
echo -e "\t[-l NUM] low value (default: ${LOW})"
echo -e "\t[-s NUM] step value (default: ${STEP})"
echo -e "\t[-h NUM] high value (default: ${HIGH})"
echo -e "\t[-p] plot bitrates using gnuplot"
echo -e "\n\t[-I] system install at ${EFG_INSTALL_PATH}"
echo -e "\t[-U] uninstall from ${EFG_INSTALL_PATH}"
print_opt_map "${EFG_OPT_MAP[@]}" || return 1
return 0
}
set_efg_opts() {
local opts='P:pl:s:h:i:IU'
local numOpts=${#opts}
# default values
unset INPUT
PRESET=10
LOW=0
STEP=1
HIGH=30
PLOT=false
ENCODE_SEGMENTS=5
EFG_INSTALL_PATH='/usr/local/bin/efg'
local EFG_OPT_MAP=(
"-i --input input file"
"-P --preset set preset (default: ${PRESET})"
"-l --low set low end value for grain range (default: ${LOW})"
"-s --step set step value for grain range (default: ${STEP})"
"-h --high set high end value for grain range (default: ${HIGH})"
"-n --segments number of segments to analyze from input (default: ${ENCODE_SEGMENTS})"
"-p --plot plot bitrates using gnuplot"
"-I --install system install at ${EFG_INSTALL_PATH}"
"-U --uninstall uninstall from ${EFG_INSTALL_PATH}"
)
# only using -I or -U
local minOpt=1
# using all
local maxOpt=${numOpts}
test $# -lt ${minOpt} && efg_usage && return 1
test $# -gt ${maxOpt} && efg_usage && return 1
local OPTARG OPTIND
while getopts "${opts}" flag; do
case "${flag}" in
P)
if ! is_positive_integer "${OPTARG}"; then
local arg value
while [[ $# -gt 0 ]]; do
arg="$1"
value="${2:-}"
case "${arg}" in
-i | --input)
INPUT="${value}"
shift
;;
-P | --preset)
if ! is_positive_integer "${value}"; then
efg_usage
return 1
fi
PRESET="${OPTARG}"
PRESET="${value}"
shift
;;
I)
-l | --low)
if ! is_positive_integer "${value}"; then
efg_usage
return 1
fi
LOW="${value}"
shift
;;
-s | --step)
if ! is_positive_integer "${value}"; then
efg_usage
return 1
fi
STEP="${value}"
shift
;;
-h | --high)
if ! is_positive_integer "${value}"; then
efg_usage
return 1
fi
HIGH="${value}"
shift
;;
-n | --num-segments)
if ! is_positive_integer "${value}"; then
efg_usage
return 1
fi
ENCODE_SEGMENTS="${value}"
shift
;;
-p | --plot)
missing_cmd gnuplot && return 1
PLOT=true
;;
-I | --install)
echo_warn "attempting install"
sudo ln -sf "${SCRIPT_DIR}/efg.sh" \
"${EFG_INSTALL_PATH}" || return 1
echo_pass "succesfull install"
return ${FUNC_EXIT_SUCCESS}
return "${FUNC_EXIT_SUCCESS}"
;;
U)
-U | --uninstall)
echo_warn "attempting uninstall"
sudo rm "${EFG_INSTALL_PATH}" || return 1
echo_pass "succesfull uninstall"
return ${FUNC_EXIT_SUCCESS}
;;
i)
if [[ $# -lt 2 ]]; then
echo_fail "wrong arguments given"
efg_usage
return 1
fi
INPUT="${OPTARG}"
;;
p)
missing_cmd gnuplot && return 1
PLOT=true
;;
l)
if ! is_positive_integer "${OPTARG}"; then
efg_usage
return 1
fi
LOW="${OPTARG}"
;;
s)
if ! is_positive_integer "${OPTARG}"; then
efg_usage
return 1
fi
STEP="${OPTARG}"
;;
h)
if ! is_positive_integer "${OPTARG}"; then
efg_usage
return 1
fi
HIGH="${OPTARG}"
return "${FUNC_EXIT_SUCCESS}"
;;
*)
echo "wrong flags given"
echo_fail "unsupported option: [${arg}]"
efg_usage
return 1
;;
esac
shift
done
if [[ ! -f ${INPUT} ]]; then
echo "${INPUT} does not exist"
# validate input
if [[ -z ${INPUT} || ! -f ${INPUT} ]]; then
echo_fail "input undefined or does not exist"
efg_usage
return 1
fi
@@ -108,7 +123,7 @@ set_efg_opts() {
EFG_DIR+="-${sanitizedInput}"
echo_info "estimating film grain for ${INPUT}"
echo_info "range: $LOW-$HIGH with $STEP step increments"
echo_info "range: ${LOW}-${HIGH} with ${STEP} step increments"
}
efg_segment() {
@@ -255,9 +270,13 @@ FB_FUNC_NAMES+=('efg')
# shellcheck disable=SC2034
FB_FUNC_DESCS['efg']='estimate the film grain of a given file'
efg() {
# localize variables used by child functions
local PRESET LOW STEP HIGH PLOT \
EFG_INSTALL_PATH EFG_DIR ENCODE_SEGMENTS \
GRAIN_LOG
EFG_DIR="${TMP_DIR}/efg"
# encode N highest-bitrate segments
ENCODE_SEGMENTS=5
set_efg_opts "$@"
local ret=$?
+14 -28
View File
@@ -324,21 +324,11 @@ setup_pgs_mkv() {
}
encode_usage() {
local OPT_INDS
declare -A SHORT_OPTS LONG_OPTS DESC_OPTS
parse_opt_map \
OPT_INDS \
SHORT_OPTS \
LONG_OPTS \
DESC_OPTS \
"${ENCODE_OPT_MAP[@]}"
echo "encode -i input [options] [output]"
echo -e "\nOPTIONS:"
for ind in "${OPT_INDS[@]}"; do
echo -e " ${SHORT_OPTS[${ind}]}, ${LONG_OPTS[${ind}]}${DESC_OPTS[${ind}]}"
done
print_opt_map "${ENCODE_OPT_MAP[@]}" || return 1
echo -e "\n [output] output filename (default: \${PWD}/av1-input-file-name.mkv)\n"
return 0
}
encode_update() {
@@ -358,14 +348,14 @@ set_encode_opts() {
local ENCODE_OPT_MAP=(
"-i --input input file"
"-P --preset set preset (default ${PRESET})"
"-C --crf set CRF (default ${CRF})"
"-g --grain set film grain (default disabled)"
"-P --preset set preset (default: ${PRESET})"
"-C --crf set CRF (default: ${CRF})"
"-g --grain set film grain (default: disabled)"
"-p --print print the script instead of executing it"
"-c --crop use crop detect to auto-crop"
"-d --dv enable dolby vision (default ${DV_TOGGLE})"
"-d --dv enable dolby vision"
"-v --version print version info"
"-s --same-container use same container as input, default is mkv"
"-s --same-container use same container as input (default: mkv)"
"-u --update update script (git pull ffmpeg-builder)"
"-I --install system install at ${ENCODE_INSTALL_PATH}"
"-U --uninstall uninstall from ${ENCODE_INSTALL_PATH}"
@@ -382,7 +372,7 @@ set_encode_opts() {
case "${arg}" in
-i | --input)
INPUT="${value}"
shift 2
shift
;;
-P | --preset)
if ! is_positive_integer "${value}"; then
@@ -390,7 +380,7 @@ set_encode_opts() {
return 1
fi
PRESET="${value}"
shift 2
shift
;;
-C | --crf)
if ! is_positive_integer "${value}" || test "${value}" -gt 63; then
@@ -399,7 +389,7 @@ set_encode_opts() {
return 1
fi
CRF="${value}"
shift 2
shift
;;
-g | --grain)
if ! is_positive_integer "${value}"; then
@@ -407,19 +397,16 @@ set_encode_opts() {
return 1
fi
GRAIN="film-grain=${value}:film-grain-denoise=1:adaptive-film-grain=1:"
shift 2
shift
;;
-c | --crop)
CROP=true
shift
;;
-p | --print)
PRINT_OUT=true
shift
;;
-d | --dv)
DV_TOGGLE=true
shift
;;
-v | --version)
get_encode_versions print || return 1
@@ -427,7 +414,6 @@ set_encode_opts() {
;;
-s | --same-container)
SAME_CONTAINER=true
shift
;;
-u | --update)
encode_update || return 1
@@ -449,14 +435,14 @@ set_encode_opts() {
*)
# OUTPUT will be the last (optional) arg
if [[ $# -ne 1 ]]; then
echo_fail "wrong flags given"
echo_fail "unsupported option: [${arg}]"
encode_usage
return 1
fi
OUTPUT="${arg}"
shift
;;
esac
shift
done
# validate input