Retrieving Media File Information for C

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

#include <windows.h>

//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);

L_MULTIMEDIATEX_API int RetrievingMediaFileInformation_Example ( )
{
   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: ", 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");
}