The following code demonstrates how to recompress an MP4 file:
#define MAKE_MEDIA_PATH(pFileName) (TEXT("C:\\LEADTOOLS Media Foundation\\Media\\")TEXT(pFileName))// include the LEAD Multimedia TOOLKIT header#include "ltmf.h"#include "resource.h"#include <tchar.h>#include <stdio.h>#include <assert.h>HINSTANCE g_hInstance; // application instance handleIltmfConvert* g_pConvert; // convert object's interface pointer// user defined message id used for conversion events#define WM_CONVERTNOTIFY (WM_USER + 1000)//// ConvertDlgProc// starts the conversion process and provides status feedback//// controls:// IDC_CONVERTSTATUS - static control used for status messages// IDC_CONVERTPROGRESS - static control used for conversion progress// IDC_USERABORT - button control used to abort the conversion or exit the dialogBOOL CALLBACK ConvertDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){TCHAR sz[256];BSTR bstr1;HRESULT hr;long streams;switch (msg){case WM_INITDIALOG:// assign the notification windowg_pConvert->SetNotifyWindow((long) hwnd, WM_CONVERTNOTIFY);// set the abort button textSetDlgItemText(hwnd, IDC_USERABORT, _T("Abort"));// start the conversionhr = g_pConvert->StartConvert();if(FAILED(hr)){bstr1 = ltmfGetErrorText(hr);_stprintf(sz, _T("%ls"), bstr1);SysFreeString(bstr1);UpdateWindow(GetDlgItem(hwnd, IDC_CONVERTSTATUS));SetDlgItemText(hwnd, IDC_USERABORT, _T("Exit"));MessageBeep(0);}g_pConvert->get_UnrenderedStreams(&streams);if(streams != 0)MessageBox(hwnd, _T("Not all the streams could be rendered."), _T("Convert"), MB_ICONEXCLAMATION | MB_OK);return TRUE;break;case WM_DESTROY:// reset the notification windowg_pConvert->SetNotifyWindow((long) NULL, 0);#ifdef _DEBUG{long state, err, pc;double start, end, dur;TCHAR sz[1024];// get the current stateg_pConvert->get_State(&state);// get the current stateg_pConvert->get_ConvertError(&err);// get the amount convertedg_pConvert->get_PercentComplete(&pc);// get the startg_pConvert->get_SelectionStart(&start);// get the endg_pConvert->get_SelectionEnd(&end);// get the durationg_pConvert->get_Duration(&dur);_stprintf(sz, _T("state = %d, error = 0x%.8X, complete = %d complete"), lParam);SetDlgItemText(hwnd, IDC_CONVERTPROGRESS, sz);UpdateWindow(GetDlgItem(hwnd, IDC_CONVERTPROGRESS));break;}#endifreturn TRUE;break;}return FALSE;}//// SetAudioType// Set the audio type into the target format if://void SetAudioType(IltmfTargetFormat* pTargetFormat){// get target audio formatsIltmfTargetAudioFormats* pTargetAudioFormats;pTargetFormat->get_AudioFormats(&pTargetAudioFormats);long v = -1;pTargetAudioFormats->get_Selection(&v);if (v < 0){pTargetAudioFormats->Release();return;}IltmfTargetAudioFormat* pTargetAudioFormat;pTargetAudioFormats->Item(v, &pTargetAudioFormat);pTargetAudioFormats->Release();// get target audio types collectionIltmfTargetAudioTypes* pTargetAudioTypes;pTargetAudioFormat->get_AudioTypes(&pTargetAudioTypes);pTargetAudioFormat->Release();long lCount = 0;pTargetAudioTypes->get_Count(&lCount);for ( long lIndex = 0 ; lIndex < lCount ; lIndex++){// get target audio type of indexIltmfTargetAudioType* pTargetAudioType;pTargetAudioTypes->Item(lIndex, &pTargetAudioType);long nAvgBytesPerSecond;long nBitsPerSample;long nNumChannels;long nSamplesPerSecond;pTargetAudioType->get_AudioAvgBytesPerSecond(&nAvgBytesPerSecond);pTargetAudioType->get_AudioBitsPerSample(&nBitsPerSample);pTargetAudioType->get_AudioNumChannels(&nNumChannels);pTargetAudioType->get_AudioSamplesPerSecond(&nSamplesPerSecond);// set the audio type into the target format if:// AudioAvgBytesPerSecond == 24000 (192 kbps)// AudioBitsPerSample == 16// AudioNumChannels == 2 (stereo)// AudioSamplesPerSecond == 44100 Hzif (nAvgBytesPerSecond == 24000 &&nBitsPerSample == 16 &&nNumChannels == 2 &&nSamplesPerSecond == 44100 ){pTargetFormat->SetAudioType(pTargetAudioType);pTargetAudioType->Release();break;}pTargetAudioType->Release();}pTargetAudioTypes->Release();}//// SetMP4Recompression// sets up H264 target video format, AAC target audio format, and MP4 file output//// hwndParent = parent window for target format property dialog boxes//HRESULT SetMP4Recompression(HWND hwndParent){long index;VARIANT_BOOL f;BSTR bstr;// set output format to MP4g_pConvert->put_TargetFormat(ltmfConvert_TargetFormat_MP4);#ifdef _DEBUG{long v;g_pConvert->get_TargetFormat(&v);assert(v == ltmfConvert_TargetFormat_MP4);}#endif// get target formatIltmfTargetFormats* pTargetFormats;IltmfTargetFormat* pTargetFormat;g_pConvert->get_TargetFormats(&pTargetFormats);long v;pTargetFormats->get_Selection(&v);pTargetFormats->Item(v, &pTargetFormat);pTargetFormats->Release();// get target video formatsIltmfTargetVideoFormats* pTargetVideoFormats;pTargetFormat->get_VideoFormats(&pTargetVideoFormats);// select the H264 target video formatbstr = SysAllocString(L"{34363248-0000-0010-8000-00AA00389B71}");pTargetVideoFormats->Find(bstr, &index);SysFreeString(bstr);if(index < 0){// video target format isn't registeredpTargetFormat->Release();pTargetVideoFormats->Release();return E_FAIL;}pTargetVideoFormats->put_Selection(index);pTargetVideoFormats->Release();// set Video bit rate to 500 KBpTargetFormat->put_VideoBitrate(500000);// set Video EncodeQuality to 10pTargetFormat->put_VideoEncodeQuality(10);// set Video Frame Width to 320pTargetFormat->put_VideoFrameWidth(320);// set Video Frame Height to 240pTargetFormat->put_VideoFrameHeight(240);// set Video Frame Rate to 30.0 fpspTargetFormat->put_VideoFrameRate(30.0);#ifdef _DEBUG{long v;pTargetFormat->get_VideoBitrate(&v);assert(v == 500000);pTargetFormat->get_VideoEncodeQuality(&v);assert(v == 10);pTargetFormat->get_VideoFrameWidth(&v);assert(v == 320);pTargetFormat->get_VideoFrameHeight(&v);assert(v == 240);double d;pTargetFormat->get_VideoFrameRate(&d);assert(d == 30.0);}#endif// get target audio formatsIltmfTargetAudioFormats* pTargetAudioFormats;pTargetFormat->get_AudioFormats(&pTargetAudioFormats);// select the AAC target audio formatbstr = SysAllocString(L"{00001610-0000-0010-8000-00AA00389B71}");pTargetAudioFormats->Find(bstr, &index);SysFreeString(bstr);if(index < 0){// audio target format isn't registeredpTargetFormat->Release();pTargetAudioFormats->Release();return E_FAIL;}pTargetAudioFormats->put_Selection(index);pTargetAudioFormats->Release();// set Audio Avg Bytes Per Second to 12000 (96 Kbps)pTargetFormat->put_AudioAvgBytesPerSecond(12000) ;// set Audio Bits Per Sample to 16pTargetFormat->put_AudioBitsPerSample(16);// set Audio Channels number to 2 (stereo)pTargetFormat->put_AudioNumChannels(2);// set Audio samples per second to 44100 (44.1 KHz)pTargetFormat->put_AudioSamplesPerSecond(44100);#ifdef _DEBUG{long v;pTargetFormat->get_AudioAvgBytesPerSecond(&v);assert(v == 12000);pTargetFormat->get_AudioBitsPerSample(&v);assert(v == 16);pTargetFormat->get_AudioNumChannels(&v);assert(v == 2);pTargetFormat->get_AudioSamplesPerSecond(&v);assert(v == 44100);}#endifpTargetFormat->Release();// set target format propertiesg_pConvert->HasDialog(ltmfConvert_Dlg_TargetFormat, &f);if(f)g_pConvert->ShowDialog(ltmfConvert_Dlg_TargetFormat, (long) hwndParent);return S_OK;}//// RecompressFile// recompresses a file using the MP4 target format//// pszSource = source file path// pszTarget = target file path//HRESULT RecompressFile(LPCTSTR pszSource, LPCTSTR pszTarget){HRESULT hr;BSTR bstr;#ifndef _UNICODEWCHAR wsz[MAX_PATH];#endif// set source file#ifdef _UNICODEbstr = SysAllocString(pszSource);#elseswprintf(wsz, L"%hs", pszSource);bstr = SysAllocString(wsz);#endifhr = g_pConvert->put_SourceFile(bstr);SysFreeString(bstr);if(FAILED(hr))return hr;// set target file#ifdef _UNICODEbstr = SysAllocString(pszTarget);#elseswprintf(wsz, L"%hs", pszTarget);bstr = SysAllocString(wsz);#endifhr = g_pConvert->put_TargetFile(bstr);SysFreeString(bstr);if(FAILED(hr))return hr;// setup AVI recompressionhr = SetMP4Recompression(NULL);if(FAILED(hr))return hr;// do conversionhr = (HRESULT) DialogBox(g_hInstance, (LPCTSTR)IDD_CONVERTDLG, NULL, ConvertDlgProc);return hr;}int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){HRESULT hr;g_hInstance = hInstance;// initialize COM libraryhr = CoInitialize(NULL);if(FAILED(hr))goto error;// create the convert objecthr = CoCreateInstance(CLSID_ltmfConvert, NULL, CLSCTX_INPROC_SERVER, IID_IltmfConvert, (void**) &g_pConvert);if(FAILED(hr))goto error;hr = RecompressFile(MAKE_MEDIA_PATH("source.MP4"), MAKE_MEDIA_PATH("target.MP4"));if(FAILED(hr))goto error;error:// cleanupif(g_pConvert)g_pConvert->Release();CoUninitialize();return 0;}