# Generate an optimized palette.
#
ffmpeg -i $INPUT.mp4 \
       -filter_complex "[0:v] palettegen" $PALETTE.png
 
# Convert the MP4 to GIF (using the palette above).
#
ffmpeg -i $INPUT.mp4 -i $PALETTE.png \
       -filter_complex "[0:v][1:v] paletteuse" \
	                    $OUTPUT.gif