Is it possible to shorten a .gif file through FFmpeg?
I need to upload a .gif but the service I'm using only allows a max filesize of 30mb (the file is 35.4mb)
2 Answers
.gif is an extremely inefficient format for video files.
If you are flexible with the format, the best option is to convert the file to .mp4: This will reduce the file size by an enormous degree without significant loss of quality. In many cases it can reduce the file size of a .gif by a factor of 100x or more. Use the following command:
ffmpeg -i input.gif -pix_fmt yuv420p output.mp4Replace input.gif with the source filename and output.mp4 with the desired name for the new file.
If you need the format to be .gif, you can shorten the animation to reduce the size. To do this, you will need to define start and end times in ffmpeg.
Here is an example:
ffmpeg -i input.gif -ss 00:00:00 -to 00:00:03 -c copy output.gif-ssdefines the start time inHH:MM:SS-todefines the end time.- You can also use
-tto specify duration instead of end time. - You can add a decimal point to indicate milliseconds. (ex.
00:00:05.5)
A third option would be to scale down the dimensions of the image:
ffmpeg -i input.gif -vf "scale=iw/2:ih/2" output.gif- This will halve the image dimensions, and roughly quarter the file size. You can scale this down even further by changing the value. For example,
scale=iw/3:ih/3will result in an image with one third the original dimensions.
With the GNU Image Manipulation Program (GIMP) you can edit GIF files. In Ubuntu 20.04 you can install GIMP from the Software app, or from the command line by running:
sudo apt install gimpIf you want to remove some frames from a GIF file, open it in GIMP, then remove the layers (frames) you don't want. Another option would be to resize the image: go to Image > Scale Image... and scale the image to the proportion you want.
Once you have edited the image, go to File > Export As.... Make sure that the filename extension is .gif and click Export. Select the As animation option and edit the animation settings below, then click Export again.