While creating my previous post on how to add an input accessory view to the iOS keyboard, I wanted to include gifs to better illustrate how to do certain steps. If a picture is worth a thousand words, a movie must be worth even more, right?

The problem was, I never made a gif before, but knew that it would be a straightforward process. I began searching how to convert a mov file, the default output format of Quicktime’s screen recording, to a gif. I found this post, which seemed to answer my question, but I liked JasonM23‘s solution, which he included in his comment of the post. I have modified his solution slightly, and would love to share it with you!

Dependencies

There are two dependencies that are required to perform this conversion, ffmpeg and imagemagick. These can be installed with homebrew.

$brew install ffmpeg$ brew install imagemagick


The script

After the dependencies are installed, copy and paste the following into Terminal.

movtogif(){
ffmpeg -i "$1" -r 20 -f image2pipe -vcodec ppm - | convert -delay 5 -layers Optimize -loop 0 - "$2"
}


This will create a temporary alias, movtogif, that you can use. After the Terminal window is closed, the alias won’t be able to be used again.

To get around this limitation, make movtogif a permanent alias. Add a function with the name __movtogif to your .bash_profile file, located in your home directory (~/.bash_profile), and make an alias that references __movtogif. This is necessary because bash aliases cannot accept parameters directly (Stack Overflow).

$cat ~/.bash_profile ... function __movtogif { if [ -z$5 ]; then
echo -e "Invalid arguments.\n\tUsage: movtogif [input] [output] [frame rate] [delay] [scale]";
return;
fi

ffmpeg -i $1 -vf scale=$5 -r $3 -f image2pipe -vcodec ppm - | convert -delay$4 -layers Optimize -loop 0 - $2 } alias movtogif=__movtogif  Here’s a one-liner that you can copy and paste to append the above to .bash_profile. $ echo -e "function __movtogif {\n\tif [ -z \$5 ]; then\n\t\techo -e \"Invalid arguments.\\\n\\\tUsage: movtogif [input] [output] [frame rate] [delay] [scale]\";\n\t\treturn;\n\tfi\n\n\tffmpeg -i "\$1" -vf scale="\$5" -r "\$3" -f image2pipe -vcodec ppm - |\n\tconvert -delay "\$4" -layers Optimize -loop 0 - "\$2"\n}\n\nalias movtogif=__movtogif" >> ~/.bash_profile


Next, source bash_profile so your terminal reads our new functions.

$source ~/.bash_profile  Usage $ movtogif [input] [output] [frame rate] [delay] [scale]


Parameters

• input - The name of the file to be converted (appended with the .mov extension)
• output - The name of the gif (appended with the .gif extension)
• frame rate (suggested: 20) - The gif’s frame rate (in fps)
• delay (suggested: 5) - The delay between frames
• scale (suggested: original scale; iw*ih) - The size of the gif. Can be one of the following (Source):
• A specific width and height (e.g. 320:240)
• A specific width, keeping the aspect ratio constant (e.g. 20:-1)
• A specific height, keeping the aspect ratio constant (e.g. -1:240)
• A width and height using the width and height constants, iw and ih, respectively, to accomplish things such as doubling the width of the image (e.g. iw*2:ih)