Skip to content

Instantly share code, notes, and snippets.

@vincurekf
Created February 26, 2018 11:10
Show Gist options
  • Select an option

  • Save vincurekf/0377ce34c32b8de34445fa9d06529ba7 to your computer and use it in GitHub Desktop.

Select an option

Save vincurekf/0377ce34c32b8de34445fa9d06529ba7 to your computer and use it in GitHub Desktop.
This script takes videofile and converts it to high quality GIF, using ffmpeg.
#!/bin/bash
# Usage:
# hdgif -f='/path/to/video.mp4' -w=720 -q=2 -fps=22 -a=1
# -f = input video file
# -w = width of the final gif
# -q = quality, sets the bayer scale, lower number mean higher quality
# -fps = frames per second in the final gif
# -a = convert automatically, uses default options if not set and converts without asking
# -d = destination of where to save the final gif
show_bold() {
echo -e "\033[1;34m$@\033[0m"
}
# declaring a couple of associative arrays
declare -A arguments=();
declare -A variables=();
# declaring an index integer
declare -i index=1;
# any variables you want to use here
# on the left left side is argument label or key (entered at the command line along with it's value)
# on the right side is the variable name the value of these arguments should be mapped to.
# (the examples above show how these are being passed into this script)
variables["-f"]="file";
variables["--file"]="file";
variables["-w"]="width";
variables["--width"]="width";
variables["-q"]="quality";
variables["--quality"]="quality";
#variables["-b"]="batch";
#variables["--batch"]="batch";
variables["-fps"]="fps";
variables["--fps"]="fps";
variables["-a"]="auto";
variables["--auto"]="auto";
variables["-d"]="destination";
variables["--dest"]="destination";
# $@ here represents all arguments passed in
for i in "$@"
do
arguments[$index]=$i;
prev_index="$(expr $index - 1)";
# this if block does something akin to "where $i contains ="
# "%=*" here strips out everything from the = to the end of the argument leaving only the label
if [[ $i == *"="* ]]
then argument_label=${i%=*}
else argument_label=${arguments[$prev_index]}
fi
# this if block only evaluates to true if the argument label exists in the variables array
if [ -n ${variables[$argument_label]} ]
then
# dynamically creating variables names using declare
# "#$argument_label=" here strips out the label leaving only the value
if [[ $i == *"="* ]]
then declare ${variables[$argument_label]}=${i#$argument_label=}
else declare ${variables[$argument_label]}=${arguments[$index]}
fi
fi
index=index+1;
done;
if [ -z "$file" ]; then
show_bold "##"
show_bold "## Input file not set, please provide basic setting."
show_bold "## Usage example:"
show_bold "## $ hdgif /some/path/to/file.mp4"
show_bold "##"
exit
fi
if [ -z $destination ]; then
if [ -z $auto ]; then
show_bold "##"
show_bold "## Destination (./ by default):"
read destination
if [ -z $destination ]; then
declare destination=./
fi
else
if [ -z $destination ]; then
declare destination=./
fi
fi
fi
if [ -z $fps ]; then
if [ -z $auto ]; then
show_bold "##"
show_bold "## FPS (22 by default):"
read fps
if [ -z $fps ]; then
declare fps=22
fi
else
if [ -z $fps ]; then
declare fps=22
fi
fi
fi
if [ -z $quality ]; then
if [ -z $auto ]; then
show_bold "##"
show_bold "## Bayer scale (0-5, 3 by default):"
read quality
if [ -z $quality ]; then
declare quality=3
fi
else
if [ -z $quality ]; then
declare quality=3
fi
fi
fi
if [ -z $width ]; then
if [ -z $auto ]; then
show_bold "##"
show_bold "## Width (720 by default):"
read width
if [ -z $width ]; then
declare width=720
fi
else
if [ -z $width ]; then
declare width=720
fi
fi
fi
dir=$(dirname "${file}")
parentdir=$(dirname "$dir")
filename=$(basename "${file}")
extension="${filename##*.}"
filename="${filename%.*}"
number=1
while [ -f $parentdir/gif/$filename"["$number-w$width-q$quality"].gif" ]
do (( number++ ))
done
outputpath=$parentdir/gif/$filename"["$number-w$width-q$quality"].gif"
touch $parentdir/gif/ || mkdir $parentdir/gif/
touch $parentdir/palette/ || mkdir $parentdir/palette/
echo
show_bold "##"
show_bold "## Convert video -> gif"
show_bold "## Path: $outputpath"
show_bold "## FPS: $fps"
show_bold "## Quality: $quality"
show_bold "## Width: $width"
show_bold "##"
do_convert() {
# Filters and palette file
palette="$parentdir/palette/$filename-palette.png"
filters="fps=$fps,scale=$width:-1:flags=lanczos"
# Generate palette
# -v warning
ffmpeg -i $file -pix_fmt yuvj422p -vf "$filters,palettegen=stats_mode=diff" -y $palette
# MP4 to GIF
# -v warning
ffmpeg -i $file -pix_fmt yuvj422p -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse=dither=bayer:bayer_scale=$quality" -y $outputpath
}
if [ -z $auto ]; then
show_bold "##"
show_bold "## Start Convert? [y/n]:"
read startconvert
if [ "$startconvert" == "y" ]; then
do_convert
fi
else
do_convert
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment