This documentation is for the old version. Go to the latest Graphics Mill docs

Writing AVI Files

Another nice feature of AVI Processor Add-on for Graphics Mill is a possibility to create new AVI files. Being highly integrated with Graphics Mill for .NET, AVI Processor Add-on can compose video data from instances of Aurigma.GraphicsMill.Bitmap class. This way you can use all power of Graphics Mill for .NET imaging features to create and pre-process video frames.

Class Structure

The main class you should use to create AVI files with AVI Processor Add-on is Aurigma.GraphicsMill.Codecs.AviWriter. Further, this class will be referred as AVI writer or merely - writer. It can be interpreted as a write-only collection: using the AddFrame(IFrame) method you put objects which implements the Aurigma.GraphicsMill.Codecs.IFrame interface (i.e. frames).

To add an audio data to the writer, an audio manager represented by the Aurigma.GraphicsMill.Codecs.AviAudioManager class is used. It enables you to add audio streams to the video movie. You can read about audio managers in the Working with Audio Track topic.

Also, the writer can use another handy class Aurigma.GraphicsMill.Codecs.AviWatermark which enables you to put watermarks on frames you add to the writer. You can access it through the Watermark property. Read more detailed about watermarks in the Working with Watermarks topic.

If you want to put transition effects between some frames, you should set the AviFrame.Transition property. Read more detailed about this in the Watermark property. Read more detailed about watermarks in the Working with Watermarks topic.

How to Use AVI Writer

The writer object usage can be described with the following steps:

  1. Create new writer object instance.
  2. Initialize its parameters such as compression type, dimensions, frames per second, etc.
  3. Open the writer object.
  4. Add frames to the writer.
  5. If necessary, add audio streams.
  6. Close the writer object.
Important

The order of actions listed above is important. E.g. if you open the writer object (using the Open method) before you initialize, say the compression type (using the CompressorHandler property), any attempts to change these settings will be disregarded.

Also, if you try to add audio data before you add all frames, you may get it truncated (depending on parameters). See the Working with Audio Track topic for more information about it.

Paragraphs below describe how to initialize parameters and add frames more detailed.

Initialize AVI Writer

After you create an instance of the writer object, you should specify the parameters which will be used to create an AVI file. Each parameter is configured by an appropriate property of the writer object. In particular:

  • CompressorHandler property - specifies a code of VfW compressor (FOURCC converted to integer) which should be used to encode the AVI file. An appropriate compressor should be installed on the machine. You can check what compressors are installed as described in the Working with Compressors topic.
  • Width and Height properties - specify dimensions of the result AVI file. If you omit some of these parameters, it will be taken from the first frame you put to the writer.
  • ResizeFrame property - specifies whether to resize all frames to values specified by Width and Height properties. If this value is set to false, frames are cropped to fit these dimensions.
  • FramesPerSecond property - specifies how fast the player should play the AVI file (a number of frames which should be displayed per one second).
  • Quality property - when the compressor implements some lossy algorithm, specifies a quality/size ratio (in range from 0 to 10000).
  • KeyFrameRate and BytesPerSecond properties - specify key frame rate and bytes per second parameters for MPEG-based compressors (disregarded by all other compressors).

The code snippet below demonstrate how to initialize the writer:

Visual Basic
Dim writer As New Aurigma.GraphicsMill.Codecs.AviWriter

' Specify dimensions and force all frames to be resized. 
writer.Width = 320
writer.Height = 200
writer.ResizeFrame = True

' Specify MPEG4 by XVID compression. Note, you should make sure that 
' XVID is available on the machine where this code is running. 
writer.CompressorHandler = Aurigma.GraphicsMill.Codecs.AviCompressor.XvidMpeg4

' Set MPEG4-related settings:
writer.KeyFrameRate = 160
writer.BytesPerSecond = 128

writer.FramesPerSecond = 24

writer.Open("c:\temp\avioutput\temp.avi")

' ... all other code is skipped for brevity ...
C#
Aurigma.GraphicsMill.Codecs.AviWriter writer = 
    new Aurigma.GraphicsMill.Codecs.AviWriter();

// Specify dimensions and force all frames to be resized. 
writer.Width = 320;
writer.Height = 200;
writer.ResizeFrame = true;

// Specify MPEG4 by XVID Compressor. Note, you should make sure that 
// XVID is available on the machine where this code is running. 
writer.CompressorHandler = Aurigma.GraphicsMill.Codecs.AviCompressor.XvidMpeg4;   

// Set MPEG4-related settings:
writer.KeyFrameRate = 160;
writer.BytesPerSecond = 128;

writer.FramesPerSecond = 24;

writer.Open(@"c:\temp\avioutput\temp.avi");

// ... all other code is skipped for brevity ...

