23

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?

1
  • I think an online editor would be much easier than finding some special GIF editor for Ubuntu, but if you open the file as layers in GIMP, you can manually delete the frames you don't want before exporting it as a GIF again. Jan 12, 2017 at 0:06

2 Answers 2

30

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.

1
  • 6
    FYI for others, -O3 is an optimization option and not necessary (though maybe helpful) and #XXXX is just a placeholder for frame number. Use the frame numbers as they appear in the previous --info step output. In my case '#0-74' for a gif I just made. (Got hung up on four digit numbers for a moment when I first saw this.)
    – Hendy
    Dec 3, 2017 at 18:02
1

Here's one more. Using ImageMagick, the command:

 magick Long.gif[0-100] Short.gif

takes the first 100 frames of Long.gif and saves them as Short.gif

You can see how many frames you're starting with using:

magick identify Long.gif

The last line of the output will show the number of frames in square brackets, for example:

Long.gif[829] GIF 64x64 64x64+0+0 8-bit sRGB 256c 1.86076MiB 0.031u 0:00.034

which indicates that Long.gif has 830 frames.

Note that much legacy documentation of ImageMagick refers to the identify (now magick identify) and convert (now magick) commands, which could be what you need depending how recent your version is.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .