0

A possible history for this question: Eliminate slow speed factor in ffmpeg and image-magic commands

I am creating a GIF using this command with wiper effect,

convert -gravity southeast logo.png  -write MPR:logo \
  \( before.jpg -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:before \) \
  \( after.jpg  -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:after  \) \
  -delete 0--1                                           \
  MPR:before  \( MPR:after  -set delay 25 -crop 15x0 -reverse     \) \
  MPR:after   \( MPR:before -set delay 27 -crop 15x0   \) \
  -set delay 2 -loop 0 temp.gif -delete 1--1 -resize 640x thumb.jpg

Which is working all fine, but it creates a gif of size 3MB+, as there are more than 100 frames in wiper effect.

How can we decrease the number of frames for the wiper effect as well as the quality remains the same, but size decreases? I tried to attach different parameters with different loop values but nothing worked.

Junaid Farooq
  • 2,484
  • 5
  • 26
  • 63
  • Change your delay values to make fewer frames. Or reprocess your animated gif so that all frames use the same color map. See -remap https://imagemagick.org/Usage/quantize/#remap. You can create your color map by appending all or selected frames that span your animation and reduce colors to 255 then get the unique colors and create an image of appended 1x1 pixel colors for all 255 colors. – fmw42 Oct 26 '18 at 16:20
  • Can you answer something with an example? – Junaid Farooq Oct 26 '18 at 16:28
  • Perhaps I misunderstood. I thought you were creating a gif animation. But your result is thumb.jpg, namely, a JPG file not an animation. Please clarify. If you are really creating a GIF animation, then post a link to your animation. – fmw42 Oct 26 '18 at 17:37
  • The above command creating 2 things, a gif and a thumbnail. you can leave this part `-delete 1--1 -resize 640x thumb.jpg` and the command is creating such file https://media.evercam.io/v1/cameras/crome-tidcz/compares/compa-ehsuwxf.gif – Junaid Farooq Oct 26 '18 at 17:42
  • The above command is taking logo and before after image and creating a gif. and it has 178 frames, in it. I need to lower the frames but with same speed as well size decreased – Junaid Farooq Oct 26 '18 at 17:44
  • I cannot reproduce your animation without before.jpg and after.jpg. Also which logo? Is it LOGO at https://imagemagick.org/script/formats.php#builtin-images? – fmw42 Oct 26 '18 at 18:12
  • The answer to using a common color map is give in the update answer by Mark Setchell at https://stackoverflow.com/questions/52944497/eliminate-slow-speed-factor-in-ffmpeg-and-image-magic-commands/ – fmw42 Oct 26 '18 at 18:18

1 Answers1

0

Here are 3 Imageamagick commands. The first is yours above. In the second I create a common color map image from the two input images (before and after) and apply that to each frame. In the third, I do the same color map processing, but reduce the number of frames. I took all the images from the web site and animation referenced and put the new logo in the bottom left corner, since there was already one in the bottom right corner. Note that I added -layers optimize to all, which decreased the file size considerably.

logo:

enter image description here

before:

enter image description here

after:

enter image description here

Method 1 (original):

convert -gravity southwest everlogo.png -write MPR:logo \
\( before.jpg -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:before \) \
\( after.jpg  -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:after \) \
-delete 0--1 \
MPR:before \( MPR:after -set delay 25 -crop 15x0 -reverse \) \
MPR:after \( MPR:before -set delay 27 -crop 15x0 \) \
-set delay 2 -layers optimize -loop 0 temp1a.gif
2,246,665 bytes
174 frames


Result is too large to upload here.

Method2 (common color map):

convert -gravity southwest \
everlogo.png -write MPR:logo +delete \
\( before.jpg -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:before \) \
\( after.jpg  -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:after \) \
-delete 0--1 \
\( MPR:before MPR:after +append +dither -colors 255 -unique-colors -write MPR:colors +delete \) \
MPR:before +dither -remap MPR:colors \( MPR:after +dither -remap MPR:colors -set delay 25 -crop 15x0 -reverse \) \
MPR:after+dither -remap MPR:colors \( MPR:before +dither -remap MPR:colors -set delay 27 -crop 15x0 \) \
-set delay 2 -layers optimize -loop 0 temp1b.gif
2,004,345 bytes
173 frames


enter image description here

Method 3 (common color map; twice the crop width and four times the delay; adjust the delay as desired to match better Method 2)

convert -gravity southwest \
everlogo.png -write MPR:logo +delete \
\( before.jpg -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:before \) \
\( after.jpg  -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:after \) \
-delete 0--1 \
\( MPR:before MPR:after +append +dither -colors 255 -unique-colors -write MPR:colors +delete \) \
MPR:before +dither -remap MPR:colors \( MPR:after +dither -remap MPR:colors -set delay 100 -crop 30x0 -reverse \) \
MPR:after+dither -remap MPR:colors \( MPR:before +dither -remap MPR:colors -set delay 108 -crop 30x0 \) \
-set delay 4 -layers optimize -loop 0 temp1c.gif
1,927,359 bytes
87 frames


enter image description here

I am bit surprised that reducing the number of frames so significantly does not make a commensurate decrease in animation file size. But I suspect that is due to the -layers optimize.

ADDITION:

Also in all the code above, I do not understand the use of setting the delay of 25 and 27 inside the parentheses and then setting it again to 2 afterwards. I do not think animated gifs can have different delays. The only one that matters is the final one.

So just in terms of delays and number of frames, these two do the same. The first uses -crop 15x0 and -set delay 4 and the second use -crop 30x0 and -set delay 8. This produces the same speed animation, but the second has fewer frames. The first is 174 and the second is 88 and so more coarse steps. But the file sizes are about the same. So it seems that the common color map causes the largest decrease in file size.

convert -gravity southwest everlogo.png -write MPR:logo \
\( before.jpg -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:before \) \
\( after.jpg  -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:after \) \
-delete 0--1 \
MPR:before \( MPR:after -crop 15x0 -reverse \) \
MPR:after \( MPR:before -crop 15x0 \) \
-set delay 4 -layers optimize -loop 0 temp3.gif
frames 174
2,246,665 bytes


convert -gravity southwest everlogo.png -write MPR:logo \
\( before.jpg -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:before \) \
\( after.jpg  -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:after \) \
-delete 0--1 \
MPR:before \( MPR:after -crop 30x0 -reverse \) \
MPR:after \( MPR:before -crop 30x0 \) \
-set delay 8 -layers optimize -loop 0 temp2.gif
frames 88
2,174,954 bytes


fmw42
  • 46,825
  • 10
  • 62
  • 80