Cut duration of animated gif

I have an animated gif and I'd like to cut off the last 5 seconds. Does anyone know how to do that in Linux/Ubuntu?


There are multiple ways to do this.

Option 1: Use a command line tool called gifsicle. It's available in default Ubuntu/Debian repositories.

It's fast and won't affect the quality, buy it only accepts frame numbers, not time, so if you need to cut exact time, you should figure out the frame number yourself. You can view the total number of frames and delay times between each frame with this command:

gifsicle --info YOURFILE.gif

Then you can divide the time you want to cut with the frame delay and it will give you the number of frames in this time, so you can figure out start frame and end frame.

Replace XXXX with the frame where you want your GIF to start, and YYYY with frame where you want your GIF to end.

gifsicle YOURFILE.gif '#XXXX-YYYYY' -O3 > cut.gif

Option 2: Using FFMPEG. It's also available in apt.

FFMPEG accepts time directly, so it's very straightforward, but it may worsen the quality of output gif and increase file size in some cases.

ffmpeg -i YOURFILE.gif -ss 00:00:04 -t 00:00:08 -async 1 cut.gif

Option 3: if you're ok with online tools and your GIF does not exceed 20MB, this online tool can cut GIFs by frame number or by time and won't affect the quality.