Fetching GIFs from GIPHY with bash

25. August 2020

As someone who can only be described as a reaction GIF ‘connoisseur’, I rarely pass up the opportunity to add a new GIF to my collection. Unfortunately, the proliferation of hosts like GIPHY and Tenor, who reencode GIFs in webp or MPEGs formats to save bandwidth and rely on JavaScript or the user agent to handle looping, has made this more complicated than a Right Click -> Save As.

Tired of trawling through Developer Tools to grab the source image, I wrote a bash script that does the heavy lifting of extracting the GIF’s ID from a URL, generating a filename based on the GIF’s title, and downloading it.

You can grab it grab it from a gist here. Plop it on your PATH, chmod +x, and invoke giphy-grab:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ giphy-grab
giphy-grab [-f|--force] [-s|--skip-title] [-o|--output] <url>
            --force: overwrite output file if it exists
            --skip-title: do not fetch GIF name from GIPHY
            --output: filename or destination folder for GIF

$ export url="https://giphy.com/gifs/cat-cats-OmK8lulOMQ9XO"
$ giphy-grab ${url}
Destination URL: ${url} | Output: Cute Cat.gif

# Output to a folder with title from Giphy - perfect for scripting!
$ mkdir gifs && giphy-grab -o gifs/ ${url}
Destination URL: ${url} | Output: gifs/Cute Cat.gif