tanvon++

September 27, 2006

Ticker ( Displaying Text Over Running Video )

Filed under: DirectShow — tanvon malik @ 1:18 am

The code for this article 

Download Demo

Displaying text is a bit same as displaying an image over video which I stated previously in my blog also.

The Actual Work

Displaying text over video means, that first we need to create a bitmap then we will write our text over that bitmap, and that bitmap will be blended with the running video with the help of VMR9.

Need Some Control Over Mixing

The thing that is vital, Is some preferences which we need to set. Actually if you display an image over video, You don’t need much control over actual blending, Because if an image is a bit is scaled or changed it is difficult to catch it, But in the case of text you can’t accept such situation. Because if text is a bit scaled or changed it would be much harder to read it. So first of all we need to tell the mixer to not scale our text. The interface which will come to save us is IVMRMixerControl9. Here we request point filtering instead of bilinear filtering (which is default) to improve the text quality. If you are not scaling the app Image, you should use point filtering.

pVmr->QueryInterface( IID_IVMRMixerControl9, (void**) &pMix);
DWORD dwPrefs=0;
pMix->GetMixingPrefs( &dwPrefs);
dwPrefs |= MixerPref_PointFiltering;
dwPrefs &= ~( MixerPref_BiLinearFiltering );
pMix->SetMixingPrefs( dwPrefs );

Text To Image

Now we will create the bitmap having our text.

CDC * pdc = GetDC();
CDC mCompatibleDC;
mCompatibleDC.CreateCompatibleDC( pdc );
mCompatibleDC.SelectObject( mFont );
CSize strSize = mCompatibleDC.GetTextExtent( strFinish );
CBitmap bm;
bm.CreateCompatibleBitmap( pdc, strSize.cx, strSize.cy);
mCompatibleDC.SelectObject( &bm );
mCompatibleDC.SetBkColor(mBK_Color);
mCompatibleDC.SetTextColor(mTXT_Color);
mCompatibleDC.TextOut(0,0,strFinish);
This is easy to understand the above code.

  • Create CDC which should be compatible with the display (or as required)
  •  Then we select the font in that CDC.
  • We get the size of the actual text in logical units.
  • Then we create the bitmap of the size of the text.
  • We select that bitmap in our CDC.
  • We set text and background color of the text .
  • We write the actual text in our CDC

This all mean, that now in our CDC we have an image with the required text.

Displaying Text Over Video

Now we will prepare for showing the image with text over video. This snippet of code is  approximately similar to a my previous article..
VMR9AlphaBitmap bmpInfo;
ZeroMemory(&bmpInfo, sizeof(bmpInfo) );
bmpInfo.dwFlags |= VMRBITMAP_HDC;
bmpInfo.hdc = pDC->m_hDC;
LONG cx, cy;
pWC->GetNativeVideoSize( &cx, &cy, NULL, NULL);
bmpInfo.rSrc = Rect;
// rDest specifies the destination rectangle
//in composition space (0.0f to 1.0f)
bmpInfo.rDest.right = 1.0f;
bmpInfo.rDest.left = 0.0f;
bmpInfo.rDest.top = (float)(cy - Rect.Height()) / (float)cy - EDGE_BUFFER;
bmpInfo.rDest.bottom = 1.0f - EDGE_BUFFER;
// Set the transparency
// value (1.0 is opaque, 0.0 is transparent)
bmpInfo.fAlpha = 1.0;
pBmp->SetAlphaBitmap( &bmpInfo);

All is done now you can blend any text over video.

7 Comments »

  1. Can this be done for a c# application that runs a media video file in fullscreen?

    I need to blend text over the video output in fullscreen. How can that be done?

    Great article!

    Comment by Catalin — October 8, 2006 @ 12:49 am

  2. Hey gr8 blog!

    Do you know how to work on the audio stream of the video using DirectShow.
    I need to hav the option of play my own audio instead of the audio stream tht come with the video file.

    Comment by Chirag Gupta — October 21, 2006 @ 9:59 am

  3. tanvon,
    I am trying to display an oscilloscope (aka audio scope) over a running video. The directshow filter i am using to produce the audio scope is located in: (http://virtualvcr.sourceforge.net/html/virtualvcr/filters/scope.php) This filter creates its own window and produces the scope. What I want to do is, instead of creating its own window, it should display the audioscope underneath the video (so instead of displaying text, display the scope!). Please point me in the right direction. Thanks!

    janreinier@yahoo.com.au

    Comment by jr — February 9, 2007 @ 12:32 am

  4. Great Article. I think one can translate the Code to C# using DirectShow.NET. Will You b Kind enough…. ;-)

    Thanks…

    Comment by Sukhjinder — August 21, 2007 @ 7:11 am

  5. very interesting, but I don’t agree with you
    Idetrorce

    Comment by Idetrorce — December 15, 2007 @ 2:07 pm

  6. Hi,

    Is there some method to save the rendered video with the text ticker to a file.

    Comment by kamalakshan — June 10, 2008 @ 12:48 pm

  7. Hi
    yes but you have to create a custom transform filter which adds the the ticker to samples, and then save that stream to a file.

    Comment by tanvon malik — September 28, 2008 @ 11:39 am


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.