Create Animations with Inkscape, ImageMagick and FFmpeg
One way to create animations in Linux is to use the GIMP Animation Package (GAP). It allows you to create each GIMP layer as a frame and export it as an animation. This is great, but what if you wanted the flexibility of vector graphics while designing your frames? That's where this tutorial comes in. You can use Inkscape to create the frames, ImageMagick to stitch them together into a gif animation, and ffmpeg (or avconv) to create a video from them.
So let's begin.
You will need the following:
- Inkscape for creating frames.
- ImageMagick suite to stitch frames into an animation.
- FFmpeg for creating a video animation
You will also also need a Linux or UNIX based operating system, any where all of these programs work, and a lack of fear of the command line.
1. Creating frames
Drawing frames in a vector graphics program such as Inkscape gives you some amount of extra flexibility, and makes it easier and quicker to draw cool things. As we go along we'll use my example, but your animation can be anything you can imagine.
First we need to create a background of the size which we want our final animation to be. Let's make it 220x180 px here. Just draw a square, select it and in your "W" (width) and "H" (height) areas enter "220.000" and "180.000" respectively. The ".000" are to make sure that it is exactly of that size.
You can make the background of any color or gradient you wish. Here is mine.
It's probably a good idea to export this square as the first frame of your animation. It is best to create a folder in a convenient place called something like "animation", and export all your frames there as files named by a number. So the first frame can be a frame "0" and should be exported as 0.png.
To do an export click on the square and go to File > Export bitmap. In the dialog that pops up make sure a "selection" tab is pressed and use the browse button to browse to the location you wish to save your frames in (that would be the animation folder mentioned above) and the file name you wish to save it under (0.png). Once you're done with the browse dialog click the "export" button.
Treat the background square just created as a stage. Anything that is positioned within that square is what will appear in the animation. That said, now we have to introduce the object that we want to be in the animation first which can be an image, text, a drawing etc. You can create this object by drawing or typing it yourself or you can import an existing one.
For this animation let's start with a blue circle without fill color and with a fairly fat stroke paint (border), like this:
Great. Now what I would like to do in this animation is for this circle to draw itself. The way we can accomplish that is by selecting the circle and switch to the "Edit paths by node" mode (second button on the left, or accessible by pressing F2). This will allow you to "undraw" the circle, so to speak, make only a desired part of it display. See the following image to get the idea.
"Un-drawing" it is basically setting it to the beginning position, where the circle is either not visible at all or only a very small part is visible. Export this as the next frame using the exact same process as above for the background square. Even this time, as for every other frame, it is the background square that needs to be selected as you are exporting your frame. Then everything that is within it will be exported as too.
I think you might already be getting the idea of where this is going. For the next frame you reveal a little more of the circle, export as frame 2.png and then draw a little more again and export as frame 3.png and so on until the full circle is complete. The less you reveal per step the more steps or frames it takes for your circle to be drawn and the smoother will its final animation be.
To make it less daunting though, you can leave the export bitmap dialog open all the while you're exporting, and then just change the number in the file name and hit export every time you make a modification to the circle (and reselect the background).
In this example, I'm going to make a fade in effect using Inkscape's opacity feature. Here's a next object we have here:
Shiny! What we're to do now is superimpose this shiny element over the now completed circle to get this:
Then select it, open "Fill and stroke" settings (Object > Fill and Stroke) and move the opacity slider to near zero and then export the frame as above again, continuing to count numbers in file names. So the idea is to move a slider a little further towards 100% and export as next frame.
So far we have a circle drawing itself and then starting to glow. Now you can write something in there, like your name or whatever you wish. I will write "Nuxified". You can use the same fade in effect with your text or you can make the typing effect by writing the first letter, exporting the frame, then the second, and so on. So, for example, first frame would be "N", then "Nu", then "Nux", then "Nuxi" etc.
The result of all this frame making and exporting should be a folder full of PNG files named from 0.png to XX.png, depending on how many you ended up making. The next step is to just put them together into an actual animation.
2. Sticking the frames together into an animation.
Open a terminal and go into the directory where your frames are. For example if your frames are in a folder called "animation" inside your home folder, just type this:
Assuming you have the ImageMagick package installed all you need to do now is type something like this:
animate -coalesce -delay 9 0.png 1.png 2.png 3.png 4.png 5.png 6.png 7.png 8.png 9.png 10.png 11.png 12.png 13.png 14.png 15.png -delay 45 16.png -delay 9 17.png 18.png 19.png 20.png 21.png 22.png 23.png 24.png 25.png 26.png 27.png 28.png 29.png 30.png 31.png -delay 90 32.png
The -coalesce option is what sticks together the frame images specified. The -delay option sets how long should it pause between each frame, therefore setting the actual speed of the animation. Above it is set to "9", but you can experiment with your own values. After that we are specifying the frame files we want to stick together into an animation. This is where it helps that we have all the files named by a number. You can use tab completion to string them together faster, and if you are trying multiple times you can use the up arrow to retry.
If the command is too big you can even select, copy and paste it into a text file where you can more easily tamper with it and then paste it back into the terminal for exection. You can even make it into a script, in which case you'd basically just have to save it as a filename.sh, make it executable (
chmod +x filename.sh and execute it as
./filename.sh (assuming you're executing it from the same directory).
Also notice the -delay 45 option before 16.png, -delay 9 after it, and -delay 90 before 32.png. This makes the frame 16 display five times longer, and the frame 32 ten times longer than the rest, which are set to -delay 9. The reason why -delay 9 displays again after 16.png is to return the delay setting back to 9 since the command will follow the last set delay. This allows you to control the flow of your animation.
An alternative way to control the flow is to just repeat a single frame multiple times. If you repeat the frame 16.png five times it will amount to -delay 45. This may in fact be a good idea if you want to convert this GIF into a video, as explained below, because then when you convert the GIF back to PNG it will instantly output each of the repeating frames as a new frame (in this example a total of 45) that can then be easily put together into a video. So this is what such a command in this example would look like:
animate -coalesce -delay 9 0.png 1.png 2.png 3.png 4.png 5.png 6.png 7.png 8.png 9.png 10.png 11.png 12.png 13.png 14.png 15.png 16.png 16.png 16.png 16.png 16.png 17.png 18.png 19.png 20.png 21.png 22.png 23.png 24.png 25.png 26.png 27.png 28.png 29.png 30.png 31.png 32.png 32.png 32.png 32.png 32.png 32.png 32.png 32.png 32.png 32.png
Whichever command you choose to run, once you run it, after a little while it will show the final animation in a window. To save it as a GIF file just click on that window and an ImageMagick window menu will appear.
Click animate and choose "save", and in the browse window that appears select location where to save, and the file name, and then click the "format" button to bring up the format selection dialog.
Choose GIF and click select. Make sure your file name ends in .gif and then click save. ImageMagick should render the GIF animation to the location you specified.
And here it is:
3. Converting to video
Finally, we can use the same frames we've created to make a video of the same animation. Then it can be used as a piece of a larger video. In this example the animation is basically a simple logo, so we can imagine it being shown at the beginning or the end of a larger video presentation.
To stitch these PNG files into a video we can use this ffmpeg command in the same directory:
ffmpeg -r 24 -i %01d.png -an nuxified.avi
The -r 24 option sets the frame rate (hence the -r), and 24 stands for 24 frames per second (FPS), which means that 24 of our 33 frames (including 0.png) will be displayed in one second. The -i represents the input location. The -an means there's no audio to be added. And the %01d within the file name just means it will match files 0.png to 32.png whereas if it was set to %02d it would match 00.png to 32.png (2 numerical places in the file name).
You might notice that this doesn't set different delays to different frames like in the gif file. A way to get around that is to use the second animate command above where we create the GIF, in which we repeat frames rather than adding custom delays to them, and then use the following command to split the GIF back into PNG files:
convert nuxified.gif nuxified%02d.png
This will output each of the repeating frames as its own PNG file, in this example resulting in 45 PNG files rather than only 32 that there were initially. That's because now the repeating frames from the GIF have their own file. Now we can run the command to create a video out of those new PNG files:
ffmpeg -r 24 -i %02d.png -an nuxified.avi
And there it goes, a video with the built in delays we wanted!
Now you can use some video editing tool to add sound to it or even put it in a larger video that you might be making. For instance, the first half of this video was done using the exact method described here. It was merged together with other parts using kdenlive. Other great video editing tools include PiTiVi and OpenShot.
Updated and republished on June 1. 2014.