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 IltmmWMScript_HeaderScripts_Example(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
   IltmmConvert_put_SourceFile(pConvert, bstrSource);
   IltmmConvert_put_TargetFile(pConvert, bstrTarget);
   IltmmConvert_put_TargetFormat(pConvert, ltmmConvert_TargetFormat_WMV_Mux);
   // free the BSTRs
   SysFreeString(bstrSource);
   SysFreeString(bstrTarget);
   // get the target object
   IltmmConvert_GetSubObject(pConvert, ltmmConvert_Object_TargetFilter, &punk);
   if (punk)
   {
      // get the WMScript object
      IUnknown_QueryInterface(punk, &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
         IltmmWMScript_put_EnableScriptStream(pWMScript, VARIANT_FALSE);
         IltmmWMScript_RemoveAllHeaderScripts(pWMScript);
         // get the source duration
         hr = IltmmConvert_get_Duration(pConvert, &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 = IltmmWMScript_AddHeaderScript(pWMScript, 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 = IltmmWMScript_AddHeaderScript(pWMScript, L"caption", bstrScriptCmd, scriptTime);
         // free the bstr
         SysFreeString(bstrScriptCmd);
         // now get the header script count, should be 2 from above
         hr = IltmmWMScript_get_HeaderScriptCount(pWMScript, &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)
               {
                  IltmmWMScript_RemoveHeaderScript(pWMScript, i);
               }
               else if (i==1) // if second one, just get it and display a msg box with properties
               {
                  hr = IltmmWMScript_GetHeaderScript(pWMScript, 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);
                  }
               }
            }
         }
         IUnknown_Release(pWMScript);
      }
            
      // if all is well, run the convert now
      if (SUCCEEDED(hr))
         hr = IltmmConvert_StartConvert(pConvert);
      IUnknown_Release(punk);
   }
}