Creating DVD Images for C++

// The following code demonstrates how to create DVD images 
 using LEAD Multimedia toolkit: 

#include "ltmm.h"
#include " ILTDVDWriter2.h"
#include <TChar.h>

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;
}

void WaitForCompletion(IltmmConvert *pConvert);

int main()
{
   IltmmConvert *pConvert = NULL;
   IUnknown *pDvdWriter = NULL;
   ILTDVDWriter *pIDvdWriter = NULL;
   BSTR szFileName = NULL;
   IltmmCompressors *pCompressors;
   long lIndex = 0;
   BOOL bRet = FALSE;

   CoInitialize(NULL);

   // create a converter object
   HRESULT hr = CoCreateInstance(CLSID_ltmmConvert, NULL, CLSCTX_INPROC_SERVER, IID_IltmmConvert, (void**) &pConvert);

   // setup the converter:
   hr = pConvert->put_TargetFormat(ltmmConvert_TargetFormat_DVD);
   hr = pConvert->get_VideoCompressors(&pCompressors);
   hr = pCompressors->Find(L"@device:sw:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\\LEAD MPEG2 Encoder (2.0)", &lIndex);
   hr = pCompressors->put_Selection(lIndex);
   pCompressors->Release();
   hr = pConvert->get_AudioCompressors(&pCompressors);
   hr = pCompressors->Find(L"@device:sw:{33D9A761-90C8-11D0-BD43-00A0C911CE86}\\LEAD MPEG Audio Encoder (2.0)", &lIndex);
   hr = pCompressors->put_Selection(lIndex);
   pCompressors->Release();

   // 1- Create a DVD image with 1 title that contains 1 chapter:
   szFileName = SysAllocString(T2OLE("c:\\MyVideos\\DaDa_CMP.avi")); // Source video
   hr = pConvert->put_SourceFile(szFileName);
   SysFreeString(szFileName);

   szFileName = SysAllocString(T2OLE("c:\\MyVideos\\DVDImage1")); // Destination image folder
   hr = pConvert->put_TargetFile(szFileName);
   SysFreeString(szFileName);

   hr = pConvert->StartConvert();
   // Wait for the conversion to finish. You can use a window to receive notifications
   WaitForCompletion(pConvert);

   // Done
   pConvert->ResetTarget();
   pConvert->ResetSource();


   // 2- Create a DVD image with 1 title that contains 2 chapters:
   szFileName = SysAllocString(T2OLE("c:\\MyVideos\\DaDa_CMP.avi")); // Source video
   hr = pConvert->put_SourceFile(szFileName);
   SysFreeString(szFileName);

   szFileName = SysAllocString(T2OLE("c:\\MyVideos\\DVDImage2")); // Destination image folder
   hr = pConvert->put_TargetFile(szFileName);
   SysFreeString(szFileName);

   // Retrieve the DVD Writer interface
   hr = pConvert->GetSubObject(ltmmConvert_Object_Sink, &pDvdWriter);
   pDvdWriter->QueryInterface(IID_ILTDvdWriter, (void**)&pIDvdWriter);

   // Set the DVD temporary files folder
   szFileName = SysAllocString(T2OLE("C:\\Temp")); // Destination image folder
   pIDvdWriter->put_TempPath(szFileName);
   SysFreeString(szFileName);

   // Set the TitleBreak property to FALSE.
   // This will prevent the title from being written immediately after the conversion
   pIDvdWriter->put_TitleBreak(VARIANT_FALSE);

   // write the first chapter
   hr = pConvert->StartConvert();
   // Wait for the conversion to finish. You can use a window to receive notifications
   WaitForCompletion(pConvert);

   // You can change the source file before this step.
   // This demonstration code uses the same file for all chapters.
   // Create chapter 2.
   hr = pConvert->StartConvert();
   WaitForCompletion(pConvert);

   // Close the title
   pIDvdWriter->put_TitleBreak(VARIANT_TRUE);

   // Done
   pDvdWriter->Release();
   pDvdWriter = NULL;
   pIDvdWriter->Release();
   pIDvdWriter = NULL;
   pConvert->ResetTarget();
   pConvert->ResetSource();

   // 3- Create a DVD image with 2 titles, each contains 2 chapters:
   szFileName = SysAllocString(T2OLE("c:\\MyVideos\\DaDa_CMP.avi")); // Source video
   hr = pConvert->put_SourceFile(szFileName);
   SysFreeString(szFileName);

   szFileName = SysAllocString(T2OLE("c:\\MyVideos\\DVDImage3")); // Destination image folder
   hr = pConvert->put_TargetFile(szFileName);
   SysFreeString(szFileName);

   // Retrieve the DVD Writer interface
   hr = pConvert->GetSubObject(ltmmConvert_Object_Sink, &pDvdWriter);
   pDvdWriter->QueryInterface(IID_ILTDvdWriter, (void**)&pIDvdWriter);

   // Set the TitleBreak property to FALSE.
   // This will prevent the title from being written immediately after the conversion
   pIDvdWriter->put_TitleBreak(VARIANT_FALSE);

   // Write the first chapter in the first title
   hr = pConvert->StartConvert();
   // Wait for the conversion to finish. You can use a window to receive notifications
   WaitForCompletion(pConvert);

   // Write the second chapter in the first title
   // You can change the source file before this step, this demonstration code uses the same file for all chapters.
   hr = pConvert->StartConvert();
   WaitForCompletion(pConvert);

   // Prepare for the second title
   // Set the TitleBreak property to TRUE, so the the current title can be flushed
   pIDvdWriter->put_TitleBreak(VARIANT_TRUE);

   // Set the TitleBreak property to FALSE.
   // This will prevent the title from being written immediately after the conversion
   pIDvdWriter->put_TitleBreak(VARIANT_FALSE);

   // Disable Overwrite so the title will be appended to an existing dvd image
   pIDvdWriter->put_Overwrite(VARIANT_FALSE);

   // Write the first chapter in the second title
   hr = pConvert->StartConvert();
   // Wait for the conversion to finish. You can use a window to receive notifications
   WaitForCompletion(pConvert);

   // Write the second chapter in the second title
   hr = pConvert->StartConvert();
   WaitForCompletion(pConvert);

   // Close the second title
   pIDvdWriter->put_TitleBreak(VARIANT_TRUE);

   // restore the overwrite property
   pIDvdWriter->put_Overwrite(VARIANT_TRUE);

   // Done
   pDvdWriter->Release();
   pDvdWriter = NULL;
   pIDvdWriter->Release();
   pIDvdWriter = NULL;
   pConvert->ResetTarget();
   pConvert->ResetSource();
   pConvert->Release();

   CoUninitialize();

   return 0;
}

void WaitForCompletion(IltmmConvert *pConvert)
{
   long lState = ltmmConvert_State_Running;
   MSG msg;

   pConvert->get_State(&lState);

   while( lState != ltmmConvert_State_Stopped )
   {
      if( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
      {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
      }
     
      pConvert->get_State(&lState);
   }
}