Files
rsmsn_blog/content/posts/create_gif_on_commandline.md

66 lines
3.2 KiB
Markdown
Raw Permalink Normal View History

---
title: 'Create a GIF from a video - Right from the Command Line!'
date: 2024-07-03T17:41:15-04:00
tags: ["tutorial", "snippets", "commandline"]
author: "Me"
showToc: true
TocOpen: false
draft: false
hidemeta: false
description: 'create_gif_on_commandline'
disableHLJS: true # to disable highlightjs
disableShare: false
disableHLJS: false
hideSummary: false
searchHidden: true
ShowReadingTime: true
ShowBreadCrumbs: true
ShowPostNavLinks: true
ShowWordCount: true
ShowRssButtonInSectionTermList: true
UseHugoToc: true
cover:
image: "<image path/url>"
alt: "<alt text>"
caption: "<text>"
relative: false
hidden: 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](https://imgur.com/vidgif) 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?!](https://www.youtube.com/watch?v=ROCKGuuviis). Here's the gif for your own collections.
![Charlie & Dennis Thank you Gif](../posts/img/thankyou.gif)
The command line functions I found came to get this done came mostly from [Funky Cloud Medina's](https://www.funkycloudmedina.com/2022/06/convert-a-video-file-to-a-gif-using-a-macos-automator-task/) 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.
```conf
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](https://www.lcdf.org/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!