Command Line

/*
//==========================================================================
//
//  Copyright (c) On2 Technologies Inc. All Rights Reserved.
//
//--------------------------------------------------------------------------
//
//  File:        $Workfile$
//               $Revision$
//
//  Last Update: $DateUTC$
//
//--------------------------------------------------------------------------
*/
using System;
using System.Runtime.InteropServices; //for COMException
using flixengine_com; //Flix Engine Interfaces

namespace cli_encode
{
    public class cli_encode
    {
        static object createInstance(string progid)
        {
            Type t = Type.GetTypeFromProgID(progid);
            object o = null;
            try {
                o = Activator.CreateInstance(t);
            } catch(Exception e) {
                Console.Error.WriteLine("Error creating object instance: "+e);
                Environment.Exit(1);
            }
            return o;
        }

        static void printStackTrace(IFlix flix, COMException e)
        {
            Console.WriteLine(e);
            if(flix!=null) {
                printEncoderStatus(flix);
            }
            Environment.Exit(e.ErrorCode);
        }

        [MTAThread]
        static void Main(string[] args)
        {
            /*
              create an instance of the main engine interface, IFlix
            */
            IFlix flix = (IFlix)createInstance("On2.FlixEngine");

            /*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
            try {
                flix.setLogLevel(3);
                flix.setLogPath("\\cli_encode_com.log");
            } catch(COMException e) {
                printStackTrace(flix,e);
            }*/

            /*
              print some library information
            */
            try {
                Console.WriteLine("Flix Engine COM library. Flix Engine v"+
                                  flix.version()+" COM v"+flix.com_version());
                Console.WriteLine(flix.copyright()+"\n");
            } catch(COMException e) {
                printStackTrace(flix,e);
            }

            if(args.Length<2) {
                Console.Error.WriteLine(
                    "usage: cli_encode.exe <infile> <outfile>\n");
                Environment.Exit(1);
            }

            /*
              set the source file
            */
            try {
                Console.WriteLine("Input file  : "+args[0]);
                flix.setInputFile(args[0]);
            } catch(COMException e) {
                printStackTrace(flix,e);
            }

            /*
              retrieve the video options interface, IVideoOptions
            */
            try {
                IVideoOptions vidopts = flix.videoOptions();

                /*
                  print input file information
                */
                Console.WriteLine("Width:    ".PadLeft(24)+
                                  vidopts.getSourceWidth());
                Console.WriteLine("Height:   ".PadLeft(24)+
                                  vidopts.getSourceHeight());
                Console.WriteLine("Duration: ".PadLeft(24)+
                                  flix.getSourceDuration()+"ms");
            } catch(COMException e) {
                printStackTrace(flix,e);
            }

            /*
              set the destination file
            */
            try {
                Console.WriteLine("Output File : "+args[1]);
                flix.setOutputFile(args[1]);
            } catch(COMException e) {
                printStackTrace(flix,e);
            }

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

            /*Add the scale filter
            try {
                IFlixPlgn filter = flix.addFilter(flix.FE2_FILTER_SCALE);

                filter.setParam(flix.FE2_SCALE_WIDTH,240);
                filter.setParam(flix.FE2_SCALE_HEIGHT,160);
            } catch(COMException e) {
                printStackTrace(flix,e);
            }*/

            /*Add the vp6 codec. Though it is the default, you must add it in order
              to modify its settings
            try {
                IFlixPlgn codec = flix.addCodec(flix.FE2_CODEC_VP6);

                codec.setParam(flix.FE2_VP6_RC_MODE,
                    Convert.ToDouble(FE2_VideoBitrateControls.VBR_1PASSControl));
            } catch(COMException e) {
                printStackTrace(flix,e);
            }*/

            /*Use the FLV muxer (default)
            try {
                IFlixPlgn muxer = flix.addMuxer(flix.FE2_MUXER_FLV);
            } catch(COMException e) {
                printStackTrace(flix,e);
            }*/

            /*
              start the encode
            */
            try {
                flix.encode();

                /*
                  retrieve the encoding status interface, IEncodingStatus
                */
                IEncodingStatus encstatus = flix.encodingStatus();

                Console.WriteLine();
                bool ier;
                do {
                    System.Threading.Thread.Sleep(500);
                    ier = flix.isEncoderRunning()==1;
                    Console.Write("\rEncoding..."+
                        encstatus.percentComplete().ToString().PadLeft(3)+"% ");
                } while(ier);
            } catch(COMException e) {
                printStackTrace(flix,e);
            }

            Console.WriteLine("Done!");
            printEncoderStatus(flix);

            /*
              Force the cleanup of IFlix.
              Though this is not strictly necessary in this sample, as
              it is about to exit, if the script is more involved it may be
              necessary so the input file can be moved as destruction of
              the underlying FLIX2HANDLE occurs within IFlix's destructor.
              By explicitly defining a WeakReference and removing the strong
              referenece to the flix object we are guaranteeing that it will
              not survive the garbage collection.
            */
            WeakReference wkref = new WeakReference(flix); flix = null;
            System.GC.Collect(System.GC.GetGeneration(wkref));
            System.GC.WaitForPendingFinalizers();
        }

        static void printEncoderStatus(IFlix flix)
        {
            Console.WriteLine("\nEncoder Status");
            try {
                Console.WriteLine(" flix.getEncoderState: "+flix.getEncoderState());

                FE2_errno flixerr = FE2_errno.ErrNone;
                int syserr = 0;
                flix.errno_(out flixerr,out syserr);

                Console.WriteLine(" flix.errno_: flixerrno:"+flixerr+
                                  " syserrno:" + syserr);
            } catch(COMException e) {
                printStackTrace(null,e);
            }
        }
    }
}

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