Convert a video file to a GIF using a MacOS Automator task
| 3 minutes
Utilities macOS Automator

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

  2. You’ll be presented with a dialog to open an existing Automator file. Click the New Document button.

  3. Select Quick Action for the document type and click Choose.

  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.

    I used my Automator task to make this lol

  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

  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.

  7. Paste the following block into the contents of the script task:
    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:

  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

  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:

You can download the Automator task here:

Convert movie to GIF Workflow

MD5 Checksum
643e03331190a9393dc3f0dc27e897e0
SHA256 Checksum
cbc4fc3e7642ce3f37e82b15b8cfb9931800ed549db836435b5740c26253cff7

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

About Stellios Williams
Senior Cloud Solutions Architect - Service Providers VMware
This is my personal tech related blog for anything private and public cloud - including homelabs! My postings are my own and don’t necessarily represent VMware’s positions, strategies or opinions. Any technical guidance or advice is given without warranty or consideration for your unique issues or circumstances.
Comments
comments powered by Disqus
Advertisement