Brendan Dawes
The Art of Form and Code

Converting a gif to a video with FFMPEG

This is just a quick one to add to my other ffmpeg workflows. I wanted to quickly convert a gif to video so it could be posted on Instagram for instance. Rather than use an online service I knew that ffmpeg would no doubt have something which could do it and sure enough the answer is as ever with this post on Stackoverflow.

I've detailed before on this blog about how to install ffmpeg.

In Automator I created a new Quick Action, chose Folders and Files as an input and then added a Run Shell Script action, changing the Pass Input to as arguments.

I then added this script to the script field:

cd "$@:h"
for f in "$@"
    name=$(echo $f | cut -f 1 -d '.')
        /usr/local/bin/ffmpeg -y -i "$f" -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" "$name.mp4"

The first line changes the directory to the parent directory of the gif file, using a handy zsh shell function.

Then for each selected file it first gets the filename of that file, which we'll use to name our new video file.

The last step is to convert the gif to video using ffmpeg.

Here's how it looks in Automator.

After saving this Automator workflow, I can Control-click on the gif and choose my newly created action to instantly convert to a video.