Properties of a Codec

This example demonstrates how to get and set the properties of a codec.

The program can be found in the %TOPLEVEL%\Samples\VC\CodecProperty directory. In order to run the program, open the solution file CodecProperty.sln in this directory and select Build -> Build CodecProperty in the menu. Then the program can be executed by selecting Debug -> Start.

The program generates a list of all available video compressors and asks the user for the name of the compressor it should use and a filename. If the selected codec has a property dialog, this dialog will now be displayed. After the dialog has been closed, the program retrieves the properties set in the property dialog and saves them to the file "codec.xml". Then it loads the properties that had been previously saved from the file and puts them in the codec. It then waits for a key press to start the capturing of the image stream. It will then write the image stream into the AVI file until enter is pressed again.

Not every codec has a property dialog that allows parameters to be set externally. The following codecs have a property dialog:

Saving Codec Settings

To save the data of a codec, use Codec::getSettings. This method returns an XML formatted string, containing the codec data. You can write this string to a file or the registry to save your program's codec settings:

std::string codec_settings = codec.getSettings();
if( !codec_settings.empty() )
{
    std::ofstream file( filename.c_str() );
    if( file.good() )
    {
        file << codec_settings;
        bSucceded = true;
    }
    else
    {
        std::cerr << "Error : Could not open file " << filename << std::endl;
    }
}
else
{
    std::cerr << "Error : No data available from the codec" << std::endl;
}

Loading Codec Settings

To restore codec settings, use Codec::setSettings:

std::ifstream file( filename.c_str() );
if( file.good() )
{
    std::string codec_settings;
    while( file.good() )    // read the file char by char
    {
        codec_settings += (char) file.get();
    }
    if( !codec_settings.empty() )    // no data in the file check
    {
        if( codec.setSettings( codec_settings ) )    // set the data
        {
            bSucceded = true;
        }
        else
        {
            std::cerr << "Error : Data does not fit codec" << std::endl;
        }
    }
    else
    {
        std::cerr << "Error : No data found in the supplied file" << std::endl;
    }
}
else
{
    std::cerr << "Error : Could not open file " << filename << std::endl;
}

See also

Writing Image Streams to an AVI file

<< Programmer's Guide