by jjcm · 2y ago · 0 comments
#!/bin/sh
SCALE=500
FPS=15
SCALEPROVIDED=false
for arg in "$@"
do
case $arg in
-s=*|--size=*)
SCALE="${arg#*=}"
SCALEPROVIDED=true
shift
;;
-f=*|--fps=*)
FPS="${arg#*=}"
shift
;;
esac
done
palette="/tmp/palette.png"
# Look at the width/height of the video
width=`ffprobe -v error -select_streams v:0 -show_entries stream=width -of csv=s=x:p=0 $1`
height=`ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=s=x:p=0 $1`
# Get the max
max=$(( width > height ? width : height))
# If we havent provided a scale, and the max size is lower than the default, set it to the lower value
if [ "$SCALEPROVIDED" == true ]
then
minScale=$SCALE
else
minScale=$(( SCALE < max ? SCALE : max))
fi
# Set our flags
filters="fps=$FPS,scale=$minScale:-1:flags=lanczos"
# If there's no name provided, output the same name as the input with gif as the extension
output=$2
if [ -z "$output" ]
then
output=${1%.*}.gif
fi
# Output our gif
ffmpeg -v error -i $1 -vf "$filters,palettegen=max_colors=256" -y $palette
ffmpeg -v error -i $1 -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y $output
echo created $output at ${minScale}px
Example usage is something like: gif input.mov
But you can also optionally specify framerate, size, and output name: gif -s=800 -f=22 input.mov my-800px-22fps-gif.gif ffmpeg -i input.mov -f yuv4mpegpipe | gifski --width=500 -o out.gif -