Mario Valle Web

The AVS/Express movie generation

I use AVS/Express a lot to produce movies on my Linux machine. After some experiment I have found a method to create better quality movies. This effort started from the problems I had with the AVS/Express image_capture module.

The two methods offered by AVS/Express image_capture module have the following problems:

Instead mencoder (found on the MPLAYER page) can create standard MPEG4 compressed movies starting from a sequence of images or the above mentioned nonstandard ZLIB compressed AVI movies.

To create movies compatible with the PC Windows platform mencoder can even use the Microsoft MPEG4 V2 codec found on every Windows machine.

The following suggestions are general and useful also outside AVS/Express usage.

Install and setup mencoder

  1. Download and install mencoder v1.0pre3 from the download page.
  2. To reduce the amount of spurious messages add the following things under the ~/.mplayer directory:
    • create an empty mencoder text file
    • copy any TTF font file here and rename it to subfont.ttf. I have copied arial.ttf font file from a Windows machine
    • copy a font from the MPLAYER distributed fonts (follow the README instructions: there are 5 files to be put in the ~/.mplayer/font subdirectory)
    • there is something more to do regarding Unicode mapping, but don't have figured how to do yet
  3. In the provided scripts (mencoder_zlib.sh and mencoder.sh) adjust the LD_LIBRARY_PATH and PATH definitions to point to the installed mencoder libraries and executable.

Select the codec

Select the coding method you want to use. Basically you have two choices:

mpeg4
Best quality, higher compression and more options, but on Windows requires the installation of the DivX codec (from www.divx.com).
 
msmpeg4v2
The Microsoft MPEG4 V2 codec. On average the files are 10% bigger, but nothing has to be installed on Windows.

How-To instructions (from compressed AVI)

This is the most convenient movie generation mode from AVS/Express on Linux. The recipe is implemented in the mencoder_zlib.sh script:

  1. Set the AVS/Express view dimensions to be multiples of 16. Set the chosen width and height in the script.
  2. Set Capture Mode to Disk or Memory in the AVS/Express image_capture module. The choice depends on the number of frames expected and memory available.
  3. After capture finished, set the Movie Control - Mode to AVI and set the Compress AVI toggle. Select also a suitable output file name (the script uses avs.avi). Then press Generate Movie.
  4. Compute the optimal bitrate and set it into the script. The 50 factor can vary between 40 and 60 to trade quality for size.
    optimal_bitrate = 50 * 25 * width * height / 256
    For a standard 768x576 format the optimal bitrate will be: 2160000
  5. Select the desired coding method by uncommenting the two appropriate lines in the script (see comments). If you want to experiment with the options for the codec check the mencoder documentation. Change 2160000 to the optimal bit rate computed above.
  6. Remove a frameno.avi movie if present. It can interfere with the two pass compression.
  7. Compress the frames. I use two pass encoding for better compression and quality.
    For the msmpeg4v2 codec:
    opt="vbitrate=2160000:mbd=2:keyint=132:vqblur=1.0:cmp=2:subcmp=2:dia=2:mv0:last_pred=3"
    
    mencoder -ovc lavc -lavcopts vcodec=msmpeg4v2:vpass=1:$opt -nosound -o /dev/null avs.avi
    mencoder -ovc lavc -lavcopts vcodec=msmpeg4v2:vpass=2:$opt -nosound -o output.avi avs.avi
    
    For the mpeg4 codec:
    opt="vbitrate=2160000:mbd=2:keyint=132:v4mv:vqmin=3:lumi_mask=0.07:dark_mask=0.2:scplx_mask=0.1:tcplx_mask=0.1:naq"
    
    mencoder -ovc lavc -lavcopts vcodec=mpeg4:vpass=1:$opt -nosound -o /dev/null avs.avi
    mencoder -ovc lavc -lavcopts vcodec=mpeg4:vpass=2:$opt -nosound -o output.avi avs.avi
    
  8. Remove the parameter file created by the two pass compression. Then clear the saved frames from image_capture.
    rm -f divx2pass.log
  9. Enjoy the created output.avi movie! On Linux you can use mplayer to play it. Its quality is far better than xine (another Linux AVI player).

How-To instructions (from image sequence)

Here is the recipe implemented in the mencoder.sh script:

  1. Set the AVS/Express view dimensions to be multiples of 16
  2. Set Capture Mode to Disk in the AVS/Express image_capture module. Set also a suitable empty directory as Temp File Dir
  3. After capture finished, go to the Temp File Dir and convert the frame images to SGI RGB format with ImageMagik convert command. For example:
    convert AVS:Img0000.x Img0000.sgi
    The SGI RGB format has been selected because it is not compressed and does not fail for a mencoder bug that affects the PNG format.
  4. Compute the optimal bitrate. The 50 factor can vary between 40 and 60 to trade quality for size.
    optimal_bitrate = 50 * 25 * width * height / 256
    For a standard 768x576 format the optimal bitrate will be: 2160000
  5. Set the options for the codec (check the mencoder documentation for the various options). Change 2160000 to the optimal bit rate computed above.
    For the mpeg4 codec:
    opt="vbitrate=2160000:mbd=2:keyint=132:v4mv:vqmin=3:vlelim=-4:vcelim=7:lumi_mask=0.07:dark_mask=0.10:\
         naq:vqcomp=0.7:vqblur=0.2:mpeg_quant"
    For scientific visualization movies (lot of hard edges) I have found interesting also the following set of parameters:
    opt="vbitrate=2160000:mbd=2:keyint=132:v4mv:vqmin=3:lumi_mask=0.07:dark_mask=0.2:mpeg_quant:scplx_mask=0.1:\
         tcplx_mask=0.1:naq"
    Anyway, I plan to test various sets of parameters to find the best one at least on common (for me) cases.
    For the msmpeg4v2 codec:
    opt="vbitrate=2160000:mbd=2:keyint=132:vqblur=1.0:cmp=2:subcmp=2:dia=2:mv0:last_pred=3"
  6. Remove a frameno.avi movie if present. It can interfere with the two pass compression.
  7. Compress the frames. I use two pass encoding for better compression and quality.
    For the msmpeg4v2 codec:
    mencoder -ovc lavc -lavcopts vcodec=msmpeg4v2:vpass=1:$opt -mf type=sgi:fps=25 -nosound -o /dev/null mf://\*.sgi
    mencoder -ovc lavc -lavcopts vcodec=msmpeg4v2:vpass=2:$opt -mf type=sgi:fps=25 -nosound -o output.avi mf://\*.sgi
    
    For the mpeg4 codec:
    mencoder -ovc lavc -lavcopts vcodec=mpeg4:vpass=1:$opt -mf type=sgi:fps=25 -nosound -o /dev/null mf://\*.sgi
    mencoder -ovc lavc -lavcopts vcodec=mpeg4:vpass=2:$opt -mf type=sgi:fps=25 -nosound -o output.avi mf://\*.sgi
    
    To change the movie speed change the fps=25 parameter
  8. Remove the converted Imag*.sgi files and the parameter file created by the two pass compression. Then clear the saved frames from image_capture.
    rm -f divx2pass.log Imag*.sgi
  9. Enjoy the created output.avi movie!

Supporting info

Other useful suggestions on how to create and compress movies with mencoder can be found here:

Much more experimentation should be done, but for now it is a good starting point, at least for me. Please let me know if you have suggestions or more experiences.