Convert Video to MP4 - Windows C++

This tutorial shows how to use the LEADTOOLS Multimedia SDK to create a C++ Windows API application that uses IltmmConvert to perform a simplified conversion of a video file to the MP4 format.

Overview  
Summary This tutorial covers how to convert a video file in a C++ Windows application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (18 KB)
Platform Windows C++ Application
IDE Visual Studio 2019
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Before working on the Convert Video to MP4 - Windows C++ tutorial, get familiar with the steps to create a C++ project by reviewing the Add References and Set a License tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the 64-bit Windows API project created in the Add References and Set a License tutorial. If the project is not available, create it by following the steps in that tutorial.

In order to convert video files, LEADTOOLS requires additional references. Add the required Multimedia library reference by opening the pre-compiled header file, either pch.h or stdafx.h depending on the Visual Studio version used, and add the following line:

// Add LEADTOOLS Multimedia reference 
#import "c:\Windows\SysWOW64\ltmm22x.dll" no_namespace, named_guids 

Note

For a complete list of which DLLs are required for specific multimedia features, refer to Multimedia Files You Must Include With Your DirectShow-based Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add Initialization and Cleanup Code

Now that the LEADTOOLS references have been added and the license has been set, coding can begin.

Using the Solution Explorer navigate to the project's CPP file, which contains the WndProc function for the main window. Add the following declaration to the global variables near the top:

// Global Variables: 
IltmmConvert* pConvert = NULL; 
const UINT WM_CONVERT_NOTIFY = WM_USER + 50; // custom Window message to handle conversion notifications 

Navigate to the WndProc function, which should have a case WM_DESTROY inside it. Add a new case WM_CREATE if it is not present and add the following code under it:

case WM_CREATE: 
{ 
   HRESULT hr = CoInitialize(NULL); 
   if FAILED(hr) 
   { 
      MessageBox(hWnd, TEXT("Error starting up COM library..\nAborting"), TEXT("LEADTOOLS Demo"), MB_ICONERROR); 
      return -1; 
   } 
   hr = CoCreateInstance(CLSID_ltmmConvert, NULL, CLSCTX_INPROC_SERVER, IID_IltmmConvert, (void**)&pConvert); 
   if FAILED(hr) 
   { 
      MessageBox(hWnd, TEXT("Error creating Convert control..\nAborting"), TEXT("LEADTOOLS Demo"), MB_ICONERROR); 
      return -1; 
   } 
   // Define our notification Window Message 
   pConvert->SetNotifyWindow((long)hWnd, WM_CONVERT_NOTIFY); 
   break; 
} 

Modify the case that handles WM_DESTROY in the WndProc() function to contain the following lines:

case WM_DESTROY: 
   if (pConvert) 
      pConvert->Release(); 
   CoUninitialize(); 
   PostQuitMessage(0); 
   break; 

Add the Convert Video Code

The steps below are for Visual Studio 2019; they could be different for other versions of Visual Studio.

Go to the Solution Explorer and double-click the resources file (.rc). Expand the menu tab in the resources tree and double-click the menu resource to open it in the designer interface. In the empty item below the Exit item, click and type &Convert to MP4.... Drag the new item above Exit. Ensure the item's ID is ID_FILE_CONVERTTOMP4.

Open the project's CPP file and navigate back to the WndProc function and under the switch (wmId) statement, that is below the WM_COMMAND case, add the new case below.

