Retrieving Media File Information for C++

This simple C++ example for using the ltmmMediaInfo object retrieves the source file properties and displays them in message boxes.

#include "ltmm.h"
#include <atlbase.h>
#include <atlconv.h>

void AddStringToMsg(char *pszTitle, BSTR sString, char *pszMsg);
void AddNumberToMsg(char *pszTitle, double dNumber, char *pszMsg, BOOL bFloat);

int main()
{
   IltmmMediaInfo *pMediaInfo;
   BSTR sString;
   char szMsg[8196];
   long lStreams, lIndex, l;
   double d;

   CoInitialize(NULL);

   // create an ltmmMediaInfo object:
   HRESULT hr = CoCreateInstance(CLSID_ltmmMediaInfo, NULL,
                                 CLSCTX_INPROC_SERVER, IID_IltmmMediaInfo,
                                 (void**) &pMediaInfo);

   memset(szMsg, 0, 8196);

   // set the source file:
   sString = SysAllocString(L"C:\\Test.avi");
   pMediaInfo->put_SourceFile(sString);
   SysFreeString(sString);

   // general properties:
   pMediaInfo->get_SourceFile(&sString);
   AddStringToMsg("Source file: ", sString, szMsg);
   SysFreeString(sString);

   pMediaInfo->get_SourceFileSize(&d);
   AddNumberToMsg("File size: ", d, szMsg, FALSE);

   pMediaInfo->get_OutputStreams(&lStreams);
   AddNumberToMsg("Number of streams: ", (double)lStreams, szMsg, FALSE);

   pMediaInfo->get_SourceDuration(&d);
   AddNumberToMsg("File duration: ", d, szMsg, TRUE);

   pMediaInfo->get_SourceBitRate(&l);
   AddNumberToMsg("Source BitRate : ", (double)l, szMsg, FALSE);

   pMediaInfo->get_SourceFormat(&l);
   AddNumberToMsg("Source format: ", (double)l, szMsg, FALSE);

   pMediaInfo->get_SourceFormatName(&sString);
   AddStringToMsg("Source format name: ", sString, szMsg);
   SysFreeString(sString);

   pMediaInfo->get_SourceFilterClassID(&sString);
   AddStringToMsg("Source filter CLSID: ", sString, szMsg);
   SysFreeString(sString);

   pMediaInfo->get_SourceFilterName(&sString);
   AddStringToMsg("Source filter name: ", sString, szMsg);
   SysFreeString(sString);

   pMediaInfo->get_Title(&sString);
   AddStringToMsg("Title The name for a group of related video files (called "Chapters") on your DVD. For example, for a DVD called "My Summer Vacation," you might have the titles "Water Skiing," "New Friends," and "Hiking." For each of those titles, you might have one or more different video files.: ", sString, szMsg);
   SysFreeString(sString);

   pMediaInfo->get_Author(&sString);
   AddStringToMsg("Author: ", sString, szMsg);
   SysFreeString(sString);

   pMediaInfo->get_Description(&sString);
   AddStringToMsg("Description: ", sString, szMsg);
   SysFreeString(sString);

   pMediaInfo->get_Rating(&sString);
   AddStringToMsg("Rating: ", sString, szMsg);
   SysFreeString(sString);

   pMediaInfo->get_Copyright(&sString);
   AddStringToMsg("Copyright: ", sString, szMsg);
   SysFreeString(sString);

   MessageBox(0, szMsg, "MediaInfo Sample: General...", 0);

   // stream properties:
   for( lIndex=0; lIndex<lStreams; lIndex++ )
   {
      memset(szMsg, 0, 8196);

      pMediaInfo->put_CurrentStream(lIndex);

      pMediaInfo->get_CurrentStream(&l);
      AddNumberToMsg("Stream number: ", (double)l, szMsg, FALSE);

      pMediaInfo->get_StreamType(&sString);
      AddStringToMsg("Stream type: ", sString, szMsg);
      SysFreeString(sString);

      pMediaInfo->get_StreamTypeName(&sString);
      AddStringToMsg("Stream type name: ", sString, szMsg);
      SysFreeString(sString);

      pMediaInfo->get_StreamSubtype(&sString);
      AddStringToMsg("Stream subtype: ", sString, szMsg);
      SysFreeString(sString);

      pMediaInfo->get_StreamSubtypeName(&sString);
      AddStringToMsg("Stream subtype name: ", sString, szMsg);
      SysFreeString(sString);

      pMediaInfo->get_StreamDuration(&d);
      AddNumberToMsg("Stream duration: ", d, szMsg, TRUE);

      // video specific
      strcat(szMsg, "\n");
      strcat(szMsg, "\t*** Video specific properties ***\n");

      pMediaInfo->get_VideoFrameRate(&d);
      AddNumberToMsg("Video frame rate: ", d, szMsg, TRUE);

      pMediaInfo->get_VideoWidth(&l);
      AddNumberToMsg("Video width: ", (double)l, szMsg, FALSE);

      pMediaInfo->get_VideoHeight(&l);
      AddNumberToMsg("Video height: ", (double)l, szMsg, FALSE);

      pMediaInfo->get_VideoBitCount(&l);
      AddNumberToMsg("Video bit count: ", (double)l, szMsg, FALSE);

      pMediaInfo->get_VideoCompression(&l);
      AddNumberToMsg("Video compression: ", (double)l, szMsg, FALSE);

      pMediaInfo->get_VideoBitRate(&l);
      AddNumberToMsg("Video bit rate: ", (double)l, szMsg, FALSE);

      pMediaInfo->get_VideoFrames(&l);
      AddNumberToMsg("Video frames count: ", (double)l, szMsg, FALSE);

      // Audio specific
      strcat(szMsg, "\n");
      strcat(szMsg, "\t*** Audio specific properties ***\n");

      pMediaInfo->get_AudioFormatTag(&l);
      AddNumberToMsg("Audio format tag: ", (double)l, szMsg, FALSE);

      pMediaInfo->get_AudioChannels(&l);
      AddNumberToMsg("Audio channels: ", (double)l, szMsg, FALSE);

      pMediaInfo->get_AudioSamplesPerSec(&l);
      AddNumberToMsg("Audio frequency: ", (double)l, szMsg, FALSE);

      pMediaInfo->get_AudioBitsPerSample(&l);
      AddNumberToMsg("Audio sample size: ", (double)l, szMsg, FALSE);

      pMediaInfo->get_AudioAvgBytesPerSec(&l);
      AddNumberToMsg("Audio average bytes per second: ", (double)l, szMsg, FALSE);

      MessageBox(0, szMsg, "MediaInfo Sample: Streams...", 0);
   }


   pMediaInfo->ResetSource();
   pMediaInfo->Release();

   CoUninitialize();
                return 0;
}

void AddStringToMsg(char *pszTitle, BSTR sString, char *pszMsg)
{
   USES_CONVERSION;

   strcat(pszMsg, pszTitle);
   if( sString )
      strcat(pszMsg, W2A(sString));
   else
      strcat(pszMsg, "NA");
   strcat(pszMsg, " \n");
}

void AddNumberToMsg(char *pszTitle, double dNumber, char *pszMsg, BOOL bFloat)
{
   char szNumber[64];

   if( bFloat )
      sprintf(szNumber, "%f", dNumber);
   else
      sprintf(szNumber, "%.f", dNumber);

   strcat(pszMsg, pszTitle);
   strcat(pszMsg, szNumber);
   strcat(pszMsg, " \n");
}