Skip to content

Instantly share code, notes, and snippets.

@timakro
Last active July 21, 2023 16:41
Show Gist options
  • Save timakro/b03c987e1e8e95b4738e55e64089f44e to your computer and use it in GitHub Desktop.
Save timakro/b03c987e1e8e95b4738e55e64089f44e to your computer and use it in GitHub Desktop.
Save GIFs from within mpv
# Press g to save A-B loop selection as GIF (select by pressing l).
# Encode at 20 FPS or less by halving the source frame rate repeatedly.
# Scale to width of 480 pixels and preserve aspect ratio.
# https://engineering.giphy.com/how-to-make-gifs-with-ffmpeg/
g run ffmpeg -nostdin -ss ${=ab-loop-a} -to ${=ab-loop-b} -i ${path} -vf "fps='st(0,source_fps);while(gt(ld(0),20),st(0,ld(0)/2));ld(0)',scale=w=480:h=-1,split[a][b];[a]palettegen[p];[b][p]paletteuse" ./mpv-${ab-loop-a}-${ab-loop-b}.gif
  • Based on mpvs A-B loop feature which is bound to the l key by default
  • Depends only on ffmpeg
  • Uses the palettegen feature of ffmpeg for better colors
  • The frame rate of the GIF will be between 10 and 20 FPS and is determined by halving the source frame rate repeatedly
  • The GIF is 480 pixels wide (this can be changed easily)
  • Tested on Linux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment