# Package animate is great but could it have a better animation compression?

I just tried the animate package to get pdf files with animations and it works great!

However, I noticed that the size of the files generated is 10 times larger that what it could be for the animations that I am dealing with, and for most cases it is even worse.

I believe that the reason is not using the difference between consecutive frames instead of the full frame for every frame. This is for example what gifsicle uses http://www.lcdf.org/gifsicle/ (source available) "Stores only the changed portion of each frame, and can radically shrink your GIFs."

Any chance this could be optimized in the animate package?

-
Welcome to TeX.sx! Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say "thank you" to users who helped you. –  Kurt Apr 7 '13 at 13:28
I think you should take a look at my answer in: tex.stackexchange.com/questions/73067/… it addresses your exact proposition. By the way, it would be very difficult for the package to automatically determine equivalent code segments as. So I think this will be very hard to achieve. The only think I can think of is generation all pdf's without compression, then remove all duplicate objects, and then compress... But... So the answer would be, yes it can be optimized in animate, but only manually. –  zeroth Apr 7 '13 at 13:49
I think PDF is not a good format for these type of improvements. It's just a document not a container after all. –  percusse Apr 7 '13 at 14:27
@zeroth I guess the OP wants to animate bitmap sequences and optimize the PDF file size by using differential bitmaps, as gifsicle does. We can use gifsicle for producing a size-optimzed bitmap sequence to be used here. And the timeline feature is needed, as in your answer; thanks for the link! –  AlexG Apr 8 '13 at 10:53
@AlexG yes, that is probably the essence. And of course generating the bitmaps via gifsicle seems like the way to go! :) Great. –  zeroth Apr 8 '13 at 11:03

You can optimize the PDF animation for size using differential bitmaps and the timeline feature of the animate package.

Note however, that with every new animation frame to be shown the number of differential frames that needs to be re-displayed increases by one. This may slow down a running animation as time advances. Adobe Reader was not primarily optimized for graphical rendering speed. For long bitmap sequences, it may therefore be better to produce a video file (MP4/H.264) and embed this one with the media9 package.

If you still want to animate differential bitmap sequences, proceed as follows:

(1) Create a size optimized version of the original animated gif using gifsicle:

gifsicle -O3 animated.gif > optimized.gif


For testing purposes you could try this animated gif from the asymptote web site: http://asymptote.sourceforge.net/gallery/animations/wheel.gif

(2) Produce a PNG sequence (difference-0.png, difference-1.png, ...) of image differences, using the the ungif.sh script listed below. The script writes the timeline to be used with \animategraphics to standard output which is redirected into the text file timeline.txt:

ungif.sh optimized.gif difference.png > timeline.txt


(3) The PDF with the animated sequence can be produced from the following LaTeX source file (The max. frame number and perhaps the frame rate will need to be adjusted):

\documentclass{article}
\usepackage{animate}

\begin{document}

\noindent\animategraphics[
controls,
width=0.5\linewidth,
timeline=timeline.txt
]{10}{difference-}{0}{99} %adjust the maximum frame number

\end{document}


(4) Before viewing the animation in Adobe Reader, make sure that 'Smooth images' in the Reader settings (Edit->Preferences->Page Display) is un-checked. Otherwise you get ugly artifacts.

Contents of Bash script ungif.sh (requires ImageMagick):

#!/bin/bash

wxh=$(identify -format '%Wx%H'$1[0])
fs=$(identify -format %n$1)

for (( i=0; i<$fs; i++ )) do convert -page$wxh ${1}[$i] -matte -background none -layers coalesce -quality 90 ${2/./-$i.}
echo "::\${i}x0"
done

-
Thank You so much for your clear explanation / description on how to proceed. I create a PDF file 50% smaller but as you pointed out it is too slow and gets slower as the frame count increases. I would like to explore how to generate a video file (MP4/H.264) and embed it with the media9 package (as you suggested above). I hope there is a compression method that keeps the lines sharp without blurring/aliasing/etc for animations such as geogebra.org/forum/… any advice will be appreciated. Thanks again for your help !!! –  Carl Baum Apr 9 '13 at 19:22
@CarlBaum: Firstly, I'd try to export the frames as vectorial graphics files in either EPS or PDF formats (depending on whether latex or pdflatex is used) from within GeoGebra, and animate those with \animategraphics. With vectorial graphics files, text and lines are kept sharp at any magnification and frame rates of 12 FPS should be possible. –  AlexG Apr 10 '13 at 7:10
@CarlBaum: If you want to create and embed a video (AdobeReader on Win or OSX required for viewing), proceed as follows: (1) gifsicle --unoptimize orig.gif > unopt.gif (2) convert unopt.gif frame.png (3) video at 12 FPS: ffmpeg -i frame-%d.png -vf scale="trunc(iw/2)*2:trunc(ih/2)*2" -vcodec libx264 -x264opts keyint=12 -r12 myvideo.mp4 (4) Go to tex.stackexchange.com/a/47116 for how to embed myvideo.mp4 using the media9 package. –  AlexG Apr 10 '13 at 8:38
Alex, Thanks You so much for the instructions. I was able to generate the mp4 and then a pdf without any problems. In my case the size reduction was 1/4 compared with the animate approach, but also the resolution is lower (perhaps half?) which would lead to a 1/4 size ratio. It was good because I learned to include .mp4/etc, but for animated GIF files I will stick with the animate package for resolution, better resizing quality, and also has the controls. After non using latex for many years, I find that these tools have become very sophisticated !!!! Thank You !!! –  Carl Baum Apr 11 '13 at 16:43
@CarlBaum: As for size, you can arbitrarily scale your video (\includemedia of media9) or animation (\animategraphics) via width and height options of these commands. Video resolution depends on the tool you are using. With ffmpeg, using the command line in my previous comment, the resolution is the same as of the input PNG sequence. In case you want to stick with the animate approach, consider animating vector graphics rather than bitmaps. You will probably get smaller files, better quality and higher frame rates. Look for PDF or EPS export in GeoGebra. –  AlexG Apr 11 '13 at 19:02