1.9w+
社区成员
// media5.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <atlbase.h>
#include <comdef.h>
#include "C:\WMSDK\WMEncSDK9\include\wmencode.h"
#include "C:\WMSDK\WMEncSDK9\include\wmdevctl.h"
#include <conio.h> // for kbhit()
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr;
IWMEncoder2* pEncoder;
IWMEncSourceGroupCollection* pSrcGrpColl;
IWMEncSourceGroup* pSrcGrp;
IWMEncSourceGroup2* pSrcGrp2;
IWMEncSource* pSrcAud;
IWMEncSource* pSrcVid;
IWMEncFile* pFile;
IWMEncProfileCollection* pProColl;
IWMEncProfile* pPro;
IWMEncDeviceControlCollection* pDCColl;
IWMEncDeviceControl* pDControl;
IWMEncDeviceControlPlugin* pDCPlugin;
IWMEncEditDecisionList* pEDList;
IWMEncEditDecisionData* pEDData;
CComBSTR bstrName = NULL;
long lCount;
int i;
// Initialize the COM library and retrieve a pointer
// to an IWMEncoder interface.
hr = CoInitialize(NULL);
if ( SUCCEEDED( hr ) )
{
hr = CoCreateInstance(CLSID_WMEncoder,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWMEncoder2,
(void**) &pEncoder);
}
// Retrieve the source group collection.
if ( SUCCEEDED( hr ) )
{
hr = pEncoder->get_SourceGroupCollection(&pSrcGrpColl);
}
// Encode to a file.
if ( SUCCEEDED( hr ) )
{
hr = pEncoder->get_File(&pFile);
}
if ( SUCCEEDED( hr ) )
{
hr = pFile->put_LocalFileName(CComBSTR("C:\\DeviceOutput2.wmv"));
}
// Add a source group to the collection.
if ( SUCCEEDED( hr ) )
{
hr = pSrcGrpColl->Add(CComBSTR("SG_1"), &pSrcGrp);
}
if ( SUCCEEDED( hr ) )
{
hr = pSrcGrp->QueryInterface(IID_IWMEncSourceGroup2, (void**)&pSrcGrp2);
}
// Add a video and audio source to the source group.
if ( SUCCEEDED( hr ) )
{
// hr = pSrcGrp->AddSource(WMENC_AUDIO, &pSrcAud);
}
if ( SUCCEEDED( hr ) )
{
hr = pSrcGrp->AddSource(WMENC_VIDEO, &pSrcVid);
}
// Add the device as the audio source and video source.
if ( SUCCEEDED( hr ) )
{
//hr = pSrcAud->SetInput(CComBSTR("Device://Realtek AC97 Audio"));
}
if ( SUCCEEDED( hr ) )
{
hr = pSrcVid->SetInput(CComBSTR("Device://USB PC Camera 301P"));
}
// Choose a profile from the collection.
if ( SUCCEEDED( hr ) )
{
hr = pEncoder->get_ProfileCollection(&pProColl);
}
if ( SUCCEEDED( hr ) )
{
hr = pProColl->get_Count(&lCount);
}
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,CComBSTR("Windows Media Video 8 for Local Area Network (384 Kbps)"))==0)
{
// Set the profile in the source group.
if ( SUCCEEDED( hr ) )
{
hr = pSrcGrp->put_Profile(CComVariant(pPro));
}
break;
}
}
// Retrieve the device control collection, then add a device to it.
if ( SUCCEEDED( hr ) )
{
hr = pSrcGrp2->get_DeviceControlCollection(&pDCColl);
}
if ( SUCCEEDED( hr ) )
{
hr = pDCColl->Add(&pDControl);
}
if ( SUCCEEDED( hr ) )
{
hr = pDControl->SetInput(CComBSTR("DeviceControl://USB PC Camera 301P"));
}
// Initialize the encoding session.
if ( SUCCEEDED( hr ) )
{
hr = pEncoder->PrepareToEncode(VARIANT_TRUE);
}
// Get the plug-in from the device.
if ( SUCCEEDED( hr ) )
{
hr = pDControl->GetDeviceControlPlugin((IUnknown**)&pDCPlugin);
}
// Retrieve an IWMEncEditDecisionList interface from the device control plug-in.
if ( SUCCEEDED( hr ) )
{
hr = pDCPlugin->get_EditDecisionList(&pEDList);
}
// Create an EDL entry.
if ( SUCCEEDED( hr ) )
{
hr = pEDList->Add(&pEDData);
}
// Add attributes to the EDL entry specifying mark-in, mark-out,
// tape ID, and description information.
if ( SUCCEEDED( hr ) )
{
hr = pEDData->Add(CComBSTR("MarkIn"), CComVariant(262295));
}
if ( SUCCEEDED( hr ) )
{
hr = pEDData->Add(CComBSTR("MarkOut"), CComVariant(267268));
}
if ( SUCCEEDED( hr ) )
{
hr = pEDData->Add(CComBSTR("TapeID"), CComVariant("A"));
}
if ( SUCCEEDED( hr ) )
{
hr = pEDData->Add(CComBSTR("Description"), CComVariant("Scene 1"));
}
// Wait for the user to press a key to start encoding.
printf("Press a key to start encoding.");
// Wait for a key press.
while(!kbhit())
_asm nop;
// Start encoding.
if ( SUCCEEDED( hr ) )
{
hr = pEncoder->Start();
// Stop the encoding process.
if ( SUCCEEDED( hr ) )
{
// Wait for the user to press a key to stop encoding.
printf("Press a key to stop encoding.");
// Wait for a key press.
while(!kbhit())
_asm nop;
hr = pEncoder->Stop();
}
}
printf("\n%x",hr);
// Release pointers.
if ( pSrcGrpColl )
{
pSrcGrpColl->Release();
pSrcGrpColl = NULL;
}
if ( pSrcGrp )
{
pSrcGrp->Release();
pSrcGrp = NULL;
}
if ( pSrcGrp2 )
{
pSrcGrp2->Release();
pSrcGrp2 = NULL;
}
//if ( pSrcAud )
//{
// pSrcAud->Release();
// pSrcAud = NULL;
//}
if ( pSrcVid )
{
pSrcVid->Release();
pSrcVid = NULL;
}
if ( pFile )
{
pFile->Release();
pFile = NULL;
}
if ( pProColl )
{
pProColl->Release();
pProColl = NULL;
}
if ( pPro )
{
pPro->Release();
pPro = NULL;
}
if ( pDCColl )
{
pDCColl->Release();
pDCColl = NULL;
}
if ( pDControl )
{
pDControl->Release();
pDControl = NULL;
}
if ( pDCPlugin )
{
pDCPlugin->Release();
pDCPlugin = NULL;
}
if ( pEDList )
{
pEDList->Release();
pEDList = NULL;
}
if ( pEDData )
{
pEDData->Release();
pEDData = NULL;
}
if ( pEncoder )
{
pEncoder->Release();
pEncoder = NULL;
}
return 0;
}