Skip to content

Instantly share code, notes, and snippets.

@WeZZard
Last active April 30, 2023 03:17
Show Gist options
  • Save WeZZard/d5093438ab22ab2bbd988718cf5ba211 to your computer and use it in GitHub Desktop.
Save WeZZard/d5093438ab22ab2bbd988718cf5ba211 to your computer and use it in GitHub Desktop.
Convert video to gif with given resolution
#!/bin/sh
NAME=""
SCALE=""
PRESET="blog"
function parse_arg_input_file() {
NAME="$1"
return 0;
}
function parse_arg_resolution() {
SCALE="$1"
return 0;
}
function parse_arg_preset() {
ARG=$1;
if [[ $ARG == "wmp" ]]; then
PRESET="wmp"
return 0;
fi
if [[ $ARG == "blog" ]]; then
PRESET="blog"
return 0;
fi
echo "Unrecognized preset: $ARG.";
return 1;
}
function arg_parse() {
ARG=$1;
if [[ "$CONSECUTIVE_ARG_PARSE_KIND" == "--input-file" ]]; then
CONSECUTIVE_ARG_PARSE_KIND=None
parse_arg_input_file "$ARG";
return $?;
fi
if [[ "$CONSECUTIVE_ARG_PARSE_KIND" == "--resolution" ]]; then
CONSECUTIVE_ARG_PARSE_KIND=None
parse_arg_resolution "$ARG";
return $?;
fi
if [[ "$CONSECUTIVE_ARG_PARSE_KIND" == "--preset" ]]; then
CONSECUTIVE_ARG_PARSE_KIND=None
parse_arg_preset "$ARG";
return $?;
fi
if [[ "$CONSECUTIVE_ARG_PARSE_KIND" -ne "None" ]]; then
echo "Invalid consecutive argument: $CONSECUTIVE_ARG_PARSE_KIND. There must be something behind it."
return 1;
fi
if [[ $ARG == "--input-file" ]]; then
CONSECUTIVE_ARG_PARSE_KIND="$ARG";
return 0;
fi
if [[ $ARG == "--resolution" ]]; then
CONSECUTIVE_ARG_PARSE_KIND="$ARG";
return 0;
fi
if [[ $ARG == "--preset" ]]; then
CONSECUTIVE_ARG_PARSE_KIND="$ARG";
return 0;
fi
echo "Unrecognized argument: $ARG.";
return 1
}
for EACH_ARG in "$@"; do
arg_parse "$EACH_ARG"
RET_VAL=$?
if [[ $RET_VAL -ne 0 ]]; then
exit 1
fi
done
FILENAME=$(basename -- "$NAME")
EXT_NAME="${FILENAME##*.}"
FILENAME="${FILENAME%.*}"
FRAME_RATE=24
DELAY=2.5
OPTIMIZE_ARGS=""
BUILD_DIR="$FILENAME.d"
case $PRESET in
"blog")
FRAME_RATE=24
DELAY=2.5
;;
"wmp")
FRAME_RATE=10
DELAY=6
OPTIMIZE_ARGS="-fuzz %3"
;;
*)
echo "Unkown product category $CATEGORY";
exit 1;
;;
esac
mkdir -p "$BUILD_DIR"
##########################################################################
# DOWNSCALE
##########################################################################
echo "ffmpeg -i \"$NAME\" -s $SCALE \"$BUILD_DIR/$FILENAME.mp4\""
ffmpeg -i "$NAME" -s $SCALE "$BUILD_DIR/$FILENAME.mp4"
##########################################################################
# CREATE PNG
##########################################################################
echo "ffmpeg -i \"$BUILD_DIR/$FILENAME.mp4\" -r $FRAME_RATE -pix_fmt rgb24 \"$BUILD_DIR/ffout%3d.png\""
ffmpeg -i "$BUILD_DIR/$FILENAME.mp4" -r $FRAME_RATE -pix_fmt rgb24 "$BUILD_DIR/ffout%3d.png"
##########################################################################
# COMPOSITION
##########################################################################
echo "convert -delay $DELAY -loop 0 \"$BUILD_DIR/ffout*.png\" \"$BUILD_DIR/$FILENAME.gif\""
convert -delay $DELAY -loop 0 "$BUILD_DIR/ffout*.png" "$BUILD_DIR/$FILENAME.gif"
##########################################################################
# OPTIMIZE
##########################################################################
# TOOD: -fuzz %n, specifies that colors within a certain tolerance can be considered as the same color.
echo "convert -layers Optimize $OPTIMIZE_ARGS \"$BUILD_DIR/$FILENAME.gif\" \"$BUILD_DIR/$FILENAME-optimized.gif\""
convert -layers Optimize $OPTIMIZE_ARGS "$BUILD_DIR/$FILENAME.gif" "$BUILD_DIR/$FILENAME-optimized.gif"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment