2

I want to manually modify an animated gif. So I'm trying to convert it to individual (editable) frames, after which I can draw my modification and then re-assemble the frames.

First I convert a 31 seconds long mp4 to animated gif :

ffmpeg -i Daisies.mp4 -r 15 -vf scale=512:-1 -ss 00:00:03 -to 00:00:06 DaisiesOK.gif 

This animated gif is OK.

Then I convert this animated gif to individual frames :

magick convert DaisiesOK.gif Daisies.png 

The first frame looks OK, except it's in 256 colors, but the next ones are very pale, full of transparent pixels.

These frames to animated gif :

magick convert -delay 20 -loop 0 *.png Daisies.gif 

Gives a very ugly video, the daisies have white "shadows", the place where they started from (they're swaying) stays imprinted with white daisy edges.

If I try to use jpeg instead of png, it's even worse, most of the video is just black.

How can I extract the frames preserving all the color information? Does the problem come from imagemagick or from ffmpeg?

0

2 Answers 2

7

The first frame looks OK, except it's in 256 colors,

You're probably aware of this, but gif is a palette-based image format with only 8 bits for palette entries, i.e., at most 256 different colors. And the way you generate the gif doesn't "cleverly" use the most prevalent colors in your input video, but a fixed default palette.

but the next ones are very pale, full of transparent pixels. How can I extract the frames preserving all the color information? Does the problem come from imagemagick or from ffmpeg?

That's how gif animation works. The frame you extract contain all the color information. They are just not useful without the previous frame. So, the problem is in none of the programs, it's what you asked them to do: generate an animated gif, which is a sequence of frames containing transparent pixels where a frame is identical to the previous and colored pixels where there's change.

Make sure you really want an animated gif. Browsers these days use less CPU power to play an MP4 than to play back a gif, and they look better in all circumstances. You can also experiment with animated PNG.

Then I convert this animated gif to individual frames …

ffmpeg can just as well be used to extract frames from a gif; the detour through convert is not necessary:

ffmpeg -i DaisiesOK.gif frame_%03d.png 

The question here is why you not simply convert the input Daisies.mp4 to an 8-bit colorspace, generating an optimal palette on the way (with the palettegen video filter), and dump the frames directly to PNGs. The detour through the GIF seems counterproductive.

7
  • 1
    Take a look at gifsicle lcdf.org/gifsicle/man.html Commented Feb 10 at 5:01
  • After reading this, I tried "ffmpeg -i Daisies.mp4 frame_%03d.png" and it functioned great. All my png pictures are fully colored. The problem now is that there are 958 of them instead of the previous 40, so I have to find a way to reduce this number, otherwise the editing would take ages. Thank you. Commented Feb 10 at 9:17
  • you're looking for setting a frame rate for the output. Commented Feb 10 at 10:15
  • 1
    @AlainReve great to hear, by the way. Also note that PNG might not be a great choice for photographic content, in terms of size wasted. Maybe try ffmpeg -i Daisies.mp4 -pred 1 frame_%03.jp2 to get losslessly compress JPEG2000 pictures instead; replace -pred 1 with -q:v 10 (the higher the number, the stronger the compression, i.e. -q:v 10 produces larger images than -q:v 20.) Commented Feb 10 at 10:30
  • @AlainReve you probably want to add the other parameters you had in your initial command line to select to specific part of the video you want and possibly adjust the frame rate. Commented Feb 10 at 14:32
4

You're missing the -coalesce flag, so with v7 ImageMagick you'd need:

magick DaisiesOK.gif -coalesce Daisies.png 

Or, if using old, outdated v6 ImageMagick:

convert -coalesce DaisiesOK.gif Daisies.png 

But also note that ImageMagick really likes loading whole processed images to the RAM in raw. You will need enough memory for that.

4
  • This command froze my computer. I only have 2 of RAM. Commented Feb 10 at 9:12
  • 1
    @AlainReve 2 GB? Your machine was possibly swap thrashing Commented Feb 10 at 10:12
  • Chris, You really should avoid magick convert ... as it has undesirable side-effects, see stackoverflow.com/a/61208844/2836621 Instead, use plain magick INPUT [OPTIONS] OUTPUT note the re-ordering - you must specify the input file before any operations on it. So your command becomes magick DaisiesOF.gif -coalesce Daisis.png You have my upvote already. Commented Feb 13 at 10:56
  • @MarkSetchell originally I wrote convert… and it was edited to magick convert…. If you're suggesting it should now be magick… then please do edit the answer. An accurate answer is always preferred. (What I originally wrote I'd tested, but I don't use IM enough to know the most uptodate techniques.) Commented Feb 13 at 11:06

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.