Command Line

/*
//==========================================================================
//
//  Copyright (c) On2 Technologies Inc. All Rights Reserved.
//
//--------------------------------------------------------------------------
//
//  File:        $Workfile$
//               $Revision$
//
//  Last Update: $DateUTC$
//
//--------------------------------------------------------------------------
*/
#define _WIN32_WINNT 0x0400 //for COINIT_MULTITHREADED
#include <atlbase.h>
#include <atlcom.h>
#include <tchar.h>
#include <iostream>
#include <iomanip>
#include "flixengine_com.h"

using std::cout;
using std::cerr;
using std::endl;
using std::ios_base;

/*
  Retrieve description for HRESULT via FormatMessage
*/
LPTSTR hresult_desc(HRESULT hr)
{
    LPTSTR str;
    if(HRESULT_FACILITY(hr) == FACILITY_WINDOWS)
        hr = HRESULT_CODE(hr);

    if(!FormatMessage(
           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
           NULL, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
           (LPTSTR)&str, 0, NULL)) {
        _stprintf(str= (LPTSTR)LocalAlloc(LPTR,512),
            _T("[no description available, HRESULT_CODE(hr):%d]\n"),hr);
    }

    return str;
}

/*
  Call 'func', outputting any available information on failure before exiting
*/
#define CHECK_HR(func) {\
    HRESULT hr = (func);\
    if(FAILED(hr)) {\
        LPTSTR desc = hresult_desc(hr);\
        /*print the failing function*/\
        cerr << endl << #func << " failed!" << endl\
             << "HRESULT(" << endl << hr << "," << endl << desc << ")" << endl;\
        LocalFree(desc);\
        \
        /*if we've instantiated the IFlix interface..*/\
        __if_exists(flix) {\
            FE2_errno flixerr = ErrNone;\
            on2s32 syserr = 0;\
            /*retrieve further error information if possible*/\
            hr= flix->errno_(&flixerr,&syserr);\
            cerr << "flix->errno_: hr:"\
                << std::setiosflags(ios_base::hex|ios_base::showbase)\
                << std::setw(8) << hr\
                << std::dec << " flixerrno:" << flixerr\
                << " syserrno:" << syserr << endl;\
        }\
        exit(EXIT_FAILURE);\
    }\
}

void print_encoder_status(IFlix* flix)
{
    cout << "\nEncoder Status\n";

    FE2_EncState state;
    CHECK_HR( flix->getEncoderState(&state) )
    cout << " flix->getEncoderState: " << state << endl;

    FE2_errno flixerr = ErrNone;
    on2s32 syserr;
    CHECK_HR( flix->errno_(&flixerr,&syserr) )
    cout << " flix->errno_: flixerrno:" << flixerr
         << " syserrno:" << syserr << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
    CHECK_HR( CoInitializeEx(NULL,COINIT_MULTITHREADED) );

    /*
      create an instance of the Flix class (Flix Engine COM)
    */
    IUnknown* unk;
    CHECK_HR( CoCreateInstance(CLSID_Flix, 0, CLSCTX_INPROC_SERVER,
                  IID_IUnknown, reinterpret_cast<void**>(&unk)) );

    /*
      retrieve the main engine interface, IFlix
    */
    IFlix* flix = NULL;
    CHECK_HR( unk->QueryInterface(IID_IFlix, reinterpret_cast<void**>(&flix)) );
    unk->Release();

    /*enable logging, 0=none(disable) 1=info 2=error(asserts) 3=debug 4=heavy
      CONOUT$ can be used as the log file name to send output to the console
    CHECK_HR( flix->setLogLevel(3) );
    CHECK_HR( flix->setLogPath(L"\\cli_encode_com.log") );*/

    /*
      print some library information
    */
    BSTR bstr;
    CHECK_HR( flix->version(&bstr) );
    cout << "Flix Engine COM library. Flix Engine v" << COLE2CT(bstr);
    ::SysFreeString(bstr);

    CHECK_HR( flix->com_version(&bstr) );
    cout << " COM v" << COLE2CT(bstr) << endl;
    ::SysFreeString(bstr);

    CHECK_HR( flix->copyright(&bstr) );
    cout << COLE2CT(bstr) << endl << endl;
    ::SysFreeString(bstr);

    if(argc<3) {
        cerr << "usage: cli_encode_com.exe <infile> <outfile>" << endl << endl;
        return EXIT_FAILURE;
    }

    /*
      set the source file
    */
    cout << "Input file  : " << argv[1] << endl;
    CHECK_HR( flix->setInputFile(CComBSTR(argv[1])) );

    /*
      retrieve the video options interface, IVideoOptions
    */
    IVideoOptions* vidopts;
    CHECK_HR( flix->videoOptions(&vidopts) );

    /*
      print input file information
    */
    on2s32 duration; CHECK_HR( flix->getSourceDuration(&duration) );
    on2s32 srcw;     CHECK_HR( vidopts->getSourceWidth(&srcw) );
    on2s32 srch;     CHECK_HR( vidopts->getSourceHeight(&srch) );
    cout << std::setw(25) << "Width:     " << srcw << endl
         << std::setw(25) << "Height:    " << srch << endl
         << std::setw(25) << "Duration:  " << duration << "ms" << endl;
    /*release vidopts as we're through with it*/
    vidopts->Release();

    /*
      set the destination file
    */
    cout << "Output File : " << argv[2] << endl;
    CHECK_HR( flix->setOutputFile(CComBSTR(argv[2])) );

    /*
       Options may be set and codecs/filters/muxers may be added prior to encode()
    */

    /*Add the scale filter
    IFlixPlgn* filter;
    CHECK_HR( flix->addFilter(CComBSTR(FE2_FILTER_SCALE), &filter) );

    CHECK_HR( filter->setParam(CComBSTR(FE2_SCALE_WIDTH),240) );
    CHECK_HR( filter->setParam(CComBSTR(FE2_SCALE_HEIGHT),160) );
    filter->Release();*/

    /*Add the vp6 codec. Though it is the default, you must add it in order
      to modify its settings
    IFlixPlgn* codec;
    CHECK_HR( flix->addCodec(CComBSTR(FE2_CODEC_VP6), &codec) );

    CHECK_HR( codec->setParam(CComBSTR(FE2_VP6_RC_MODE), VBR_1PASSControl) );
    codec->Release();*/

    /*Use the FLV muxer (default)
    IFlixPlgn* muxer;
    CHECK_HR( flix->addMuxer(CComBSTR(FE2_MUXER_FLV), &muxer) );

    muxer->Release();*/

    /*
      start the encode
    */
    CHECK_HR( flix->encode() );

    /*
      retrieve the encoding status interface, IEncodingStatus
    */
    IEncodingStatus* encstatus;
    CHECK_HR( flix->encodingStatus(&encstatus) );

    on2bool b;
    on2s32 pcnt;
    cout << endl;
    do {
        Sleep(500);
        CHECK_HR( flix->isEncoderRunning(&b) );
        CHECK_HR( encstatus->percentComplete(&pcnt) );
        cout << "\rEncoding..." << std::setw(3) << pcnt << "% ";
    } while(b);

    cout << "Done!" << endl;
    print_encoder_status(flix);
    encstatus->Release();

    /*
      cleanup
    */
    flix->Release();
    return EXIT_SUCCESS;
}

On2 Technologies, Inc Flix Engine Windows documentation, generated on Tue Nov 2 15:38:05 2010 by doxygen 1.6.1