Starting 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 output file should only be the bit that is to be converted to a Gif. Next, to get a high-quality gif, we need to extract a pallet 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 filter to create a palette. At the time of writing, this command doesn’t really print any progress info and it takes a while, depending on how long the input video is. Be patient!
With the palette, we can now create 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 happens in this one line, lets break it down step by step:
- Take two inputs, first our Video and second the palette we created in the previous step. Both of these are available as video streams.
- Apply a filter to create a new video output stream which…
- takes the only video stream from the first input file (the Video) …
- sets it’s FPS to
- scales it down to
480pxwidth, keeping the aspect ratio (by specifying
-1for height) …
- and names the temporary video stream created by these filters
a(denoted in square-brackets).
- Then, a second filter is applied (denoted by the semi-colon following the first filter) which…
- takes the frame-rate adjusted and scaled video as the first argument and…
- the palette we created in the previous step as the second argument and…
- uses the both to optimize the output Gif.
- The output is then written to the
Note: Adjusting FPS and scaling the video is mainly done to lower the required space for the gif, as they can get pretty big. Play with these values until you’re satisfied with the result!