Convert DVD with Subtitles for C

This example shows how to convert only one chapter in a DVD title and how to enable DVD subtitles during conversion when you use the DVD Source object.

// This function will select one chapter in a certain title
void SelectChapter(IltmmDVDTitle *pTitle, int nChapterIndex)
{
   long nCount, i;
   IltmmDVDTitle_get_ChapterCount(pTitle, &nCount);
   for(i = 0; i < nCount; i++)
   {
      IltmmDVDChapter *pChapter;
      if(IltmmDVDTitle_GetChapter(pTitle, i, &pChapter) == S_OK)
      {
         IltmmDVDChapter_put_Selected(pChapter, i == nChapterIndex ? VARIANT_TRUE : VARIANT_FALSE);
         IltmmDVDChapter_Release(pChapter);
      }
   }
}

/*
   This function will select the main title for conversion.
   Within the main title, it will select only the second chapter.
   It will also select the first subtitle stream.
*/

void SelectOneChapterAndSubtitles(IltmmConvert *pConvert)
{
   IUnknown *punk;
   IltmmDVDSource* pDVDSource;
   // Get the DVD source interface
   IltmmConvert_GetSubObject(pConvert, ltmmConvert_Object_SourceFilter, &punk);
   if (punk)
   {
      IUnknown_QueryInterface(punk, &IID_IltmmDVDSource, (void**)&pDVDSource);
      if (pDVDSource)
      {
         IltmmDVDTitle *pTitle;
         long lCount, i;
         long lVal;
         // Select the main title
         IltmmDVDSource_put_Selected(pDVDSource, ltmmDVDSource_Main_Selected);
         // We now need to select one chapter and enable subtitles for this title
         // For that, we will loop through the titles and find out which one was selected above
         // Get the title count in the disc
         IltmmDVDSource_get_TitleCount(pDVDSource, &lCount);
         for (i = 0; i < lCount; i++)
         {
            // Get the title interface
            IltmmDVDSource_GetTitle(pDVDSource, i, &pTitle);
            if (pTitle)
            {
               long bSelected;
               IltmmDVDTitle_get_Selected(pTitle, &bSelected);
               if(bSelected)
               {
                  // we found the main title
                  // (Optional step). We will convert only one chapter to speed up the conversion. 
                  // We will select the 2nd chapte, because the first chapter will sometimes have movie credits and little dialogue
                  // Chapters are 0-based, so the second chapter has index 1
                  SelectChapter(pTitle, 1);
                  // Get the subpicture stream count
                  IltmmDVDTitle_get_SubpictureStreamCount(pTitle, &lVal);
                  if(lVal > 0)
                     // Select the first subpicture stream
                     IltmmDVDTitle_put_SelectedSubpictureStream(pTitle, 0);
               }
               IltmmDVDTitle_Release(pTitle);
            }
         }
         IltmmDVDSource_Release(pDVDSource);
      }
      IUnknown_Release(punk);
   }
}

#define LEAD_MPEG2_ENCODER L"@device:sw:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\\LEAD MPEG2 Encoder (3.0)"
#define LEAD_MPEG_AUDIO_ENCODER L"@device:sw:{33D9A761-90C8-11D0-BD43-00A0C911CE86}\\LEAD MPEG Audio Encoder (2.0)"

// select a certain encoder
void SelectCompressor(IltmmCompressors *pCompressors, LPCWSTR pszCompressorName)
{
   long index;
   BSTR bstrCompressorName = SysAllocString(pszCompressorName);
   IltmmCompressors_Find(pCompressors, bstrCompressorName, &index);
   IltmmCompressors_put_Selection(pCompressors, index);
   SysFreeString(bstrCompressorName);
}

// convert one chapter with subtitles (close captioning)
HRESULT ConvertOneChapterWithSubtitles()
{
   IltmmConvert *pConvert;
   IltmmCompressors *pCompressors;
   HRESULT hr = CoCreateInstance(&CLSID_ltmmConvert, NULL, CLSCTX_INPROC_SERVER, &IID_IltmmConvert, (void**) &pConvert);
   if(FAILED(hr))
      return hr;

   // use the DVD source for greater control over the DVD conversion process
   IltmmConvert_put_UseDVDSource(pConvert, VARIANT_TRUE);
   // set the source, compressors and target format
   IltmmConvert_put_SourceFile(pConvert, L"e:\\video_ts\\video_ts.ifo");
   IltmmConvert_put_TargetFormat(pConvert, ltmmConvert_TargetFormat_MPEG2_PROGRAM);
   // select mpeg2 encoder
   IltmmConvert_get_VideoCompressors(pConvert, &pCompressors);
   SelectCompressor(pCompressors, LEAD_MPEG2_ENCODER);
   IltmmCompressors_Release(pCompressors);
   // select mpeg audio encoder
   IltmmConvert_get_AudioCompressors(pConvert, &pCompressors);
   SelectCompressor(pCompressors, LEAD_MPEG_AUDIO_ENCODER);
   IltmmCompressors_Release(pCompressors);
   IltmmConvert_put_TargetFile(pConvert, L"c:\\i\\1.mpg");
   // this function is the interesting part of the example
   SelectOneChapterAndSubtitles(pConvert);
   hr = IltmmConvert_StartConvert(pConvert);
   // wait for conversion to finish... Not very elegant, but we want to keep the example simple
   MessageBox(NULL, TEXT("Wait to finish"), TEXT("wait"), MB_OK);
   IltmmConvert_Release(pConvert);
   return hr;
}