Convert a video file to a GIF using a MacOS Automator task

While I was writing this post I had a step where I wanted to show a GIF of the operation. MacOS’ built-in screen capture was able to record a section of the screen for me as a Quicktime movie (.mov). For obvious reasons a GIF would be a lot simpler for a blog post, but there was no straight forward mechanism to do what I needed.

A quick Google returned this Medium article describing a couple of utilities that can get the job done. It’s a few extra steps that I didn’t care to do so I wrapped them in an Automator script task and integrated it with Finder for some sweet right-click action.

What you’ll need

  • Homebrew
  • ffmpeg (via Homebrew: brew install ffmpeg)
  • gifsicle (via Homebrew: brew install gifsicle)
  1. Launch Automator - use Command + Space to search for Automator search-automator
  2. You’ll be presented with a dialog to open an existing Automator file. Click the New Document button. new-document
  3. Select Quick Action for the document type and click Choose. quick-action
  4. Welcome to the Automator canvas! In the “Library” on the left, search for Shell and drag the Run Shell Script item to the canvas on the right hand side. shell-drag
  5. Above the canvas, you’ll see a handful of settings you can configure. Change them to the following:
    1. Workflow receives current: movie files in Finder.app
    2. Image: Slide show action-settings
  6. In the Script task, select your preferred shell environment. I stuck with my usual zsh. You’ll also want to set the “Pass input” value to as arguments. script-settings
  7. Paste the following block into the contents of the script task:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    export PATH=/usr/local/bin:$PATH
    filename="$(basename -- $1)"
    dir="$(dirname -- $1)"
    mkdir "/tmp/pngs"
    mkdir "/tmp/gifs"
    ffmpeg -i $1 -r 10 /tmp/pngs/out%04d.png
    sips -s format gif /tmp/pngs/*.png --out /tmp/gifs
    cd /tmp/gifs
    gifsicle --optimize=3 --delay=10 --loopcount *.gif > $dir/$filename.gif
    rm -r "/tmp/pngs"
    rm -r "/tmp/gifs"
    
    You’ll notice that the first line appends /usr/local/bin to $PATH. This is to get access to the Homebrew installed utilities ffmpeg and gifsicle. Let’s break down the rest:
    1. filename and dir get me the name of the file and the files directory, respectively.
    2. Then we make some temp directories.
    3. Run ffmpeg to convert the video file to a series of PNG files.
    4. Run sips to convert those PNGs to GIFs and put them in another folder.
    5. Run gifsicle to combine the individual GIF files into a single GIF animated image. You’ll notice the output is the directory the source file was in, with the same name, except for the additional .gif extension.
    6. Remove our temp directories.
  8. Your final task should look like this: final
  9. Go ahead and save it with Command+S or File > Save. The name you give this action is the same name that will appear in the Actions menu in Finder.
  10. Open Finder and locate a video file. Right click your file and go to Quick Actions > Name of your new action quick-action-menu
  11. Depending on the size of the source video, you’ll have a new animated GIF land in the same directory as your source video: final-output

That’s it! Any questions or suggestions please let me know.

Built with Hugo
Theme Stack designed by Jimmy