Encoding from any format to wmv/wma by encoder sdk by c++
please check the link
http://www.codeproject.com/Purgatory/ConvertVideoFileFormats.asp[^]
this in c# while i am in vc++
pEncoder->PrepareToEncode-failed most time
HRESULT Encoder :: convertintowma_wmv(WCHAR *source,WCHAR * destionation,WCHAR * w_encoderprofile,BOOL b_MultiPass,UINT top ,UINT bottom ,UINT left,UINT right,HACCEL hAccelTable)
{
HRESULT hr;
IWMEncoder* pEncoder;
IWMEncSourceGroupCollection* pSrcGrpColl=NULL;
IWMEncSourceGroup* pSrcGrp=NULL;
IWMEncSource* pSrc=NULL;
IWMEncSource* pSrcAud=NULL;
IWMEncVideoSource* pSrcVid=NULL;
IWMEncProfileCollection* pProColl=NULL;
IWMEncProfile* pPro=NULL;
IWMEncFile* pFile=NULL;
IWMEncAttributes* pAttr=NULL;
IWMEncDisplayInfo* pDispInfo=NULL;
CComBSTR bstrName = NULL;
CComVariant varIndex;
CCallBack EventSink;
WMENC_ENCODER_STATE enumCurState;
WMENC_ENCODER_STATE enumPrvState;
short iAudCount, iVidCount;
long lCount=0;
int i=0;
hr = CoInitialize(NULL);
if ( SUCCEEDED( hr ) )
{
hr = CoCreateInstance(CLSID_WMEncoder,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWMEncoder,
(void**) &pEncoder);
}else
{
MessageBox(0,L"FAIL CREATE instance",L"FAIL CREATE instance",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pEncoder->get_SourceGroupCollection(&pSrcGrpColl);
}else
{
MessageBox(0,L"pEncoder->get_SourceGroupCollection Fail",L"get_SourceGroupCollection",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pSrcGrpColl->Add(CComBSTR("SG_1"), &pSrcGrp);
}
else
{
MessageBox(0,L"pSrcGrpColl->Add",L"pSrcGrpColl->Add",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pSrcGrp->AddSource(WMENC_AUDIO, &pSrcAud);
}else
{
MessageBox(0,L"pSrcGrp->AddSource",L"pSrcGrp->AddSource",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pSrcGrp->AddSource(WMENC_VIDEO, &pSrc);
}else
{
MessageBox(0,L"pSrcGrp->AddSource(WMENC_VIDEO",L"pSrcGrp->AddSource(WMENC_VIDEO",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pSrc->QueryInterface(IID_IWMEncVideoSource, (void**)&pSrcVid);
}else
{
MessageBox(0,L"pSrc->QueryInterface",L"pSrc->QueryInterface",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pSrcAud->SetInput(source);
}else
{
MessageBox(0,L"pSrcAud->SetInput",L"pSrcAud->SetInput",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pSrcVid->SetInput(source);
}else
{
MessageBox(0,L"pSrcVid->SetInput(source);",L"pSrcVid->SetInput(source);",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pEncoder->get_DisplayInfo(&pDispInfo);
}else
{
MessageBox(0,L"pEncoder->get_DisplayInfo",L"pEncoder->get_DisplayInfo",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pDispInfo->put_Author(CComBSTR("Author Name"));
}else
{
MessageBox(0,L"pDispInfo->put_Author",L"pDispInfo->put_Author",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pDispInfo->put_Copyright(CComBSTR("Copyright"));
}else
{
MessageBox(0,L"pDispInfo->put_Copyright",L"pDispInfo->put_Copyright",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pDispInfo->put_Description(CComBSTR("A description"));
}else
{
MessageBox(0,L"pDispInfo->put_Description",L"pDispInfo->put_Description",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pDispInfo->put_Rating(CComBSTR("Rating"));
}else
{
MessageBox(0,L"pDispInfo->put_Rating",L"pDispInfo->put_Rating",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pDispInfo->put_Title(CComBSTR("The Title"));
}else
{
MessageBox(0,L"pDispInfo->put_Title",L"pDispInfo->put_Title",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pEncoder->get_Attributes(&pAttr);
}else
{
MessageBox(0,L"source group",L"source group ",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pAttr->Add(CComBSTR("URL"), CComVariant("IP Address"));
}else
{
MessageBox(0,L"pAttr->Add",L"pAttr->Add",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pEncoder->get_File(&pFile);
}else
{
MessageBox(0,L"pEncoder->get_File",L"pEncoder->get_File",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pFile->put_LocalFileName(destionation);
}else
{
MessageBox(0,L"pFile->put_LocalFileName",L"pFile->put_LocalFileName",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pEncoder->get_ProfileCollection(&pProColl);
}else
{
MessageBox(0,L"pEncoder->get_ProfileCollection",L"pEncoder->get_ProfileCollection",0);
}
if ( SUCCEEDED( hr ) )
{
hr = pProColl->get_Count(&lCount);
}else
{
MessageBox(0,L"pProColl->get_Count",L"pProColl->get_Count",0);
}
for (i=0; i<lCount; i++)
{
if ( SUCCEEDED( hr ) )
{
hr = pProColl->Item(i, &pPro);
}
if ( SUCCEEDED( hr ) )
{
hr = pPro->get_Name(&bstrName);
}
if (_wcsicmp(bstrName,w_encoderprofile)==0)
{
if ( SUCCEEDED( hr ) )
{
hr = pSrcGrp->put_Profile(CComVariant(pPro));
}else
{
MessageBox(0,L"pSrcGrp->put_Profile(CComVariant(pPro)",L"source group ",0);
}
break;
}
}
if ( SUCCEEDED( hr ) )
{
hr = pEncoder->PrepareToEncode(VARIANT_FALSE);
}else
{
MessageBox(0,L"pEncoder->PrepareToEncode",L"pEncoder->PrepareToEncode",0);
}
<code>if ( SUCCEEDED( hr ) )
{
hr = pEncoder->PrepareToEncode(VARIANT_TRUE);
}else
{
MessageBox(0,L"PrepareToEncode fail",L"PrepareToEncode fail",0);
}</code> if ( SUCCEEDED( hr ) )
{
hr = pEncoder->Start();
if ( SUCCEEDED( hr ) )
{
bool glbboolStartNext=false;
bool b_forfirsttime=false;
MSG msg;
while(!glbboolStartNext)
{
while(PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(b_forfirsttime)
{
enumPrvState = enumCurState;
}
b_forfirsttime=true;
enumCurState = EventSink.State();
if( EventSink.SrcState() == WMENC_SOURCE_STOP )
enumCurState = WMENC_ENCODER_STOPPED;
{
switch ( enumCurState )
{
case WMENC_ENCODER_STARTING:
break;
case WMENC_ENCODER_RUNNING:
break;
case WMENC_ENCODER_PAUSING:
break;
case WMENC_ENCODER_PAUSED:
break;
case WMENC_ENCODER_STOPPING:
break;
case WMENC_ENCODER_STOPPED:
glbboolStartNext=true;
goto EXIT;
case WMENC_ENCODER_END_PREPROCESS:
break;
}
}
}
EXIT:
hr = pEncoder->Stop();
hr = EventSink.Shutdown( pEncoder );
}
}
varIndex.vt = VT_I2;
if ( SUCCEEDED( hr ) )
{
hr = pSrcGrpColl->get_Count(&lCount);
}
for (i=0; i<lCount; i++)
{
varIndex.iVal = i;
hr = pSrcGrpColl->Remove(varIndex);
}
{
hr = pSrcGrp->get_SourceCount(WMENC_AUDIO, &iAudCount);
}
{
hr = pSrcGrp->get_SourceCount(WMENC_VIDEO, &iVidCount);
}
varIndex.vt = VT_I2;
varIndex.iVal = 0;
if (iVidCount != 0)
{
{
hr = pSrcGrp->RemoveSource(WMENC_VIDEO, varIndex);
}
}
if(iAudCount!=0)
{
hr = pSrcGrp->RemoveSource(WMENC_AUDIO, varIndex);
}
{
hr = pEncoder->PrepareToEncode(VARIANT_FALSE);
}
if ( pSrcGrpColl )
{
pSrcGrpColl->Release();
pSrcGrpColl = NULL;
}
if ( pSrcGrp )
{
pSrcGrp->Release();
pSrcGrp = NULL;
}
if ( pProColl )
{
pProColl->Release();
pProColl = NULL;
}
if ( pPro )
{
pPro->Release();
pPro = NULL;
}
if ( pFile )
{
pFile->Release();
pFile = NULL;
}
if ( pSrcAud )
{
pSrcAud->Release();
pSrcAud = NULL;
}
if ( pSrcVid )
{
pSrcVid->Release();
pSrcVid = NULL;
}
if ( pSrc )
{
pSrc->Release();
pSrc = NULL;
}
if ( pAttr )
{
pAttr->Release();
pAttr = NULL;
}
if ( pDispInfo )
{
pDispInfo->Release();
pDispInfo = NULL;
}
if ( pEncoder )
{
pEncoder->Release();
pEncoder = NULL;
}
CoUninitialize();
return(hr);
}
"Success lies not in the result , But in the efforts !!!!!"
Amit Mistry - petlad -Gujarat-India
|