FFmpeg - Create GIF Animated Images
Below is my really simple script for creating GIFs quickly and easily from longer video files. If you want to put in a lot of effort to get them nicer looking there is this post at engineering.giphy.com.
#!/bin/bash # Specify the relative or rull path to the video you wish to convert to an animated webp. INPUT_VIDEO="MyVideoFile.mkv" # Specify the name for the generated animated image. This may also be a full filepath like # /path/to/myfile.gif OUTPUT_FILENAME=myGeneratedFile.gif # Specify the name for a temporary file to put within /tmp. This must have the same file # extension as the original video. TEMP_FILENAME="temp.mkv" # Specify the start time of the video that the clip should be taken e.g. 1 minute and 10 seconds in START_TIME="00:08:35" # Specify the length of time for the clip. E.g. 10 seconds LENGTH_TIME="00:00:05" # Set width of the output gif. The height will be derived from the w:h ratio. WIDTH=500 # Set the number of times to loop. # 0 = infinite # 1+ = that many times. # -1 = no loop LOOP=0 # Extract the short video we wish to turn into a gif ffmpeg \ -i $INPUT_VIDEO \ -ss $START_TIME \ -t $LENGTH_TIME \ -vcodec copy \ -acodec copy \ /tmp/$TEMP_FILENAME # Set framerate of animated image. FRAMERATE=20 ffmpeg \ -i /tmp/$TEMP_FILENAME \ -loop $LOOP \ -vf "fps=$FRAMERATE, scale=$WIDTH:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" \ $OUTPUT_FILENAME
-ss before the
-i parameter, it is much quicker, but the time will not be as accurate
as Ffmpeg skips to the closest I frame in the video which may be seconds away from where you wish to start/end.
First published: 28th February 2021