You can have AVI Processor Add-on to open a dialog associated with a compressor so that the user could fill all parameters there. Use ShowOptionDialog and OptionsDialogParentHandle properties to do it. If you run this code:

Visual Basic
Dim writer As New Aurigma.GraphicsMill.Codecs.AviWriter

' Settings which you will you set here are used as default 
' values on the dialog
writer.CompressorHandler = Aurigma.GraphicsMill.Codecs.AviCompressor.MsH263
writer.Quality = 8000

' These two lines make AVI Processor to display the dialog
' If you do not initialize the parent handle, the dialog will be 
' displayed as an independent window (it will be visible in Task Bar).
writer.ShowOptionDialog = True
writer.OptionsDialogParentHandle = Form1.Handle

writer.Open("C:\Temp\writer.avi")

writer.AddFrame(New Aurigma.GraphicsMill.Codecs.AviFrame( _
    New Aurigma.GraphicsMill.Bitmap("c:\animated.gif")))

' ... other frames are skipped for brevity ...

writer.Close()
C#
Aurigma.GraphicsMill.Codecs.AviWriter writer = 
    new Aurigma.GraphicsMill.Codecs.AviWriter();

// Settings which you will set here are used as default 
// values on the dialog. 
writer.CompressorHandler = Aurigma.GraphicsMill.Codecs.AviCompressor.MSVideo1;
writer.Quality = 9000;

// These two lines make AVI Processor to display the dialog
// If you do not initialize the parent handle, the dialog will be 
// displayed as an independent window (it will be visible in Task Bar).
writer.ShowOptionDialog = true;
writer.OptionsDialogParentHandle = form1.Handle;

writer.Open(@"C:\Temp\writer.avi");

writer.AddFrame(new Aurigma.GraphicsMill.Codecs.AviFrame( 
    new Aurigma.GraphicsMill.Bitmap(@"c:\animated.gif")));

// ... other frames are skipped for brevity ...

writer.Close();

the following dialog will be opened:

Compressor dialog screenshot

Add Frames

As soon as you initialize the writer and call the Open method to get it opened, you can add frames. To do it, you should call the AddFrame(IFrame) method.

This method takes an object which implements Aurigma.GraphicsMill.Codecs.IFrame interface. You can get a frame in two ways:

  • Create new Aurigma.GraphicsMill.Codecs.AviFrame object and put the bitmap (an instance of Aurigma.GraphicsMill.Bitmap class) into it. You can do it in the following way:

    Visual Basic
    ' Load the image.jpg file and initialize a frame object with it.
    Dim bitmap As New Aurigma.GraphicsMill.Bitmap("c:\image.jpg")
    Dim frame As New Aurigma.GraphicsMill.Codecs.AviFrame(bitmap)
    
    ' Put this frame to the writer object (it is assumed that the 
    ' writer is properly initialized and opened). 
    writer.AddFrame(frame)
    
    C#
    // Load the image.jpg file and initialize a frame object with it.
    Aurigma.GraphicsMill.Bitmap bitmap = 
        new Aurigma.GraphicsMill.Bitmap(@"c:\image.jpg");
    Aurigma.GraphicsMill.Codecs.AviFrame frame = 
        new Aurigma.GraphicsMill.Codecs.AviFrame(bitmap);
    
    // Put this frame to the writer object (it is assumed that the 
    // writer is properly initialized and opened). 
    writer.AddFrame(frame);
    
  • Get a frame from a reader object. Note, it is not obligatory to use AVI reader. You can use a reader of any other format which is available in Graphics Mill for .NET. For example, you can open some multi-frame format file like animated GIF and convert it to AVI:

    Visual Basic
    ' Load the animated.gif file and copy its frames to AVI.
    
    Dim reader As Aurigma.GraphicsMill.Codecs.IFormatReader
    reader = Aurigma.GraphicsMill.Codecs.FormatManager.CreateFormatReader( _
     "c:\animated.gif")
    
    Dim frame As Aurigma.GraphicsMill.Codecs.IFrame
    For Each frame In reader
        ' Put this frame to the writer object (it is assumed that the 
        ' writer is properly initialized and opened). 
        writer.AddFrame(frame)
    Next
    reader.Close()
    
    C#
    // Load the animated.gif file and copy its frames to AVI.
    
    Aurigma.GraphicsMill.Codecs.IFormatReader reader;
    reader = Aurigma.GraphicsMill.Codecs.FormatManager.CreateFormatReader(
            @"c:\animated.gif");
    foreach (Aurigma.GraphicsMill.Codecs.IFrame frame in reader)
    {
        // Put this frame to the writer object (it is assumed that the 
        // writer is properly initialized and opened). 
        writer.AddFrame(frame);
    }
    reader.Close();
    

See Also

Manual