Files
rsmsn_blog/content/posts/create_gif_on_commandline.md
2024-07-03 18:21:05 -04:00

3.2 KiB

title, date, tags, author, showToc, TocOpen, draft, hidemeta, description, disableHLJS, disableShare, disableHLJS, hideSummary, searchHidden, ShowReadingTime, ShowBreadCrumbs, ShowPostNavLinks, ShowWordCount, ShowRssButtonInSectionTermList, UseHugoToc, cover
title date tags author showToc TocOpen draft hidemeta description disableHLJS disableShare disableHLJS hideSummary searchHidden ShowReadingTime ShowBreadCrumbs ShowPostNavLinks ShowWordCount ShowRssButtonInSectionTermList UseHugoToc cover
Create a GIF from a video - Right from the Command Line! 2024-07-03T17:41:15-04:00
tutorial
snippets
commandline
Me true false false false create_gif_on_commandline true false false false true true true true true true true
image alt caption relative hidden
<image path/url> <alt text> <text> false true

Finding this little set of commands is one of the main reasons why I love the Command Line. Once I realized that almost everything that I was doing in a UI was possible on the CLI, the world opened up. One of the things I return to from time to time is when I can't find a GIF I want to use in a message or email. Every time, I look up how to convert a video to a GIF and every time, I'm given a new solution. Imgur's video-to-gif used to be reliable, but the last few times it hasn't been working as expected.

This last time, I was trying to convert a very important It's Always Sunny in Philadelphia clip to a reaction gif. Can you believe there is no "thank you" reaction gif from when Dennis reads Charlie's speech?!. Here's the gif for your own collections.

Charlie & Dennis Thank you Gif

The command line functions I found came to get this done came mostly from Funky Cloud Medina's post on this. I didn't want to use automator, but just write out a few commands, so here is what I did (on MacOS):

First, install ffmpeg and gifsicle with Homebrew. brew install ffmpeg gifsicle

Next, navigate to the directory where your video is. If you plan on doing this regularly, you can create some permanent directories, but I started with creating two temporary directories for the images and then the final gifs.

mkdir pngs/ gifs/

This will create both gifs and pngs folders in the directory you're currently in.

Next, we'll process the movie.

ffmpeg -i Untitled.mov -r 10 pngs/out%04d.png

Untitled.mov is the name of the video file in the directory you're currently in and outputs each frame to the pngs folder with increment digits. The incrementing digits are so you don't overwrite everything and end up with just a single picture file.

Next, we'll use sips, the Scriptable Image Processing System. (Check out man sips from your own CLI for more info!)

sips -s format gif pngs/*.png --out gifs

Almost there! This is processing all the image files into the gifs folder. Let's now move into the gifs folder with cd: cd gifs

And now we can use gifsicle to merge all the images into a single gif! We'll do that with the following command:

gifsicle --optimize=3 --delay=10 --loopcount *.gif > ~/Documents/thankyou.gif

And voila! You should have a auto-playing gif file ready for use in all your reactions and emails. Enjoy!