Skip to content

Instantly share code, notes, and snippets.

@montehurd
Last active October 26, 2020 23:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save montehurd/d6647cf5a5c1859e0716 to your computer and use it in GitHub Desktop.
Save montehurd/d6647cf5a5c1859e0716 to your computer and use it in GitHub Desktop.
Simple optimized .mov to .gif conversion from macOS command line using only ffmpeg and gifsicle - with instructions for automatic conversion any time a .mov file is dropped into a special folder.
ffmpeg -i in.mov -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 > out.gif
@montehurd
Copy link
Author

montehurd commented Dec 11, 2015

I primarily use this to convert short screen recordings (via the macOS command-shift-5 keyboard shortcut) to more share-able gifs.

Once the installation instructions below are completed, converting a screen recording (or other) .mov file to a gif is as simple as dropping it in a special folder - a couple seconds later, a gif version of the file appears in the folder:

folder action mov to gif

Benefits

  • Fairly simple one-time setup (takes about 5 minutes)
  • Easy to use
  • Free

free 2
free 1

Installation:

Then use Homebrew to install ffmpeg and gifsicle libraries:

  • Hit command-space to open Spotlight search

  • Type terminal and open the terminal app

  • Install ffmpeg by pasting the following command into terminal, then press enter:

    brew install ffmpeg

  • Install gifsicle by pasting the following command into terminal, then press enter:

    brew install gifsicle

Usage:

Manual:

Once installation steps are complete, a command like the following can be used to convert a .mov file to a .gif:

ffmpeg -i in.mov -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 > out.gif

Automatic (run when mov files placed in a folder):

Typing such a command can be tedious (plus you would have tweak it for the specific location of each .mov file to be converted), so instead we can configure macOS to do this automatically any time a .mov file is dropped into a folder. macOS's Automator provides Folder Actions to allow scripts to be triggered when files are placed in a folder.

Here's a shell script triggering our mov-to-gif command once for every file we drop in the folder:

for f
do
    if [[ ($f == *.mov || $f == *.mp4 ) ]]; then
        /usr/local/bin/ffmpeg -i "$f" -pix_fmt rgb24 -r 10 -f gif - | /usr/local/bin/gifsicle --optimize=3 > "$f".gif
    fi
done

Here's sticking that script into an Automator "Folder Action" workflow so it can be triggered when dropping files in a folder:

make mov to gif folder action mov

Technical notes:

ffmpeg -i in.mov -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 > out.gif

  • the ffmpeg command is used to simplify the color palate and reduce the frame rate of the .mov file
  • -r controls the frame rate - here we use 10 frames per second, which is high enough to make it easy to see what's going on, but low enough to help keep the file size down
  • the gifsicle command is used to convert the resulting .mov file to an optimized .gif
  • --optimize=3 helps keep the gif file size down even more without compromising quality too much

You can tweak these settings (or add others) - see:
ffmpeg settings docs
gifsicle settings docs

Thoughts:

Aside from the gif-specific bits, you may find this useful as an example of how to trigger a script to run any time you drop a file in a folder.

I suppose it's a fair example of how to pass the result of one command (ffmpeg via its stdout) to the input of a second command (gifsicle via its stdin) too.

@pamela-drouin
Copy link

Super helpful tool!

@montehurd
Copy link
Author

Thanks!! So was your tip about using command-shift-5 to quickly record the ".mov" files!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment