IltmmWMScript HeaderScripts Example for C++

This example shows how to add and remove header scripts from a convert graph.

#define MAKE_MEDIA_PATH(pFileName) (TEXT("C:\\LEADTOOLS 17.5\\Media\\")TEXT(pFileName))
void ManageHeaderScripts(IltmmConvert* pConvert)
{
   HRESULT hr = S_OK;
   IUnknown *punk;
   IltmmWMScript *pWMScript;
   // source and target path names
   BSTR bstrSource = SysAllocString(MAKE_MEDIA_PATH("source.avi"));
   BSTR bstrTarget = SysAllocString(MAKE_MEDIA_PATH("source_script.wmv"));
   
   // set the source, target and WMV target formats for our conversion
   pConvert->put_SourceFile(bstrSource);
   pConvert->put_TargetFile(bstrTarget);
   pConvert->put_TargetFormat(ltmmConvert_TargetFormat_WMV_Mux);
   // free the BSTRs
   SysFreeString(bstrSource);
   SysFreeString(bstrTarget);
   // get the target object
   pConvert->GetSubObject(ltmmConvert_Object_TargetFilter, &punk);
   if (punk)
   {
      // get the WMScript object
      punk->QueryInterface(IID_IltmmWMScript, (void**)&pWMScript);
      if (pWMScript)
      {
         double duration;
         long lScriptCnt;
         WCHAR szTemp[128];
         double scriptTime;
         BSTR bstrScriptType, bstrScriptCmd;
         // turn off stream scripts and remove any header scripts
         pWMScript->put_EnableScriptStream(VARIANT_FALSE);
         pWMScript->RemoveAllHeaderScripts();
         // get the source duration
         hr = pConvert->get_Duration(&duration);
      
         if (FAILED(hr))
            duration = 0.5 ;//assume a relatively small duration
         
         // create a sample header script (type of caption) and set it to execute @ 0 secs
         // we will delete this one later for demonstation purposes
         scriptTime = 0.;
         swprintf_s(szTemp, _countof(szTemp), L"Sample caption script at %g seconds", scriptTime);      
         bstrScriptCmd = SysAllocString(szTemp);
         // add the header script
         hr = pWMScript->AddHeaderScript(L"caption", bstrScriptCmd, scriptTime);
         // free the BSTR
         SysFreeString(bstrScriptCmd);
         // create a sample header script (type of caption) and set it to execute @ duration / 2
         scriptTime = duration/2.0;
         swprintf_s(szTemp, _countof(szTemp), L"Sample caption script at %g seconds", scriptTime);      
         bstrScriptCmd = SysAllocString(szTemp);
         // add the header script
         hr = pWMScript->AddHeaderScript(L"caption", bstrScriptCmd, scriptTime);
         // free the BSTR
         SysFreeString(bstrScriptCmd);
         // now get the header script count, should be 2 from above
         hr = pWMScript->get_HeaderScriptCount(&lScriptCnt);
         if (SUCCEEDED(hr) && lScriptCnt>0)
         {
            long i;
            
            // iterate through the script indexes 
            for (i=0; i<lScriptCnt; i++)
            {
               if (i==0) // if first one, delete it (demonstration purposes)
               {
                  pWMScript->RemoveHeaderScript(i);
               }
               else if (i==1) // if second one, just get it and display a msg box with properties
               {
                  hr = pWMScript->GetHeaderScript(i, &bstrScriptType, &bstrScriptCmd, &scriptTime);
                  if (SUCCEEDED(hr))
                  {
                     WCHAR szTemp[255];
                     swprintf_s(szTemp, _countof(szTemp), L"type: %s\ncmd: %s\ntime: %g", bstrScriptType, bstrScriptCmd, scriptTime);      
                     ::MessageBox(NULL, szTemp, L"Script Properties", MB_OK);
                     // free the BSTRs
                     SysFreeString(bstrScriptType);
                     SysFreeString(bstrScriptCmd);
                  }
               }
            }
         }
         // release the ASF mux script interface
         pWMScript->Release();
      }
            
      // release the target filter
      punk->Release();
      // if all is well, run the convert now
      if (SUCCEEDED(hr))
         hr = pConvert->StartConvert();
   }
}