Shellcheck fixes, quoting vars etc

This commit is contained in:
stian committed 2026-09-23 14:46:23 +02:00
1 parent 56991acdae
commit e1f6804e72
2 files changed
+72 -69

No files matched your search

+6 -6
View File
@@ -39,19 +39,19 @@ if not exist "%input%" (
set "fps=15"
set "mode=1"
set "dither=0"
set "scale=-1"
set "filetype=gif"
set "loglevel=error"
set "webp_lossy_q=75"
set "webp_lossy="
set "bayerscale="
set "colormax="
set "start_time="
set "end_time="
set "crop="
set "scale="
set "colormax="
set "bayerscale="
set "errorswitch="
set "picswitch="
set "playswitch="
set "picswitch="
:varin
:: Parse Arguments, first shift input one left
@@ -194,7 +194,7 @@ if "!fps!"=="-" (
:: Putting together filters
set "filters=fps=%fps%"
if defined crop ( set "filters=%filters%,crop=%crop%" )
set "filters=%filters%,scale=%scale%:-1:flags=lanczos"
if defined scale ( set "filters=%filters%,scale=%scale%:-1:flags=lanczos" )
:: FFplay preview
if defined playswitch (
@@ -210,7 +210,7 @@ if defined playswitch (
if not defined start_time set "start_time=0"
if not defined end_time set "end_time=3"
ffplay -v %loglevel% -i "%input%" -vf "%filters%" -an -loop 0 -ss !start_time! -t !end_time!
ffplay -v %loglevel% -ss !start_time! -t !end_time! -i "%input%" -vf "%filters%" -an -loop 0
goto :EOF
)
+66 -63
View File
@@ -11,13 +11,15 @@
### Start Main ###
main() {
# Define ANSI Colors
off=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 10)
yellow=$(tput setaf 11)
blue=$(tput setaf 12)
cyan=$(tput setaf 14)
# Define ANSI Colors if TTY output
if [[ -t 1 ]]; then
off=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 10)
yellow=$(tput setaf 11)
blue=$(tput setaf 12)
cyan=$(tput setaf 14)
fi
# Check for blank input or help commands
if [[ $# -eq 0 ]]; then print_help; exit; fi
@@ -29,7 +31,7 @@ esac
# Check if ffmpeg exists on PATH, if not exit
if ! command -v 'ffmpeg' >/dev/null 2>&1; then
echo ${red}"FFmpeg not found in PATH, please install it first"${off}; exit 1
echo "${red:-}FFmpeg not found in PATH, please install it first${off:-}"; exit 1
fi
# Assign input and output
@@ -38,7 +40,7 @@ output=$(basename "${input%.*}")
# Validate input file
if [[ ! -f "$input" ]]; then
echo ${red}"Input file not found: $input"${off}; exit 1
echo "${red:-}Input file not found: $input${off:-}"; exit 1
fi
# Set uname for later use
@@ -58,19 +60,19 @@ trap 'rm -rf "$WD"' EXIT INT TERM
fps=15
mode=1
dither=0
scale="-1"
filetype="gif"
loglevel="error"
webp_lossy_q=75
webp_lossy=""
bayerscale=""
colormax=""
start_time=""
end_time=""
crop=""
scale=""
colormax=""
bayerscale=""
errorswitch=""
picswitch=""
playswitch=""
picswitch=""
# Parse Arguments, first shift input one left
shift
@@ -94,18 +96,17 @@ while [[ $# -gt 0 ]]; do
-k) errorswitch=1;;
-p) picswitch=1;;
-y) playswitch=1;;
*) echo ${red}"Unknown option $1"${off}; exit 1;;
*) echo "${red:-}Unknown option $1${off:-}"; exit 1;;
esac
shift
done
# Validate if output file is set and not starts with a -
[[ -z $output || $output == -* ]] && { echo ${red}"Missing value for -o"${off}; exit 1; }
[[ -z $output || $output == -* ]] && { echo "${red:-}Missing value for -o${off:-}"; exit 1; }
# Validate if output is a directory; strip trailing slash and use input filename
if [[ -d "$output" ]]; then
output="${output%/}/"$(basename "${input%.*}")
echo $output
output="${output%/}/$(basename "${input%.*}")"
fi
# Validate output file extension
@@ -114,65 +115,65 @@ case $filetype in
png) output="$output.png"; filetype="apng";;
apng) output="$output.png";;
webp) output="$output.webp";;
*) echo ${red}"Invalid file type: $filetype"${off}; exit 1;;
*) echo "${red:-}Invalid file type: $filetype${off:-}"; exit 1;;
esac
# Validate Palettegen
if [[ $mode -lt 1 || $mode -gt 3 ]]; then
echo ${red}"Not a valid palettegen (-m) mode"${off}; exit 1
echo "${red:-}Not a valid palettegen (-m) mode${off:-}"; exit 1
fi
# Validate Dithering
if [[ $dither -gt 8 || $dither -lt 0 ]]; then
echo ${red}"Not a valid dither (-d) algorithm"${off}; exit 1
echo "${red:-}Not a valid dither (-d) algorithm${off:-}"; exit 1
fi
# Validate Bayerscale
if [[ -n $bayerscale ]]; then
if [[ $bayerscale -gt 5 || $bayerscale -lt 0 ]]; then
echo ${red}"Not a valid bayerscale (-b) value"${off}; exit 1
echo "${red:-}Not a valid bayerscale (-b) value${off:-}"; exit 1
fi
if [[ $dither -ne 1 ]]; then
echo ${red}"Bayerscale (-b) only works with Bayer dithering"${off}; exit 1
echo "${red:-}Bayerscale (-b) only works with Bayer dithering${off:-}"; exit 1
fi
fi
# Validate Lossy WEBP
if [[ -n $webp_lossy ]]; then
if [[ "$filetype" != "webp" ]]; then
echo ${red}"Lossy (-l) is only valid for filetype webp"${off}; exit 1
echo "${red:-}Lossy (-l) is only valid for filetype webp${off:-}"; exit 1
fi
if [[ $webp_lossy_q -gt 100 || $webp_lossy_q -lt 0 ]]; then
echo ${red}"Not a valid lossy (-l) quality value"${off}; exit 1
echo "${red:-}Not a valid lossy (-l) quality value${off:-}"; exit 1
fi
fi
# Validate Clipping
if [[ -n "$start_time" && -z "$end_time" ]]; then
echo ${red}"End time (-e) is required when Start time (-s) is specified."${off}; exit 1
elif [[ -n "$end_time" && -z "$start_time" ]]; then
echo ${red}"Start time (-s) is required when End time (-e) is specified."${off}; exit 1
elif [[ -n "$end_time" && -n "$start_time" ]]; then
trim="-ss $start_time -to $end_time"
if [[ -n $start_time && -z $end_time ]]; then
echo "${red:-}End time (-e) is required when Start time (-s) is specified.${off:-}"; exit 1
elif [[ -n $end_time && -z $start_time ]]; then
echo "${red:-}Start time (-s) is required when End time (-e) is specified.${off:-}"; exit 1
elif [[ -n $end_time && -n $start_time ]]; then
trim=(-ss "$start_time" -to "$end_time")
fi
# Validate Max Colors
if [[ -n $colormax && ( $colormax -lt 3 || $colormax -gt 256 ) ]]; then
echo ${red}"Max colors (-c) must be between 3 and 256."${off}; exit 1
echo "${red:-}Max colors (-c) must be between 3 and 256.${off:-}"; exit 1
fi
# Validate Framerate
if [[ "$fps" == "-" ]]; then
fps="source_fps"
elif [[ $fps -le 1 ]]; then
echo ${red}"Framerate (-f) must be greater than 0."${off}; exit 1
echo "${red:-}Framerate (-f) must be greater than 0.${off:-}"; exit 1
fi
# Putting together command to generate palette
palette="$WD/palette_%05d.png"
filters="fps=$fps"
[[ -n "$crop" ]] && filters+=",crop=$crop"
filters+=",scale=$scale:-1:flags=lanczos"
[[ -n $crop ]] && filters+=",crop=$crop"
[[ -n $scale ]] && filters+=",scale=$scale:-1:flags=lanczos"
# Fix paths for Cygwin before running ffmpeg/ffplay
if [[ $uname_os == *"CYGWIN"* ]]; then
@@ -185,16 +186,16 @@ fi
if [[ -n $playswitch ]]; then
# Check if ffplay exists on PATH, if not exit
if ! command -v 'ffplay' >/dev/null 2>&1; then
echo ${red}"FFplay not found in PATH, please install it first"${off}; exit 1
echo "${red:-}FFplay not found in PATH, please install it first${off:-}"; exit 1
fi
echo ${yellow}"$(ffplay -version | head -n2)"${off}
ffplay -v ${loglevel} -i "${input}" -vf "${filters}" -an -loop 0 -ss ${start_time:-0} -t ${end_time:-3}
echo "${yellow:-}$(ffplay -version | head -n2)${off:-}"
ffplay -v "${loglevel}" -ss "${start_time:-0}" -t "${end_time:-3}" -i "${input}" -vf "${filters}" -an -loop 0
exit 0
fi
# APNG muxer does not support multiple palettes, fallback to palettegen diff mode
if [[ $filetype == "apng" && $mode -eq 2 ]]; then
echo ${yellow}"APNG does not support multiple palettes - falling back to Palettegen mode 1 (diff)"${off}
echo "${yellow:-}APNG does not support multiple palettes - falling back to Palettegen mode 1 (diff)${off:-}"
mode=1
fi
@@ -203,7 +204,7 @@ case $mode in
1) encode="palettegen=stats_mode=diff";;
2) encode="palettegen=stats_mode=single";;
3) encode="palettegen";;
*) echo ${red}"Invalid palettegen (-m) mode"${off}; exit 1;;
*) echo "${red:-}Invalid palettegen (-m) mode${off:-}"; exit 1;;
esac
# Max colors
@@ -213,16 +214,16 @@ if [[ -n $colormax ]]; then
fi
# Displaying FFmpeg version string and output file
echo ${yellow}"$(ffmpeg -version | head -n2)"${off}
echo ${green}Output file:${off} $output
echo "${yellow:-}$(ffmpeg -version | head -n2)${off:-}"
echo "${green:-}Output file:${off:-} $output"
# Executing command to generate palette
echo ${green}"Generating palette..."${off}
ffmpeg -v ${loglevel} ${trim:-} -i "${input}" -vf "${filters},${encode}${mcol:-}" -y "${palette}"
echo "${green:-}Generating palette...${off:-}"
ffmpeg -v "${loglevel}" "${trim[@]}" -i "${input}" -vf "${filters},${encode}${mcol:-}" -y "${palette}"
# Checking if the palette file is in the Working Directory, if not cleaning up
if [[ ! -f "$WD/palette_00001.png" ]]; then
echo ${red}"Palette generation failed: $palette not found."${off}; exit 1
echo "${red:-}Palette generation failed: $palette not found.${off:-}"; exit 1
fi
## Setting variables to put the encode command together ##
@@ -232,7 +233,7 @@ case $mode in
1) decode="paletteuse";;
2) decode="paletteuse=new=1";;
3) decode="paletteuse";;
*) echo ${red}"Invalid palettegen (-m) mode"${off}; exit 1;;
*) echo "${red:-}Invalid palettegen (-m) mode${off:-}"; exit 1;;
esac
# Error diffusion
@@ -241,7 +242,7 @@ if [[ -n $errorswitch ]]; then
1) errordiff="=diff_mode=rectangle";;
2) errordiff=":diff_mode=rectangle";;
3) errordiff="=diff_mode=rectangle";;
*) echo ${red}"Invalid palettegen (-m) mode"${off}; exit 1;;
*) echo "${red:-}Invalid palettegen (-m) mode${off:-}"; exit 1;;
esac
fi
@@ -256,7 +257,7 @@ case $dither in
6) ditheralg="sierra3";;
7) ditheralg="burkes";;
8) ditheralg="atkinson";;
*) echo ${red}"Invalid dither (-d ) mode"${off}; exit 1;;
*) echo "${red:-}Invalid dither (-d ) mode${off:-}"; exit 1;;
esac
# Paletteuse error diffusion
@@ -276,28 +277,30 @@ fi
# WEBP pixel format and lossy quality
if [[ $filetype == "webp" && -n $webp_lossy ]]; then
type_opts="-lossless 0 -pix_fmt yuva420p -quality $webp_lossy_q"
type_opts=(-lossless 0 -pix_fmt yuva420p -quality "$webp_lossy_q")
elif [[ $filetype == "webp" && -z $webp_lossy ]]; then
type_opts="-lossless 1"
type_opts=(-lossless 1)
fi
# Executing the encoding command
echo ${green}"Encoding animation..."${off}
ffmpeg -v ${loglevel} ${trim:-} -i "${input}" -thread_queue_size 512 -i "${palette}" \
echo "${green:-}Encoding animation...${off:-}"
ffmpeg -v "${loglevel}" "${trim[@]}" -i "${input}" -thread_queue_size 512 -i "${palette}" \
-lavfi "${filters} [x]; [x][1:v] ${decode}${errordiff:-}${ditherenc}${bayer}" \
-f ${filetype} ${type_opts:-} -loop 0 -plays 0 -y "${output}"
-f "${filetype}" "${type_opts[@]}" -loop 0 -plays 0 -y "${output}"
# Checking if output file was created
if [[ ! -f "$output" ]]; then
echo ${red}"Failed to generate animation: $output not found"${off}; exit 1
echo "${red:-}Failed to generate animation: $output not found${off:-}"; exit 1
fi
# Open output file if picswitch is enabled
if [[ -n $picswitch ]]; then
xdg-open "$output"
viewer="xdg-open"
[[ $uname_os == *"CYGWIN"* ]] && viewer="cygstart"
"$viewer" "$output"
fi
echo ${green}"Done."${off}
echo "${green:-}Done.${off:-}"
}
### End Main ###
@@ -305,13 +308,13 @@ echo ${green}"Done."${off}
### Function to print the help message ###
print_help() {
cat << EOF
${green}Video to GIF/APNG/WEBP converter v6.1${off}
${blue}By MDHEXT, Nabi KaramAliZadeh, Pathduck${off}
${green:-}Video to GIF/APNG/WEBP converter v6.1${off:-}
${blue:-}By MDHEXT, Nabi KaramAliZadeh, Pathduck${off:-}
${green}Usage:${off}
${green:-}Usage:${off:-}
$(basename "$0") [input_file] [arguments]
${green}Arguments:${off}
${green:-}Arguments:${off:-}
-o Output file. Default is the same as input file, sans extension
-t Output file type: 'gif' (default), 'apng', 'png', 'webp'
-r Resize output width in pixels. Default is original input size
@@ -331,7 +334,7 @@ ${green}Arguments:${off}
-p Opens the resulting animation in the default image viewer
-v Set FFmpeg log level (default: error)
${green}Dithering Algorithms:${off}
${green:-}Dithering Algorithms:${off:-}
0: None
1: Bayer
2: Heckbert
@@ -342,18 +345,18 @@ ${green}Dithering Algorithms:${off}
7: Burkes
8: Atkinson
${green}Palettegen Modes:${off}
${green:-}Palettegen Modes:${off:-}
1: diff - only what moves affects the palette
2: single - one palette per frame
3: full - one palette for the whole animation
${green}About Bayerscale:${off}
${green:-}About Bayerscale:${off:-}
When bayer dithering is selected, the Bayer Scale option defines the
scale of the pattern (how much the crosshatch pattern is visible).
A low value means more visible pattern for less banding, a higher value
means less visible pattern at the cost of more banding.
${green}People who made this project come to fruition:${off}
${green:-}People who made this project come to fruition:${off:-}
ubitux, Nabi KaramAliZadeh, MDHEXT, Pathduck
Along with the very kind and patient people in the Batch Discord Server.
Without these people's contributions, this script would not be possible.