Arsip untuk Tutorial kategori

Have Fun with MATLAB

Posted in Tutorial on Juni 15, 2008 by dudiek

MAKING_MOVIES

Creating AVI Movies from MATLAB images


Here are a couple of examples of how to use MATLAB to make Audio Video Interleaved (AVI) movies on Linux. The resulting movies can be played independently of MATLAB. On Linux, for instance, there is the free XINE AVI movie player, and on a PC, there are Windows Media Player or QuickTime.

If you are happy just playing a movie within MATLAB then see

creating movies

in the MATLAB help pages. Note that a number of web-sites recommend using the free ‘mpgwrite’ software to convert a Matlab movie to an MPEG movie, but this doesn’t appear to work with Linux anymore (the system crashes).

Here is an example of the animation of a simple series of sine wave graphs, each created with the plot command.

In the second example, the graphic image is 3D, composed by rotating a surface. The AVI movie takes much longer to create, and contains 100 frames:

Both these examples should be easily adapted to make other movies, for example, replacing the ’surf’ command with ‘pcolor’ to create flat pseudocolor plots. A problem is that AVI movie files can be very large. A possible solution to this problem is to compress the AVI files, which requires additional software. Alternatively, convert your AVI file to MPEG or MP4 format using QuickTime Player (‘Pro’) on Windows. This can also be problematic (e.g., the system crashes!), so an alternative approach is to do the conversion within Linux using FFMPEG, a command line tool to convert one video file format to another. FFMPEG is currently threatened by software patents, see http://ffmpeg.sourceforge.net/index.php for further details and download instructions. Once FFMPEG is installed in your root directory, to convert a file from AVI to, say MP4 format, simple type at a command line

ffmpeg -i myfile.avi myfile.mp4

The general syntax is of the form

ffmpeg [[infile options][`-i' infile]]... {[outfile options] outfile}... To see a list of options type ‘ffmpeg -h’ at the command window, for example, you could also override the avi header’s default rate of 25 frames/second to 5 frames/second by using the -r flag like so: ffmpeg -r 5 -i myfile.avi myfile.mp4 (output parameters are by default the same as input parameters). Alternatively, you could override the default frame-size of 160×128 (width by height) to a frame-size of 128×96 by using the -s flag as follows: ffmpeg -s 128x96 -i myfile.avi myfile.mp4 A useful option is ‘-sameq’, which leads to (or attempts) to create the same video quality as source, e.g.: ffmpeg -sameq -i myfile.avi myfile.mp4 (with however, the cost of a larger file size). Finally, it should be mentioned that we can convert to other file formats too. If there is no sound to the movie (no audio component, just video) then the MPEG format is appropriate: ffmpeg -i myfile.avi myfile.mpeg

However, it can still be useful to convert to MP4 even if there is no audio as the FFMPEG facility can’t convert to MPEG if the frames/second is not equal to 25 (and MP4 seems to play just as well as MPEG). Additional instructions and option can also be found at the web-page http://ffmpeg.sourceforge.net/ffmpeg-doc.html

Some brief notes regarding the use of the Linux movie player XINE are given here. After XINE has been installed on your machine follow these steps after creating your AVI movie in Matlab:

  1. in a terminal window, type ‘xine’ (Enter);
  2. Right click mouse, then select open – file;
  3. Choose directory – filename, and the appropriate filter to show only AVI files;
  4. Click on the desired AVI file;
  5. Click on Select.

Once a file has been selected, it can be played quickly via

Right click mouse – Playback – Play

See the menu list for additional options (e.g. to control the speed at which the movie is played). Note that the Linux movie player MPLAYER will also play AVI, MP4, and MPEG movies.

For further Information please contact: Marcus R. Garvie (garvie@csit.fsu.edu)