Code Sample
Code sample showing how to interface MP4Writer filter programmatically
#include <streams.h>
#include "imp4props.h" // for put_IVOPQuant()
// Declarations...
const GUID FAR CLSID_MP4Filter = {0x38b89f93, 0xe2c1, 0x4aec, 0x95, 0x79, 0xfd,
0x4e, 0xd, 0x5d, 0x2a, 0x77};
// optional interface
const GUID FAR IID_IMP4Props = {0x7c6ecc69, 0xb82a, 0x4613, 0x8b, 0x75, 0xb9, 0x55,
0xd5, 0x5c, 0x21, 0xc5};
IGraphBuilder *m_pGB;
IBaseFilter *m_pMP4;
IBaseFilter *m_pSrcFilter;
IBaseFilter
*m_pPropsFilter;
IFileSinkFilter *m_pMP4FileSink;
IMediaControl *m_pMC;
#define SAFE_RELEASE(i) {if (i) i->Release(); i = NULL;}
#define CHECK_RESULT(i) if (i != S_OK) goto CLEANUP;
....
// Initialization
CoInitialize(NULL);
CHECK_RESULT(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder,
(void **)&m_pGB));
CHECK_RESULT(CoCreateInstance(CLSID_MP4Filter, NULL, CLSCTX_INPROC, IID_IBaseFilter,
(void **)&m_pMP4));
CHECK_RESULT(m_pMP4->QueryInterface(IID_IFileSinkFilter, (void **)&m_pMP4FileSink));
CHECK_RESULT(m_pMP4->QueryInterface(IID_IMP4Props, (void **)&m_pPropsFilter));
CHECK_RESULT(m_pMP4FileSink->SetFileName(L"C:\\DestVideo.mp4", NULL));
CHECK_RESULT(((IMP4Props*)m_pPropsFilter)->put_IVOPQuant(4));
CHECK_RESULT(m_pGB->AddFilter(m_pMP4, L"My MP4 Writer"));
CHECK_RESULT(m_pGB->RenderFile(L"C:\\SourceVideo.mpg", NULL));
int arate[2];
CHECK_RESULT(((IMP4Props*)m_pPropsFilter)->get_AudioBitrateSelection(arate));
CHECK_RESULT(((IMP4Props*)m_pPropsFilter)->put_AudioBitrate(
arate[1]));
CHECK_RESULT(m_pGB->QueryInterface(IID_IMediaControl,
(void **)&m_pMC));
// Start transcoding
CHECK_RESULT(m_pMC->Run());
......
// Stop transcding and clean up
CLEANUP:
HRESULT hr = m_pMC->Stop();
SAFE_RELEASE(m_pPropsFilter);
SAFE_RELEASE(m_pMC);
SAFE_RELEASE(m_pMP4FileSink);
SAFE_RELEASE(m_pSrcFilter);
SAFE_RELEASE(m_pMP4);
SAFE_RELEASE(m_pGB);
CoUninitialize();
.....
//include file with additional interface - not needed to run graph
#ifndef __IMP4PROPS__
#define __IMP4PROPS__
#ifdef __cplusplus extern "C"
{
#endif
DEFINE_GUID(IID_IMP4Props, 0x7c6ecc69, 0xb82a, 0x4613, 0x8b, 0x75, 0xb9, 0x55, 0xd5,
0x5c, 0x21, 0xc5);
// IMP4Props //
DECLARE_INTERFACE_(IMP4Props, IUnknown)
{
STDMETHOD(get_VideoBitrate) (THIS_ int *iVideoBirate /* [out] */) PURE;
STDMETHOD(put_VideoBitrate) (THIS_ int iVideoBirate /* [in] */ ) PURE;
STDMETHOD(get_AudioBitrate) (THIS_ int *iAudioBitrate /* [out] */) PURE;
STDMETHOD(put_AudioBitrate) (THIS_ int iAudioBitrate /* [in] */ ) PURE;
STDMETHOD(get_IVOPQuant) (THIS_ int *iLevel /* [out] */ ) PURE;
STDMETHOD(put_IVOPQuant) (THIS_ int iLevel /* [in] */ ) PURE;
STDMETHOD(get_PVOPQuant) (THIS_ int *iLevel /* [out] */ ) PURE;
STDMETHOD(put_PVOPQuant) (THIS_ int iLevel /* [in] */ ) PURE;
STDMETHOD(set_AudioBitrateSelection)(THIS_ int iIndex )PURE;
STDMETHOD(get_AudioBitrateSelection)(THIS_ int* iAudioBitrateTable ) PURE;
#ifdef __cplusplus
}
#endif
#endif // __IMP4PROPS__