.. _Intermediate/CreateAFrameCounter:

Create a Frame Counter
======================

Problem
-------

You want to overlay a frame counter on a video.

Solution
--------

To create a frame counter for a video we need to create a text annotation of the frame number and composite the frame number on the appropriate frame.

::

    #Set up the decoder
    decoder = Draft.VideoDecoder( "path/to/clip.mov" )
    image = Draft.Image.CreateImage( 1, 1 )
    frameNumber = 1
    textInfo = Draft.AnnotationInfo()

    #Set up encoder
    encoder = Draft.VideoEncoder( "path/to/save/video.mov" )

    while decoder.DecodeNextFrame( image ):
        frameText = Draft.Image.CreateAnnotation( str( frameNumber ), textInfo )

        #composite annotation onto frame
        anchor = Draft.Anchor.SouthEast
        compOp = Draft.CompositeOperator.OverCompositeOp
        image.CompositeWithAnchor( frameText, anchor, compOp )

        #encode the frame
        encoder.EncodeNextFrame( image )
        frameNumber += 1

    encoder.FinalizeEncoding()

Discussion
----------

Draft gives you a lot of freedom when creating a frame counter.  Firstly, You can set ``frameNumber`` to start at any number you want. For example, you can make the frame count from the start of the reel or some other number like a timecode.

Also, you can set the frame number to be padded with zeros like this: 0012. Just change :meth:`~Draft.Image.CreateAnnotation` to::

    CreateAnnotation( "%04d" % frameNumber, textInfo )

The number after the zero is the total number of digits that will appear.

Secondly, the size, font and colour can be set to anything by changing ``textInfo`` (see the entry on :ref:`Basic/CreateAnnotation` for more information).

Finally, you can change the position of your frame counter by changing the :class:`~Draft.Anchor`. In this example, the frame counter will be put in the bottom right corner. By changing the :class:`~Draft.Anchor` you can make the frame counter appear anywhere on the frame such as the top right corner with a :const:`~Draft.Anchor.NorthEast` anchor.