// In WndProc(), under "case WM_COMMAND:" 
switch (wmId) 
{ 
   case ID_FILE_CONVERTTOMP4: 
   { 
      TCHAR szFileIn[260] = TEXT(""); // Input file name   
      TCHAR szFileOut[260] = TEXT(""); // Output file name   
 
      // Choose output file name  
      if (!GetSourceFileName(hWnd, szFileIn, ARRAYSIZE(szFileIn))) 
         break; 
      // copy input file name to output 
      _tcscpy_s(szFileOut, ARRAYSIZE(szFileOut), szFileIn); 
      // append ".mp4" to output file name 
      _tcscat_s(szFileOut, TEXT(".mp4")); 
      pConvert->sourcefile = szFileIn; 
      pConvert->TargetFile = szFileOut; 
      pConvert->TargetFormat = ltmmConvert_TargetFormat_ISO; 
 
      // Select the LEAD H264 video compressor 
      int nVideoCodec = pConvert->VideoCompressors->Find("@device:sw:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\\LEAD H264 Encoder (4.0)"); 
      pConvert->VideoCompressors->Selection = nVideoCodec; 
 
      // Select the LEAD AAC audio compressor 
      int nAudioCodec = pConvert->AudioCompressors->Find(L"@device:sw:{33D9A761-90C8-11D0-BD43-00A0C911CE86}\\{E2B7DD70-38C5-11D5-91F6-00104BDB8FF9}"); 
      pConvert->AudioCompressors->Selection = nAudioCodec; 
 
      pConvert->AllowedStreams = ltmmConvert_Stream_Audio | ltmmConvert_Stream_Video; 
      MessageBox(hWnd, TEXT("Click OK to begin conversion.\nAnother message box will appear when finished. Do not exit application before that!"), TEXT("Convert Demo"), MB_ICONINFORMATION); 
      pConvert->StartConvert(); 
   } 
   break; 
   // Keep rest of the code as is 

To display a File Open dialog to obtain the file name, create a new function named GetSourceFileName() and add the code below. The GetSourceFileName() function can be any function that fills the pszFileName variable with a valid input file name (including full path, if needed).

bool GetSourceFileName(HWND hwnd, TCHAR* pszFileName, DWORD nLen) 
{ 
   OPENFILENAME OpenFileName = { 0 }; 
   OpenFileName.lStructSize = sizeof OpenFileName; 
   OpenFileName.hwndOwner = hwnd; 
   OpenFileName.lpstrFile = pszFileName; 
   OpenFileName.nMaxFile = nLen; 
   OpenFileName.lpstrFilter = TEXT("Video Files\0*.avi;*.ogg;*.mkv;*.mpg;*.wmv\0"); 
   OpenFileName.nFilterIndex = 1; 
   OpenFileName.lpstrFileTitle = NULL; 
   OpenFileName.nMaxFileTitle = 0; 
   OpenFileName.lpstrInitialDir = NULL; 
   OpenFileName.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; 
 
   // Show the File Open dialog box   
   if (!GetOpenFileName(&OpenFileName)) 
      return false; 
   return true; 
} 

In the WndProc function, add a new case WM_CONVERT_NOTIFY under switch (message) to handle the Convert Control's notifications. Add the following code under it:

case WM_CONVERT_NOTIFY: 
{ 
   switch (wParam) 
   { 
   case ltmmConvert_Notify_Complete: 
      pConvert->ResetSource(); 
      pConvert->ResetTarget(); 
      MessageBox(hWnd, TEXT("Finished converting."), TEXT("Convert Demo"), MB_ICONINFORMATION); 
      InvalidateRect(hWnd, NULL, TRUE); 
      break; 
   case ltmmConvert_Notify_ErrorAbort: 
      MessageBox(hWnd, TEXT("Error converting."), TEXT("Convert Demo"), MB_ICONERROR); 
      break; 
   case ltmmConvert_Notify_Progress: 
   { 
      TCHAR szProgress[1024]; 
      _stprintf_s(szProgress, ARRAYSIZE(szProgress), TEXT("Converting... %d%%    "), (int)lParam); 
      HDC hDC = GetDC(hWnd); 
      TextOut(hDC, 50, 50, szProgress, _tcslen(szProgress)); 
      ReleaseDC(hWnd, hDC); 
   } 
   } 
} 
break; 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps are followed correctly, the project builds and the application runs. Select File -> Convert To MP4 to select a multimedia source video file and convert it to the MP4 (ISO) file format. The output file is saved to the same location as the source file with an ".mp4" extension appended to its name.

Wrap-up

This tutorial covered how to use a convert control to produce MP4 video files from other multimedia formats while handling conversion progress notifications.

See Also

Help Version 22.0.2022.9.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2022 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2022 LEAD Technologies, Inc. All Rights Reserved.