jonrh Dashboard Consulting

Using FFmpeg to convert video to gif on macOS

November 04, 2016

I use Nix for package management on macOS. To install FFmpeg I ran the following command in a terminal: nix-env -i ffmpeg-3.1.4. If you prefer Homebrew I believe the equivalent would be brew install ffmpeg.

Below is the command explained with comments. It converts an input video file (input_video.mov) to a output.gif.

# Change the file names that suits you
inputfile=input_video.mov && outputfile=output.gif && \
# Change FPS and width to manage quality and size
fps=10 && scale=200 && \
# Create a temporary palette image to narrow down colour spectrum
ffmpeg -y -i $inputfile -vf fps=$fps,scale=$scale:-1:flags=lanczos,palettegen palette.png && \
# Convert the video to a .gif
ffmpeg -i $inputfile -i palette.png -filter_complex "fps=$fps,scale=$scale:-1:flags=lanczos[x];[x][1:v]paletteuse" $outputfile && \
# Remove temporary palette file and variable names
rm palette.png && inputfile="" && outputfile="" && fps="" && scale=""

To run this, copy the line below to a text editor and change the file names, FPS and width to suit your needs. Open a terminal and move into the folder where your source video file is. Paste your modified command into the terminal and hit enter:

inputfile=input_video.mov && outputfile=output.gif && fps=10 && scale=200 && ffmpeg -y -i $inputfile -vf fps=$fps,scale=$scale:-1:flags=lanczos,palettegen palette.png && ffmpeg -i $inputfile -i palette.png -filter_complex "fps=$fps,scale=$scale:-1:flags=lanczos[x];[x][1:v]paletteuse" $outputfile && rm palette.png && inputfile="" && outputfile="" && fps="" && scale=""

This is essentially LordNeckbeard’s answer on SuperUser merged into a single convenience copy/paste command that doesn’t leave a temporary file (palette.png) behind.


Jón Rúnar HelgasonJón Rúnar Helgason