145

I'm very new to presentations in LaTeX (I've been using PowerPoint for years but have finally converted).

I am wondering how to get a GIF into a section. If need be, I can convert a GIF into lots of pictures and have them overlay to give the impression of movement.

epR8GaYuh
  • 2,432
iluvLatex
  • 1,513

3 Answers3

145

1. Convert and split animated GIF into PNG sequence

convert -coalesce something.gif something.png

or

magick convert -coalesce something.gif something.png

convert/magick convert is a command line tool from the ImageMagick.org software package. The command name depends on the software version.

This produces a set of numbered PNG files something-0.png, ..., something-16.png (Here, the original GIF https://i.stack.imgur.com/VHJmL.gif, renamed to something.gif consists of 17 frames.) Option -coalesce is necessary to undo a possible optimization of the original GIF file.

2. Get original animation speed

magick identify -verbose something.gif | grep 'Delay'

Users of Windows may want to run

magick identify -verbose something.gif | sls -Pattern 'Delay'

in the PowerShell.

This outputs lines (one for every frame) like:

Delay: 10x100
Delay: 10x100
Delay: 10x100
...

The frame rate (frames per second), to be passed as an argument to the \animategraphics command below, is found by dividing the number after x by the number in front of it:

frame rate = 100 (tics/s) / 10 (tics/frame) = 10 frames/s

3. Embed PNG sequence as an embedded looping animation in the final PDF

(This kind of animation requires a JavaScript-supporting PDF viewer, such as Acrobat Reader or KDE Okular.)

\documentclass{beamer}
\usepackage{animate}

\begin{document} \begin{frame}{Embedded Animation} \animategraphics[loop,controls,width=\linewidth]{10}{something-}{0}{16} \end{frame} \end{document}

Argument {10} sets the desired frame rate (frames per second), {0} and {16} set the first and last file numbers of the PNG series to be included in the animation. Note that frame rates above 30 FPS, if at all achieved by the PDF viewer, do not make much sense. 30 FPS is a typical value in video encoding. Use command option measure and the + button to see which frame rates are possible. They may depend on the image size and of course on the hardware on which the PDF viewer is running.

For a more GIF-like impression, option autoplay can be used instead of or in addition to option controls.

enter image description here

AlexG
  • 54,894
  • I am unsure what gifsicle --unoptimize VHJmL.gif > something.gif convert something.gif something.png does or how to use it. – iluvLatex Apr 24 '15 at 15:21
  • convert -coalesce ... does the same like gifsicle --unoptimize .... Thus, gifsicle is not needed. – AlexG Apr 24 '15 at 15:24
  • @AlexG Thanks! Learned something new today! Than sorry for my misleading comment! -> I deleted them – samcarter_is_at_topanswers.xyz Apr 29 '15 at 16:08
  • I don't see the controls in the pdf. There is a hand cursor, but clicking does nothing. – user4417 Mar 22 '16 at 02:33
  • I have the latest Adobe Reader DC. – user4417 Mar 22 '16 at 10:32
  • 2
    @user4417 You will also need the most recent animate package version 2016/03/15 . – AlexG Mar 22 '16 at 11:11
  • What is the {12} for? – AnnanFay May 31 '17 at 13:30
  • @Annan : Frames per second. – AlexG May 31 '17 at 13:31
  • Does LaTeX support APNG (Animated PNG)? – Royi Aug 28 '17 at 15:46
  • 5
    @Royi The question is rather: Does the PDF standard support APNG? The answer is 'No'. You must convert it into a series of images and use one of the suggested methods (multipage PDF or animate-based animation). – AlexG Aug 28 '17 at 15:55
  • @AlexG, I see. Is there a reason it doesn't support? Is there a way to change that (Namely let those who set the standard know)? It is really limiting that PDF doesn't support APNG & videos out of the box. – Royi Aug 28 '17 at 16:00
  • 3
    The PDF format and its viewer are Adobe inventions. I think they still define and extend the standard, although PDF is now an ISO standard. – AlexG Aug 28 '17 at 16:07
  • 3
    For someone else who stumbles upon this: The {0}{16} describes the first and last frame of the animation. You'll need to identify something.gif to determine how many frames are in the gif – inspectorG4dget Sep 18 '17 at 17:45
  • in Ubuntu 16.04: mogrify -identify -verbose something.gif | grep 'Delay' – Joris Bierkens Mar 09 '18 at 17:49
  • 1
    Another observation: identify -verbose something.gif | grep 'Delay' is enough. In fact, prepending magick causes an error. – Francisco Escobar Jul 02 '18 at 17:09
  • Thanks for the feedback. As said, it depends on ImageMagick's version. For me both work. – AlexG Jul 03 '18 at 07:00
  • magick identify -verbose something.gif | sls -Pattern 'Delay' doesn't work for me on Win10 with the last version of imagemagick. So, would you please edit your answer to calculate the framerate based on the delay that can be obtained as explained in this answer. – Diaa Oct 05 '19 at 10:45
  • A true operating system independent way of getting ImageMagick is with docker. I used this container: https://hub.docker.com/r/dpokidov/imagemagick/. You don't even really need to understand docker to use it for this example. – marvin Mar 19 '20 at 13:56
  • @Diaa Command line as given works for me. – AlexG Sep 10 '20 at 14:15
  • 1
    It doesn't work. I got one picture on a slide and it is not showing any animation in Okular. Using Ubuntu 20.04. – praveen pathak Jun 30 '21 at 04:18
  • Edit: It runs smoothly in Adobe reader in windows but then I will have to install adobe (2013 version, vulnerable) or use wine acrordrdc (display issues reported). – praveen pathak Jun 30 '21 at 05:04
  • 1
    @praveenpathak : Works for me: Okular-21.04.2 with Poppler-21.05.0 . Check the Okular and Poppler versions. The OS version you mentioned doesn't tell anything about the PDF viewer version you are using. – AlexG Jun 30 '21 at 09:54
  • @praveenpathak see edit: image replaced with Okular example. – AlexG Jun 30 '21 at 10:50
  • @AlexG Okular version is 1.9.3 (21.04). I dont know how to check Poppler version. But I converted gif in mp4 and it is working properly in pdfpc and Pympress. – praveen pathak Jul 01 '21 at 03:25
  • Are there free online websites that can convert these set of images to a javascript supporting pdf viewer? – B E I R U T Jul 18 '21 at 10:17
  • I would do the conversion locally, with ImageMagick installed. On Windows, enter cmd.exe in the search/execute bar somewhere in the Start menue to open a command line window. – AlexG Jul 18 '21 at 13:05
  • 1
    As of today (2021) this does not work on Windows 10 (with Adobre Reader DC or Okular). Clicking on the control bar doesn't launch the animation. – Guillaume Sep 14 '21 at 11:55
  • 1
    Which AR DC version do you use? (Help->About) – AlexG Sep 14 '21 at 12:32
49

Ok, let's start with the .gif from @SeanAllred comment.

First Step: Converting

There are probably many methods to do so. To be independent of the operation system, there are online converter, ask your favourite search engine for suggestions.

Or if you prefer using the command line use convert from ImageMagick:

convert -coalesce VHJmL.gif something.png

Make sure you resulting images are named something-0 and so on.

Second Step: multiinclude

\documentclass{beamer}
\usepackage{xmpmulti}
\begin{document}
%asuming you images are called "something-0.png" up to "something-16.png" 
\begin{frame}
    \transduration<0-16>{0}
    \multiinclude[<+->][format=png, graphics={width=\textwidth}]{something}
\end{frame}

\end{document}

enter image description here

10

Have a look at pdfpc they have an example how to embed videos. pdfpc is a tool for displaying your beamer presentations.

It doesn't work with gifs but you can still convert something.gif something.avi. To get a preview picture run convert something.gif poster.png

Updated example for using the multimedia package now recommended by pdfpc:

\documentclass{beamer}
\usepackage{multimedia}

\begin{document} \frame{ \movie{\includegraphics{poster.png}}{movie.avi} } \end{document}

Display your presentation with pdfpc my-presentation.pdf

In earlier versions it didn't seem to work if your video was in another directory set by \graphicspath.

Benjamin
  • 499
  • 7
  • 17