HOWTO: Creating MPEG movies from PNM frames
There are many ways to create MPEG files from the PNM frames generated by Gazebo. One of the easiest is to use theppmtompeg
program that is part of the Netpbm software package and included by default in many distributions.Some MPEG basics
During MPEG compression each frame is divided into slices that are separately compressed. The frame is then encoded as an I, P or B frame. I frames are independent frames, where the P frames are given as the difference to the preceding P or I frame and the B frames as the difference to both the preceding and following I or P frame. Several frames are grouped into GOPs (Group of Pictures) that correspond to independently decoded sequences. Each such GOP starts with an I frame.Using ppmtompeg
The ppmtompeg program parses a parameter file that contains all the information pertaining to the creation of a single MPEG sequence. The syntax of this file is relatively simple. The contents of a typical file are included below followed by a basic line by line explanation.
OUTPUT movie.mpeg INPUT_DIR . INPUT *.pnm [0100-0500] END_INPUT BASE_FILE_FORMAT PNM INPUT_CONVERT * FRAME_RATE 25 PATTERN IBBPBBPBBPBBPBB SLICES_PER_FRAME 16 GOP_SIZE 30 PIXEL HALF IQSCALE 1 PQSCALE 5 BQSCALE 10 RANGE 5 PSEARCH_ALG TWOLEVEL BSEARCH_ALG CROSS2 REFERENCE_FRAME DECODED
The first line specifies the output file, which in this case is movie.mpeg. The INPUT_DIR
specifies the directory were the frames reside, in this case is the same as the parameter file.
The files used to create the MPEG sequence are between the INPUT
and END_INPUT
tags. It can contain individual files or a range of files. In this case the input contains the files from 0100.pnm to 0500.pnm. The BASE_FILE_FORMAT
specifies the format of the files and the INPUT_CONVERT
is used when the files are not in PNM format already. Here no conversion is necessary.
Next the frame rate is defined as 25 fps, followed by the PATTERN
option. This option specifies the sequence of I, B and P frames. Many I frames usually means better picture quality at the expense of bigger file sizes. The GOP_SIZE
usually is a multiple of the PATTERN size, but not necessarily. The SLICES_PER_FRAME
defines the number of slices that each frame will be divided into to be compressed. Larger number of this option result in larger file sizes with little to no effect in picture quality.
The rest of the options refer to the picture quality and the compression algorithms used.
Creating the MPEG file
To create the MPEG file it is sufficient to create a param file with the contents above in the directory were the frames reside and issue the following command:
$ ppmtompeg param
Resizing the video, adding a watermark, etc.
It is possible to take advantage of theINPUT_CONVERT
option and the Imagemagick program to resize the video, add watermarks or create other effects.The following line in the parameter file will resize all the frames to 320x240 before creating the sequence. This will not affect the files existing on disk.
INPUT_CONVERT convert -size 320x240 * -
The following line will add a caption "My text" in white letters near the top left corner of the MPEG video.
INPUT_CONVERT convert -font arial -fill white -pointsize 18 -draw 'text 10,20 "My text"' * -
Although there can be only one INPUT_CONVERT option, it is possible to combine the above two effects in a single command
INPUT_CONVERT convert -size 320x240 -font arial -fill white -pointsize 18 -draw 'text 10,20 "My text"' * -
More information
For extended descriptions of all the options offered by ppmtompeg, check the program's manual page.For more information on the operations available with the convert program of the Imagemagick package check http://www.imagemagick.org/script/convert.php.