Retrieving Media File Information for C

This example will retrieve the source file properties and show them in message boxes.

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include "ltmm.h"
#include "prsht.h"

#if defined(_UNICODE)
LPOLESTR T2OLE(LPCTSTR lpt)
{
   return (LPOLESTR) lpt;
}
LPTSTR OLE2T(LPCOLESTR lpw)
{
   return (LPTSTR) lpw;
}
#else
LPOLESTR T2OLE(LPCTSTR lpt)
{
   static OLECHAR lpw[512];
   if(!lpt)
      return NULL;
   lpw[0] = L'\0';
   MultiByteToWideChar(CP_ACP, 0, lpt, -1, lpw, 512);
   return lpw;
}

LPTSTR OLE2T(LPCOLESTR lpw)
{
   static TCHAR lpt[512];
   if(!lpw)
      return NULL;
   lpt[0] = _T('\0');
   WideCharToMultiByte(CP_ACP, 0, lpw, -1, lpt, 512, NULL, NULL);
   return lpt;
}
#endif

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;
   HRESULT hr;

   CoInitialize(NULL);

   /* create an ltmmMediaInfo object: */
   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");
   IltmmMediaInfo__put_SourceFile(pMediaInfo, sString);
   SysFreeString(sString);

   /* general properties: */
   IltmmMediaInfo__get_SourceFile(pMediaInfo, &sString);
   AddStringToMsg("Source file: ", sString, szMsg);
   SysFreeString(sString);

   IltmmMediaInfo__get_SourceFileSize(pMediaInfo, &d);
   AddNumberToMsg("File size: ", d, szMsg, FALSE);

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

   IltmmMediaInfo__get_SourceDuration(pMediaInfo, &d);
   AddNumberToMsg("File duration: ", d, szMsg, TRUE);

   IltmmMediaInfo__get_SourceBitRate(pMediaInfo, &l);
   AddNumberToMsg("Source BitRate : ", (double)l, szMsg, FALSE);

   IltmmMediaInfo__get_SourceFormat(pMediaInfo, &l);
   AddNumberToMsg("Source format: ", (double)l, szMsg, FALSE);

   IltmmMediaInfo__get_SourceFormatName(pMediaInfo, &sString);
   AddStringToMsg("Source format name: ", sString, szMsg);
   SysFreeString(sString);

   IltmmMediaInfo__get_SourceFilterClassID(pMediaInfo, &sString);
   AddStringToMsg("Source filter CLSID: ", sString, szMsg);
   SysFreeString(sString);

   IltmmMediaInfo__get_SourceFilterName(pMediaInfo, &sString);
   AddStringToMsg("Source filter name: ", sString, szMsg);
   SysFreeString(sString);

   IltmmMediaInfo__get_Title(pMediaInfo, &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);

   IltmmMediaInfo__get_Author(pMediaInfo, &sString);
   AddStringToMsg("Author: ", sString, szMsg);
   SysFreeString(sString);

   IltmmMediaInfo__get_Description(pMediaInfo, &sString);
   AddStringToMsg("Description: ", sString, szMsg);
   SysFreeString(sString);

   IltmmMediaInfo__get_Rating(pMediaInfo, &sString);
   AddStringToMsg("Rating: ", sString, szMsg);
   SysFreeString(sString);

   IltmmMediaInfo__get_Copyright(pMediaInfo, &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);

      IltmmMediaInfo__put_CurrentStream(pMediaInfo, lIndex);

      IltmmMediaInfo__get_CurrentStream(pMediaInfo, &l);
      AddNumberToMsg("Stream number: ", (double)l, szMsg, FALSE);

      IltmmMediaInfo__get_StreamType(pMediaInfo, &sString);
      AddStringToMsg("Stream type: ", sString, szMsg);
      SysFreeString(sString);

      IltmmMediaInfo__get_StreamTypeName(pMediaInfo, &sString);
      AddStringToMsg("Stream type name: ", sString, szMsg);
      SysFreeString(sString);

      IltmmMediaInfo__get_StreamSubtype(pMediaInfo, &sString);
      AddStringToMsg("Stream subtype: ", sString, szMsg);
      SysFreeString(sString);

      IltmmMediaInfo__get_StreamSubtypeName(pMediaInfo, &sString);
      AddStringToMsg("Stream subtype name: ", sString, szMsg);
      SysFreeString(sString);

      IltmmMediaInfo__get_StreamDuration(pMediaInfo, &d);
      AddNumberToMsg("Stream duration: ", d, szMsg, TRUE);

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

      IltmmMediaInfo__get_VideoFrameRate(pMediaInfo, &d);
      AddNumberToMsg("Video frame rate: ", d, szMsg, TRUE);

      IltmmMediaInfo__get_VideoWidth(pMediaInfo, &l);
      AddNumberToMsg("Video width: ", (double)l, szMsg, FALSE);

      IltmmMediaInfo__get_VideoHeight(pMediaInfo, &l);
      AddNumberToMsg("Video height: ", (double)l, szMsg, FALSE);

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

      IltmmMediaInfo__get_VideoCompression(pMediaInfo, &l);
      AddNumberToMsg("Video compression: ", (double)l, szMsg, FALSE);

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

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

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

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

      IltmmMediaInfo__get_AudioChannels(pMediaInfo, &l);
      AddNumberToMsg("Audio channels: ", (double)l, szMsg, FALSE);

      IltmmMediaInfo__get_AudioSamplesPerSec(pMediaInfo, &l);
      AddNumberToMsg("Audio frequency: ", (double)l, szMsg, FALSE);

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

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

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

   IltmmMediaInfo_ResetSource(pMediaInfo);
   IltmmMediaInfo_Release(pMediaInfo);

                return 0;
}

void AddStringToMsg(char *pszTitle, BSTR sString, char *pszMsg)
{
   strcat(pszMsg, pszTitle);
   if( sString )
      strcat(pszMsg, OLE2T(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");
}