Save the Captured Data As MPEG

1.

Create an instance of the ltmmCapture class. This is accomplished using the Win32 CoCreateInstance function as follows:

C Source

IltmmCapture* pCapture;
CoCreateInstance(&CLSID_ltmmCapture, NULL, CLSCTX_INPROC_SERVER, &IID_IltmmCapture, (void**) &pCapture);

C++ Source

IltmmCapture* pCapture; 
CoCreateInstance(CLSID_ltmmCapture, NULL, CLSCTX_INPROC_SERVER, IID_IltmmCapture, (void**) &pCapture);

2.

Define video devices to be used as the capture source. The ltmmCapture object contains video device collection objects.

3.

The first video device can be selected for capture by calling the device collection’s put_Selection function:

C Source

IltmmDevices* pDevices;

// get an interface into video devices collection
IltmmCapture_get_VideoDevices(pCapture, &pDevices);

// select the first video device
IltmmDevices_put_Selection (pDevices, 0);

// release collection
IUnknown_Release(pDevices);

C++ Source

IltmmDevices* pDevices;

// get an interface into video devices collection
pCapture->get_VideoDevices(&pDevices);

// select device
pDevices->put_Selection(0);

// release collection
pDevices->Release();

4.

Define the target or output file, as demonstrated with the following code:

C Source

BSTR bstr; 

// create a string containing the target file path
bstr = SysAllocString(L"c:\\target.mpg"); 

// assign the target file path to the capture object
IltmmCapture_put_TargetFile (pCapture, bstr); 

// free the string
SysFreeString(bstr);

C++ Source

BSTR bstr; 

// create a string containing the target file path
bstr = SysAllocString(L"c:\\target.mpg"); 

// assign the target file path to the capture object
pCapture->put_TargetFile (bstr); 

// free the string
SysFreeString(bstr);

 

5.

Select the Video and Audio compressors, as demonstrated with the following code:

C Source

long index;
IltmmCompressors* pCompressors;

// select the MS MPEG-4 Video Codec

IltmmCapture_get_VideoCompressors(pCapture, &pCompressors);
bstr = SysAllocString(L"@device:sw:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\\Microsoft MPEG-4 Video Codec V2");
IltmmCompressors_Find(pCompressors, bstr, &index); 
SysFreeString(bstr); 
if(index >= 0) 
   IltmmCompressors_put_Selection(pCompressors, index); 
IUnknown_Release(pCompressors); 


// select the MP3 audio video compressor

IltmmCapture_get_AudioCompressors(pCapture, &pCompressors);
bstr = SysAllocString(L"@device:cm:{33D9A761-90C8-11D0-BD43-00A0C911CE86}\\85MPEG Layer-3");
IltmmCompressors_Find(pCompressors, bstr, &index); 
SysFreeString(bstr); 
if(index >= 0) 
   IltmmCompressors_put_Selection(pCompressors, index); 
IUnknown_Release(pCompressors); 

C++ Source

IltmmCompressors* pCompressors;
long index;

// select the MS MPEG-4 Video Codec
pCapture->get_VideoCompressors (&pCompressors);
bstr = SysAllocString(L"@device:sw:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\\Microsoft MPEG-4 Video Codec V2");
pCompressors->Find (bstr, &index);
SysFreeString(bstr);
if(index < 0)
{
   // compressor isn't registered
   pCompressors->Release ();
   return E_FAIL;
}
pCompressors->put_Selection (index);
pCompressors->Release();


// select the MP3 audio video compressor
pCapture->get_AudioCompressors (&pCompressors);
bstr = SysAllocString(L"@device:cm:{33D9A761-90C8-11D0-BD43-00A0C911CE86}\\85MPEG Layer-3");
pCompressors->Find (bstr, &index);
SysFreeString(bstr);
if(index < 0)
{
   // compressor isn't registered
   pCompressors->Release ();
   return E_FAIL;
}
pCompressors->put_Selection (index);
pCompressors->Release();

6.

To view the graph in DirectShow GraphEdit

C Source

IltmmCapture_EditGraph(pCapture);

C++ Source

pCapture->EditGraph();

7.

You should set the MPEG multiplexer. This example will use an AVI multiplexer because it is available on all computers. You should use a real MPEG multiplexer, as demonstrated with the following code:

C Source

IltmmTargetFormats* pTargetFormats;
IltmmTargetFormat* pTargetFormat;

IltmmCapture_get_TargetFormats(pCapture, &pTargetFormats);


// retrieve collection item
IltmmTargetFormats_Item(pTargetFormats, 0, &pTargetFormat);


// assign the required Mux
// this case: AVI
bstr = SysAllocString(L"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\\{E2510970-F137-11CE-8B67-00AA00A3F1A6}");
IltmmTargetFormat_put_Mux(pTargetFormat, bstr); 
SysFreeString(bstr); 

// assign the optional SinkSubType
// this case: MEDIASUBTYPE_Avi
bstr = SysAllocString(L"{E436EB88-524F-11CE-9F53-0020AF0BA770}");
IltmmTargetFormat_put_SinkSubType(pTargetFormat, bstr); 
SysFreeString(bstr); 

// assign an optional Sink
// this case: default
bstr = SysAllocString(L"");
IltmmTargetFormat_put_Sink(pTargetFormat, bstr); 
SysFreeString(bstr); 


IUnknown_Release(pTargetFormat);

C++ Source

IltmmTargetFormats* pTargetFormats;
IltmmTargetFormat* pTargetFormat;

pCapture->get_TargetFormats(&pTargetFormats);


// retrieve collection item
pTargetFormats->Item(0, &pTargetFormat);


// assign the required Mux
// this case: AVI
bstr = SysAllocString(L"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\\{E2510970-F137-11CE-8B67-00AA00A3F1A6}");
pTargetFormat->put_Mux(bstr); 
SysFreeString(bstr); 

// assign the optional SinkSubType
// this case: MEDIASUBTYPE_Avi
bstr = SysAllocString(L"{E436EB88-524F-11CE-9F53-0020AF0BA770}");
pTargetFormat->put_SinkSubType(bstr);
SysFreeString(bstr); 

// assign an optional Sink
// this case: default
bstr = SysAllocString(L"");
pTargetFormat->put_Sink(bstr); 
SysFreeString(bstr); 


pTargetFormat->Release();

8.

You should set a frame rate supported by MPEG. Look at the documentation for your MPEG multiplexer to see which frame rates it supports. Common frame rates are: 23.976, 24, 25, 29.97, 30fps, as demonstrated with the following code:

C Source

// set rate to 30 fps
IltmmCapture_put_UseFrameRate(pCapture, VARIANT_TRUE);
IltmmCapture_put_FrameRate(pCapture, 30.0);
// you should check with their particular compressor which frame rates are supported.

C++ Source

// set rate to 30 fps
pCapture->put_UseFrameRate(VARIANT_TRUE);
pCapture->put_FrameRate(30.0);
// you should check with their particular compressor which frame rates are supported.

9.

Now you should start to capture the data and save it to the target file by calling the following function:

C Source

// start running
IltmmCapture_StartCapture(pCapture, ltmmCapture_Mode_VideoAndAudio); 

C++ Source

// start running
pCapture->StartCapture(ltmmCapture_Mode_VideoAndAudio);