About SP Initiatives Software

  / gears / sp / software / opendx / tutorials



Past Projects

Tutorials

How to Make MPEG Movies on Unix Workstations

First off, what's an MPEG?

MPEG (Moving Pictures Experts Group) is a group of people that meet under ISO (the International Standards Organization) to generate standards for digital video (sequences of images in time) and audio compression. In particular, they define a compressed bit stream, which implicitly defines a decompressor. However, the compression algorithms are up to the individual manufacturers, and that is where proprietary advantage is obtained within the scope of a publicly available international standard. MPEG meets roughly four times a year for roughly a week each time. In between meetings, a great deal of work is done by the members, so it doesn't all happen at the meetings. The work is organized and planned at the meetings. MPEG itself is a nickname. The official name is: ISO/IEC JTC1 SC29 WG11.


ISO: International Organization for Standardization

IEC: International Electro-technical Commission

JTC1: Joint Technical Committee 1

SC29: Sub-committee 29

WG11: Work Group 11 (moving pictures with audio)


MPEG has also become a synonym for a digital computer animation. (e.g. -- "Does this site have any neat MPEGs?"). The filename suffix .mpg usually denotes an MPEG-format animation.

You will need the following data and software tools to create an MPEG movie on a Unix workstation:

  • a series of image files containing the individual frames of the animation. A word of caution here -- the mpeg encoding process doesn't deal very well with single-pixel wide lines or other small details. Consequently, computer-generated images of wire-frame objects, animated graphs, etc., won't come out quite like you expected. Natural images, sampled video, and fully-rendered computer images will work quite well.

  • a program -- actually, a collection of programs, NetPBM. Netpbm is a toolkit for conversion of images between a variety of different formats, as well as to allow a few basic image operations. The package is intended to be portable to many platforms. It has been tested under UNIX (BSD and SYSV, e.g. SGI, Sun4, Sun386i, DEC and Apollo DN 3500), VMS and Amiga OS. There are also compiler directives in it for MS-DOS. NetPBM will can convert these images into three separate Y, U and V files (Y-signal, U-signal and V-signal dates back to early color television terminology. The Y component is the luminance, U and V are chrominance.) The mpeg encoding process subsequently works with the luminance information at full resolution, and the chrominance information at reduced resolution (the reason why single-pixel lines don't look right).

  • an MPEG encoder that will assemble all the Y,U,V files into a single animation file (.mpg). A popular, public domain, encoder, Mpeg, and its related documentation, is available on the web (Note: you may want to browse the directories a level up from the converters for other interesting items). There was an MPEG video research group (the Portable Video Research Group) at Stanford University in the early 1990's. I can't find pointers to their work, but copies of it exist elsewhere. Download the PVRG MPEG encoder, CV 1.2.2, a colormap conversion utility that is necessary to use the PVRG encoder, and download a PVRG technical report that describes MPEG-1 video and their software.

  • an MPEG display program to view the results. The most common player for Unix platforms is mpeg_play, also in the public domain and freely available.

NOTE: A new web resource for all things MPEG and related is now available at: www.mpeg.org .

Image data files

The image data files can be in any of the most common image formats (.rle, .sgi, .ppm, .gif, .ps, .tiff, .xwd, .jpg, and .tga) Using available public-domain conversion tools such as netpbm, command lines or scripts can be used to convert them to ppm format and then to YUV files. It is often desirable to scale the image data down to a relatively small size (about 320 pixels by 240 pixels maximum, 160 pixels by 120 pixels preferred, especially if you are intending to provide access to the animations on the Web.) This is done so that the resulting movie will play back at a reasonable speed and be a manageable size. The MPEG encoding process works best with image dimensions that are even multiples of 16.

Converting to YUV

The image data is first converted to a .ppm (portable pixmap file format) format using the appropriate converter for the image data format. For example, if the images are in .gif form, the converter "giftopnm" should be used. In a similar fashion, .tga format images (Targa) would be converted using "tgatoppm". No matter what image format the data is currently in, before sending it to the MPEG encoder, it must be converted into separate Y, U and V files. One way to do this is to use the program ppmtoyuvsplit (which is part of NetPBM.)

Here is an example of a simple script that will process all the .gif image format files in the current directory and create the appropriate Y, U, and V files:

#!/bin/csh -f

foreach file (*.gif)

set outf=$file:r

giftopnm $file | ppmtoyuvsplit $outf

end

MPEG Encoder

The commonly-used MPEG encoder for Unix platforms is mpeg. It process a series of Y,U,V files and assembles an .mpg file as output. The basic form of the mpeg command and options is:

mpeg [-d] [-NTSC] [-CIF] [-QCIF] [-PF] [-NPS] [-MBPS mbps] [-UTC]

     [-XING] [-DMVB] [-MVNT] 

     [-a StartNumber] [-b EndNumber]

     [-h HorizontalSize] [-v VerticalSize]

     [-f FrameInterval] [-g GroupInterval]

     [-4] [-c] [-i MCSearchLimit] [-o] [-p PictureRate]

     [-q Quantization] [-r Target Rate]

     [-s StreamFile] [-x Target Filesize] [-y]

     [-z ComponentFileSuffix i]

     ComponentFilePrefix1 [ComponentFilePrefix2 ComponentFilePrefix3]

As you can see, there are a lot of specifications that require some familiarity with image compression terminology and techniques. A simpler approach is provided by the script, "makempeg" from the folks at the University of Minnesota. The script combines the conversion from image file format to Y,U,V with the necessary mpeg arguments. Basically, you start with a bunch of image files, then invoke makempeg, which does all the work.

makempeg.1

       makempeg  -  create  an  mpeg  movie file from a series of

       image files

SYNOPSIS

       
makempeg  [  -fs   start_frame_number   ]   [   -fe   end-

       ing_frame_number ] [ -fi frame_increment ] [ -base name ]


DESCRIPTION

This command can be used to generate an mpeg movie. Before attempting to run this command, make sure you have the following:

  • a series of image files

  • the program "ppmtoyuvsplit" is in your path

  • sufficient disk space for the temporary files

When run, this command will make two passes over the data. The first pass involves converting the data to YUV format. The second pass takes the YUV data and encodes it into an mpeg stream. The entire process may take a great deal of time depending upon the size and number of images.

OPTIONS

-fs start_frame_number
Use this option to specify the starting image frame number. Default: 1

-fe ending_frame_number
Use this option to specify the ending image frame number. Default: 1

-fi frame_increment
Use this option to specify the frame increment if other than the default of 1.

-base name
Use this option to specify the base name of the image files. Default "image"

EXAMPLES

makempeg -fs 1 -fe 100 -base truck
From the series of image files named truck.0001.rle to truck.0100.rle, make an mpeg movie file named truck.mpg.

IMAGE SIZES

While mpeg movies can be made from images of any size, images whose X and Y dimensions are evenly divisible by 16 are preferred. If the source images are originally sized for the Abekas A60 (720x486) and were generated using Wavefront software, an image size of 384x288 would work well. If the source images are originally sized for the are a couple of examples showing how to resize an image using various commands:


An Abekas A60 image created by Wavefront software:

       fant -S 384 288 old.rle >new.rle

       pnmscale -width 384 -height 288 old.ppm >new.ppm

       izoom old.sgi new.sgi .533 .593


An Abekas A60 image NOT created by Wavefront software:

       fant -S 400 272 old.rle >new.rle

       pnmscale -width 400 -height 272 old.ppm >new.ppm

       izoom old.sgi new.sgi .556 .560

IMAGE FILE FORMAT

This command expects two things of the image files provided:

they must be named using the following convention:

  • base.xxxx.rle

they must be in one of the following image file formats:

  • Utah Raster Toolkit (.rle)

  • SGI RGB Format (.sgi or .rgb)

  • PBMPLUS Format (.ppm)

  • GIF (.gif)

  • XWD (.xwd)

  • JPEG (.jpg)


NOTE: "base" is any name you choose for these image files. "xxxx" is a four digit number padded with zeros. While an mpeg movie file can be made from images of any size, it is wise to keep the images small so that the movie can be played back at a reasonable speed. An image size of about 400x272 is suggested.

Here is an example of how you might convert and scale a series of image files for use with makempeg.

       @ i = 1

       foreach file (*.sgi)

          set  out  =  `echo  $i | awk '{printf "image.%04d.sgi",

       $1}'`

          echo Converting $file to $out...

          izoom $file $out .556 .560

          @ i++

       end


SUFFICIENT DISK SPACE

makempeg generates a lot of temporary files.

AUTHOR

Wesley C. Barris (wesb@msc.edu)
Army High Performance Computing Research Center (AHPCRC)
Minnesota Supercomputer Center, Inc.


Please send questions or suggestions about this web page to: sp@rcc.its.psu.edu

ASET | ITS | Penn State