Wednesday, December 5, 2012

Animation tutorial


Computer animation in C++ made easy.


Step 1. Write a sequence of frames to separate image files.
Step 2. Concatenate the frames into a video file.

No, there's no audio here.


For the first step, it is convenient to use a library such as pngwriter that takes care of storing the image in the correct file format  (however, this is neither the most flexible nor the most efficient solution). The frames should be named with an increasing series of numbers, e.g. img001.png, img002.png, img003.png and so on.

Then the frames can be converted to video using ffmpeg. Supposing the images reside in the folder imgfolder, a command like

ffmpeg -f image2 -r 25 -i imgfolder/img_%2d.png animfilm.avi

should do it. There are many other parameters to set, some of which need to be tweaked in order to obtain an acceptable quality. For the C++ program that generates the frames, the main loop may look somewhat as follows. (Note that these lines of code generate something slightly different than the video example above.)


int main()
{
const int W=720, H=576;
pngwriter pict(W, H, 0.0, "imgfolder/img.png");
char fname[32];
int i=0;
const int N = 100;
while(i++ < N)
 {
 float t = (float) i/N;
 pict.line(0, 2*N-i, W, 2*N-i/2, 0.5, 0.5, t);
 sprintf(fname, "imgfolder/img%03d.png", i);
 pict.pngwriter_rename(fname);
 pict.write_png();
}
pict.close();
}



To add audio to all of this (but why should you want to?), mux the video file with an audio file in ffmpeg.





ffmpeg -i soundfile.ogg -i animation.avi -acodec copy -vcodec copy muxedfilm.avi



Following all these steps, you have disqualified yourself to contribute to the monochrome video contest (black only, and no sound track).

No comments:

Post a Comment

I'm not home right now, but please leave a message and I'll get back to you in the next few years.