Skip to content

Instantly share code, notes, and snippets.

@montehurd
Last active June 18, 2024 20:10
Show Gist options
  • 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 - with instructions for automatic conversion any time a .mov file is dropped into a special folder.
ffmpeg -i in.mov -pix_fmt rgb8 -r 10 -f gif 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:

  • 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

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 rgb8 -r 10 -f gif 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
        /opt/homebrew/bin/ffmpeg -i "$f" -pix_fmt rgb8 -r 10 -f gif "${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 rgb8 -r 10 -f gif 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

You can tweak these settings (or add others) - see:
ffmpeg 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.

Alternatives:

Optimize for speed

If you want faster conversions, for example, if you're regularly dropping large numbers of files into your conversion folder at once, you can also install parallel (via brew install parallel) and use a slightly different script:

filtered_files=$(printf "%s\0" "$@" | grep -Ez '\.(mov|mp4)$')
if [ -z "$filtered_files" ]; then
    exit 0
fi
printf "%s" "$filtered_files" | /opt/homebrew/bin/parallel -0 -j+0 '/opt/homebrew/bin/ffmpeg -i {} -pix_fmt rgb8 -r 10 -f gif {.}.gif'

Optimize for gif file size

If you want gifs as small as possible, at the cost of slower conversions, you can install both parallel and gifsicle (via brew install parallel gifsicle) and use this script:

filtered_files=$(printf "%s\0" "$@" | grep -Ez '\.(mov|mp4)$')
if [ -z "$filtered_files" ]; then
    exit 0
fi
printf "%s" "$filtered_files" | /opt/homebrew/bin/parallel -0 -j+0 '/opt/homebrew/bin/ffmpeg -i {} -pix_fmt rgb8 -r 10 -f gif - | /opt/homebrew/bin/gifsicle --optimize=3 > {}.gif'

Miscellaneous:

If you later want to edit the shell script that gets triggered when you drop files in your folder, you can right-click on your folder and select Folder Actions Setup then select Edit Workflow for the conversion folder action

@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