inline Make high-quality gif

Originally published on May 20th, 2020 (Last updated on May 20th, 2020)

Start­ing from a Source file, extract only the Video and cut it down:

ffmpeg -ss <start> -i <video-file> -t <length> -map 0:v -c copy cut.mp4

This gives a nice clean plate to work from. The out­put file should only be the bit that is to be con­vert­ed to a Gif. Next, to get a high-qual­i­ty gif, we need to extract a pal­let from our video for usage when the gif will be created:

ffmpeg -i cut.mp4 -filter_complex "[0:v] palettegen" palette.png

This takes the only video stream from the first input file and uses the palettegen fil­ter to cre­ate a palette. At the time of writ­ing, this com­mand does­n’t real­ly print any progress info and it takes a while, depend­ing on how long the input video is. Be patient!

With the palette, we can now cre­ate the final Gif:

ffmpeg -i cut.mp4 -i palette.png -filter_complex "[0:v] fps=24,scale=480:-1 [a]; [a][1:v] paletteuse" out.gif

A lot hap­pens in this one line, lets break it down step by step:

  1. Take two inputs, first our Video and sec­ond the palette we cre­at­ed in the pre­vi­ous step. Both of these are avail­able as video streams.
  2. Apply a fil­ter to cre­ate a new video out­put stream which…
  3. takes the only video stream from the first input file (the Video) …
  4. sets it’s FPS to 24
  5. scales it down to 480px width, keep­ing the aspect ratio (by spec­i­fy­ing -1 for height) …
  6. and names the tem­po­rary video stream cre­at­ed by these fil­ters a (denot­ed in square-brackets).
  7. Then, a sec­ond fil­ter is applied (denot­ed by the semi-colon fol­low­ing the first fil­ter) which…
  8. takes the frame-rate adjust­ed and scaled video as the first argu­ment and…
  9. the palette we cre­at­ed in the pre­vi­ous step as the sec­ond argu­ment and…
  10. uses the both to opti­mize the out­put Gif.
  11. The out­put is then writ­ten to the out.gif-file.

Note: Adjust­ing FPS and scal­ing the video is main­ly done to low­er the required space for the gif, as they can get pret­ty big. Play with these val­ues until you’re sat­is­fied with the result!



No com­ment sec­tion here 😄

You can reach me over at @knuth_dev or send me an Email.