Skip to content

Instantly share code, notes, and snippets.

@dannguyen
Last active September 6, 2024 12:22
Show Gist options
  • Save dannguyen/a64319e99b4c1935f826 to your computer and use it in GitHub Desktop.
Save dannguyen/a64319e99b4c1935f826 to your computer and use it in GitHub Desktop.
Using youtube-dl and gifify from the command-line to make a cat gif

Using youtube-dl and gifify from the command-line

Turn this cute YouTube cat video into a briefer-but-still-cute GIF:

gif

Software to download

  • youtube-dl is a command-line tool for quickly downloading video files from a given YouTube URL
  • the gifify utility for converting video into GIFs, which is part of the gifsicle library.

Installing youtube-dl and gifify

Homebrew can be used to install both youtube-dl and gifify (via gifsicle):

brew install youtube-dl gifsicle

Commands to run

# downloads from the given YouTube URL to the file path of `catdrawer.mp4`
youtube-dl "https://youtu.be/sP1_rT0wyQo" -o catdrawer.mp4

# creates a copy of `catdrawer.mp4` and converts seconds 19 to 27 to a GIF file named `catdrawer.gif`
gifify catdrawer.mp4 -o catdrawer.gif  --from 19 --to 27 --resize 400:-1 --colors 128

Then upload to Imgur. You could probably write a curl command to do it via Imgur's API

Piping youtube-dl to stdout

As victorperin points out below, instead of downloading and saving a MP4 file with youtube-dl, we can pipe the MP4 file to stdout and directly into gifify. This means that the intermediary catdrawer.mp4 is never saved to disk:

youtube-dl "https://youtu.be/sP1_rT0wyQo" -o - \
   | gifify -o catdrawer.gif --from 19 --to 27 --resize 400:-1 --colors 128
@victorperin
Copy link

victorperin commented Jan 27, 2020

You can use pipes to run it faster, without need to write to disk when downloading the mp4 version:

youtube-dl "https://www.youtube.com/watch?v=kl1RwhATTzA" -o - | gifify -o catdrawer.gif --from 19 --to 27 --resize 600:-1 --colors 128

@dannguyen
Copy link
Author

good call! I frequently forget that - can be used to direct to stdout instead of a file path. I'll update my readme, thanks!

@weltonrodrigo
Copy link

Gifify is installed with brew install gifify

@alexjyong
Copy link

I can second that brew install gifify is needed for this to work and appears to not be part of gifsicle.

@cosmojg
Copy link

cosmojg commented Oct 7, 2022

More importantly, the version of gifify installed by Homebrew isn't the one used in this guide and lacks the features needed to follow it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment