Thursday, October 10, 2019

A video alternative to animated GIFs

I wanted to do something like my previous post, but using a video format instead of the ancient .gif format. Here's what I ended up using:

ffmpeg -ss 00:04.20 -i in.mp4 -t 00:06.30 -an -vf "crop=1668:1668:1292:10, scale=480:-1" out.mp4

Options:
  • -ss 00:04.20
    Start 4.2 seconds into the input file
  • -i in.mp4
    Specifies the input file
  • -t 00:06.30
    Limit duration to 6.3 seconds
  • -an
    No audio
  • -vf " ... "
    Specify the filter chain to operate on the video
    • crop=1668:1668:1292:10
      Crop out a 1668 (width) x 1668 (height) window, with the top-left corner located at x=1292, y=10
    • scale=480,-1
      Rescale to width=480, height=auto (preserving aspect ratio)
  • out.mp4
    Specifies the output file

Monday, April 8, 2019

Making animated gifs

Recently I wanted to make some animated gifs from a video.

First I used my video player (mpc-hc) to grab some frames and save them as .jpg's in a folder.

Then using ImageMagick, I cropped, resized, and created a gif:
$ convert -crop 1200x900+480+0 +repage -resize 480x480 -delay 8 -loop 0 *.jpg output.gif

The repage argument is necessary so that the output gif doesn't use a virtual canvas for the crop operation.

This was kinda big, though, so I looked at this gist and this forum thread to get some ideas on how to reduce the filesize. The source video is live-action footage with a stationary camera, and I settled on:
$ convert -fuzz 5% -ordered-dither o8x8,30 -layers Optimize +map output.gif output2.gif

This dropped the filesize from 1.9 MB to 0.6 MB without affecting the quality too much. The number of colors for posterizing the ordered dither (30) was chosen by some trial and error.