Skip to content

Instantly share code, notes, and snippets.

@s25g5d4
Last active January 8, 2020 01:33
Show Gist options
  • Save s25g5d4/8dd958e78181977490a8a52e356d1a99 to your computer and use it in GitHub Desktop.
Save s25g5d4/8dd958e78181977490a8a52e356d1a99 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# This is a rather minimal example Argbash potential
# Example taken from http://argbash.readthedocs.io/en/stable/example.html
#
# ARG_OPTIONAL_SINGLE([start],[s],[start time])
# ARG_OPTIONAL_SINGLE([to],[t],[end time])
# ARG_OPTIONAL_SINGLE([frame-rate],[r],[set frame rate (Hz value, fraction or abbreviation)],[10])
# ARG_OPTIONAL_BOOLEAN([palette],[],[build custom palette],[on])
# ARG_OPTIONAL_BOOLEAN([override],[y],[override output files without asking])
# ARG_POSITIONAL_SINGLE([input],[input file])
# ARG_POSITIONAL_SINGLE([output],[output file])
# ARG_HELP([Convert video to gif using ffmpeg.])
# ARGBASH_GO()
# needed because of Argbash --> m4_ignore([
### START OF CODE GENERATED BY Argbash v2.8.1 one line above ###
# Argbash is a bash code generator used to get arguments parsing right.
# Argbash is FREE SOFTWARE, see https://argbash.io for more info
# Generated online by https://argbash.io/generate
die()
{
local _ret=$2
test -n "$_ret" || _ret=1
test "$_PRINT_HELP" = yes && print_help >&2
echo "$1" >&2
exit ${_ret}
}
begins_with_short_option()
{
local first_option all_short_options='stryh'
first_option="${1:0:1}"
test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}
# THE DEFAULTS INITIALIZATION - POSITIONALS
_positionals=()
# THE DEFAULTS INITIALIZATION - OPTIONALS
_arg_start=
_arg_to=
_arg_frame_rate="10"
_arg_palette="on"
_arg_override="off"
print_help()
{
printf '%s\n' "Convert video to gif using ffmpeg."
printf 'Usage: %s [-s|--start <arg>] [-t|--to <arg>] [-r|--frame-rate <arg>] [--(no-)palette] [-y|--(no-)override] [-h|--help] <input> <output>\n' "$0"
printf '\t%s\n' "<input>: input file"
printf '\t%s\n' "<output>: output file"
printf '\t%s\n' "-s, --start: start time (no default)"
printf '\t%s\n' "-t, --to: end time (no default)"
printf '\t%s\n' "-r, --frame-rate: set frame rate (Hz value, fraction or abbreviation) (default: '10')"
printf '\t%s\n' "--palette, --no-palette: build custom palette (on by default)"
printf '\t%s\n' "-y, --override, --no-override: override output files without asking (off by default)"
printf '\t%s\n' "-h, --help: Prints help"
}
parse_commandline()
{
_positionals_count=0
while test $# -gt 0
do
_key="$1"
case "$_key" in
-s|--start)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_start="$2"
shift
;;
--start=*)
_arg_start="${_key##--start=}"
;;
-s*)
_arg_start="${_key##-s}"
;;
-t|--to)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_to="$2"
shift
;;
--to=*)
_arg_to="${_key##--to=}"
;;
-t*)
_arg_to="${_key##-t}"
;;
-r|--frame-rate)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_frame_rate="$2"
shift
;;
--frame-rate=*)
_arg_frame_rate="${_key##--frame-rate=}"
;;
-r*)
_arg_frame_rate="${_key##-r}"
;;
--no-palette|--palette)
_arg_palette="on"
test "${1:0:5}" = "--no-" && _arg_palette="off"
;;
-y|--no-override|--override)
_arg_override="on"
test "${1:0:5}" = "--no-" && _arg_override="off"
;;
-y*)
_arg_override="on"
_next="${_key##-y}"
if test -n "$_next" -a "$_next" != "$_key"
then
{ begins_with_short_option "$_next" && shift && set -- "-y" "-${_next}" "$@"; } || die "The short option '$_key' can't be decomposed to ${_key:0:2} and -${_key:2}, because ${_key:0:2} doesn't accept value and '-${_key:2:1}' doesn't correspond to a short option."
fi
;;
-h|--help)
print_help
exit 0
;;
-h*)
print_help
exit 0
;;
*)
_last_positional="$1"
_positionals+=("$_last_positional")
_positionals_count=$((_positionals_count + 1))
;;
esac
shift
done
}
handle_passed_args_count()
{
local _required_args_string="'input' and 'output'"
test "${_positionals_count}" -ge 2 || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require exactly 2 (namely: $_required_args_string), but got only ${_positionals_count}." 1
test "${_positionals_count}" -le 2 || _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect exactly 2 (namely: $_required_args_string), but got ${_positionals_count} (the last one was: '${_last_positional}')." 1
}
assign_positional_args()
{
local _positional_name _shift_for=$1
_positional_names="_arg_input _arg_output "
shift "$_shift_for"
for _positional_name in ${_positional_names}
do
test $# -gt 0 || break
eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
shift
done
}
parse_commandline "$@"
handle_passed_args_count
assign_positional_args 1 "${_positionals[@]}"
# OTHER STUFF GENERATED BY Argbash
### END OF CODE GENERATED BY Argbash (sortof) ### ])
# [ <-- needed because of Argbash
start=""
if [ "$_arg_start" != "" ]; then
start="-ss \"$_arg_start\""
fi
to=""
if [ "$_arg_to" != "" ]; then
to="-to \"$_arg_to\""
fi
palette=""
if [ "$_arg_palette" = "on" ]; then
palette="-filter_complex \"fps=$_arg_frame_rate,palettegen[p];[0:v][p]paletteuse\""
fi
override=""
if [ "$_arg_override" = "on" ]; then
override="-y"
fi
frame_rate="-r \"$_arg_frame_rate\""
eval "ffmpeg $start $to -i \"$_arg_input\" $palette $frame_rate -f gif $override \"$_arg_output\""
# ] <-- needed because of Argbash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment