Skip to content

Instantly share code, notes, and snippets.

@jorgecoa
Last active June 25, 2019 16:26
Show Gist options
  • Select an option

  • Save jorgecoa/83ba7dc57f6c16f4ca6cf59e336d3b8f to your computer and use it in GitHub Desktop.

Select an option

Save jorgecoa/83ba7dc57f6c16f4ca6cf59e336d3b8f to your computer and use it in GitHub Desktop.
Convert video into high quality GIF

This is the way I found to create high quality GIFs from videos. Feel free to recommend a better path or tool.

  1. Install ffmpeg. Make sure ffplay is installed along with ffmpeg. For example for macOS:
    brew install ffmpeg --with-ffplay
  2. If the original video has black bars at the top/bottom, you can remove them:
    1. Detect the values for the cropping filter using ffplay. The cropdetect filter is configured with the arguments cropdetect=limit:round:reset, 24:16:0is the default.
      ffplay -i video.mp4 -vf "cropdetect=24:16:0"
      # At the end of each line from the output you will find the values for the cropping filter. E.g. crop=1280:656:0:72
    2. Let's crop using the cropping filter values.
      ffmpeg -i video.mp4 -vf crop=1280:656:0:72 -c:a copy video_cropped.mp4
  3. Use this script to convert your video into GIF.
    videotogif.sh input.mp4 output.gif
#!/bin/sh
palette="/tmp/palette.png"
filters="fps=15,scale=1280:-1:flags=lanczos"
ffmpeg -v warning -i $1 -vf "$filters,palettegen" -y $palette
ffmpeg -v warning -i $1 -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse=dither=bayer:bayer_scale=5" -y $2
@jorgecoa

Copy link
Copy Markdown
Author

Works with other formats as .mov too

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