Help! I need a ʒaɪf!

So, you just watched James Hoffmann doing another glorious oh no face and naturally you need this in your life.

However, you need subtitles, an acceptable file size, and more. Let’s get to it!

You download your video using youtube-dl, possibly in a more sensible file-size (--format '[height <=? 720]') and then also the subtitles using --write-auto-sub --skip-download.

And then comes the magic, which will result in fun things like this:

Which was generated by calling ./gif-it.sh 3:06 3:13.5 Coffee\ Substitutes\ -\ Tasted\ and\ Explained-KArQ3mBzWC4.mp4 ooh-no.

Behold gif-it.sh:

#!/bin/bash

set -eof pipefail

START="$1"
END="$2"
INPUT="$3"
OUTPUT_BASE="$4"

# reencode include subtitles (also cuts to size)
mpv --start="$START" --end="$END" --sub-font-size=70 "$INPUT" -o "${OUTPUT_BASE}.mp4"

# mp4 with sound
#mpv --start="$START" --end="$END" "$INPUT" -o "${OUTPUT_BASE}.mp4"

# gif
ffmpeg -i "${OUTPUT_BASE}.mp4" -filter_complex "[0:v] fps=12,scale=480:-1,split [a][b];[a] palettegen [p];[b][p] paletteuse" "${OUTPUT_BASE}.gif"

# smaller mp4 of gif
mpv "${OUTPUT_BASE}.gif" -vf format=yuv420p -o "${OUTPUT_BASE}.gif.mp4"

A few notes:

  1. I could not figure out how to make ffmpeg output a GIF and video, so the first step is a reencode to render the subtitles. It also cuts the video to size, because we only want that part anyways.
  2. The filter pipeline is interesting, if impenetrable to debug for me if things break. See the post by Giphy below for a detailed explanation.
  3. Finally there’s an MP4 of the GIF for smaller file sizes.
    • Note that here we have -vf format=yuv420p, because Firefox does not play yuv444 videos, which was what mpv selected by default when converting from the GIF.

Speaking of file sizes, for the 8.5 second GIF we get the following:

$ ls -lh ooh-no.*
-rw-r--r-- 1 luna luna 3.2M May  5 17:45 ooh-no.gif
-rw-r--r-- 1 luna luna 195K May  5 17:45 ooh-no.gif.mp4
-rw-r--r-- 1 luna luna 760K May  5 17:45 ooh-no.mp4

Not too bad, especially the .gif.mp4, and given that I am not a GIF professional.

That’s it, enjoy, this time with sound:

Resources:

And as for the ʒaɪf monstrosity/awesomeness, see https://t.numblr.net/pbsideachannel/status/341708073540399105 and https://www.youtube.com/watch?v=bmqy-Sp0txY from PBS Idea Channel.