Convert Video to HEVC / H265 and AVC / H264 - Windows C++

This tutorial shows how to use the LEADTOOLS Multimedia SDK to convert a video using High Efficiency Video Coding (HEVC) or Advanced Video Coding (AVC) with the H.265 and H.264 codecs in C++ using a Windows C DLL application.

Overview  
Summary This tutorial shows how to use the LEADTOOLS Multimedia SDK to convert a video using High Efficiency Video Coding or Advanced Video Coding (HEVC / H265 or AVC / H264).
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform Windows C++ Application
IDE Visual Studio 2019+
Runtime License Download LEADTOOLS

Required Knowledge

Before working on the Convert Video to HEVC / H265 and AVC / H264 tutorial, complete the Add References and Set a License tutorial.

Create the Project and Add the Multimedia Headers and LIB Files

In Visual Studio 2019, select Create a new project. Select the Windows Desktop Wizard template, then click Next. Configure the project by adding the project name and selecting the location for the project to be saved to, then click Create. Set the Application type to Console Application (.exe), enable Precompiled headers, then click OK.

Open the pre-compiled headers file pch.h of the project and add the following lines after the #define PCH_H line:

// Add LEADTOOLS Multimedia Headers and Libs 
#include "C:\LEADTOOLS23\Include\ltmm.h" 
#include "C:\LEADTOOLS23\Include\ltmm_Errors.h" 
 
//x64 libs 
#pragma comment (lib, "C:\\LEADTOOLS23\\Lib\\CDLL\\x64\\ltmmx.lib") 
#pragma comment (lib, "C:\\LEADTOOLS23\\Lib\\CDLL\\x64\\ltmmuuidx.lib") 

Add the Project Header File

Add a new file to the project named <PROJECT-NAME>.h. In this file, add the following method definitions:

#include "pch.h" 
 
void WaitForCompletion(IltmmConvert* pConvert); 
 
void SelectCompressor(IltmmCompressors* pCompressors, LPCWSTR pszCompressorName); 
 
HRESULT ConvertFile(IltmmConvert* pConvert, LPCWSTR pszVideoCompressorName, LPCWSTR pszAudioCompressorName); 

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 Code to Convert the File

Open up the <PROJECT-NAME>.cpp source file. This file contains the main function and program execution begins and ends with this file.

Replace the contents of this file with the following code:

//This file contains the 'main' function. Program execution begins and ends here. 
 
#include "pch.h" 
#include "Convert-Video-to-HEVC-H265-H264.h" 
 
#include <iostream> 
#define LEAD_H265_ENCODER L"@device:sw:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\\LEAD H265 Encoder" 
#define LEAD_H264_ENCODER L"@device:sw:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\\LEAD H264 Encoder (4.0)" 
#define LEAD_AAC_AUDIO_ENCODER L"@device:sw:{33D9A761-90C8-11D0-BD43-00A0C911CE86}\\{E2B7DD70-38C5-11D5-91F6-00104BDB8FF9}" 
 
 
// Main functionality of the program 
int main() 
{ 
   const L_TCHAR* pszLicenseFile = L_TEXT("C:\\LEADTOOLS23\\Support\\Common\\License\\LEADTOOLS.lic"); 
   const L_TCHAR* pszDeveloperKey = L_TEXT("PLACE DEVELOPER KEY HERE"); 
   L_SetLicenseFile((L_TCHAR*)pszLicenseFile, (L_TCHAR*)pszDeveloperKey); 
   L_BOOL LicenseFailed = L_IsSupportLocked(L_SUPPORT_BASIC); 
   if (LicenseFailed) 
   { 
      std::cout << "License file invalid or expired.. Aborting\n"; 
      return -1; 
   } 
 
   std::cout << "Starting Convert...\n"; 
 
   // Initialize the convert control 
   CoInitialize(NULL); 
   IltmmConvert* pConvert; 
   HRESULT hr = CoCreateInstance(CLSID_ltmmConvert, NULL, CLSCTX_INPROC_SERVER, IID_IltmmConvert, (void**)&pConvert); 
   if (FAILED(hr)) 
      return hr; 
 
   // Set which source file to convert from 
   BSTR bstr = ::SysAllocString(TEXT("C:\\LEADTOOLS23\\Resources\\Media\\DaDa_CMP.avi")); 
   pConvert->put_SourceFile(bstr); 
   SysFreeString(bstr); 
 
   //Set the output file's location and name 
   bstr = ::SysAllocString(TEXT("C:\\LEADTOOLS23\\Resources\\Media\\out-h265.avi")); 
   pConvert->put_TargetFile(bstr); 
   SysFreeString(bstr); 
 
   // Convert to h265 
   ConvertFile(pConvert, LEAD_H265_ENCODER, LEAD_AAC_AUDIO_ENCODER); 
 
   //Set the output file's location and name to a different output file 
   bstr = ::SysAllocString(TEXT("C:\\LEADTOOLS23\\Resources\\Media\\out-h264.avi")); 
   pConvert->put_TargetFile(bstr); 
   SysFreeString(bstr); 
 
   // Convert to h264 
   ConvertFile(pConvert, LEAD_H264_ENCODER, LEAD_AAC_AUDIO_ENCODER); 
 
   //Free up resources 
   pConvert->Release(); 
} 
 
HRESULT ConvertFile(IltmmConvert* pConvert, LPCWSTR pszVideoCompressorName, LPCWSTR pszAudioCompressorName) 
{ 
   //Set the target format to AVI 
   pConvert->put_TargetFormat(ltmmConvert_TargetFormat_Avi); 
 
   //Set the Video Compressor 
   IltmmCompressors* pCompressors; 
   pConvert->get_VideoCompressors(&pCompressors); 
   SelectCompressor(pCompressors, pszVideoCompressorName); 
   pCompressors->Release(); 
 
   //Set the Audio Compressor 
   pConvert->get_AudioCompressors(&pCompressors); 
   SelectCompressor(pCompressors, pszAudioCompressorName); 
   pCompressors->Release(); 
 
   //Run the conversion 
   HRESULT hr = pConvert->StartConvert(); 
   WaitForCompletion(pConvert); 
   return hr; 
} 
 
//Method to select the desired compressor based on the name 
void SelectCompressor(IltmmCompressors* pCompressors, LPCWSTR pszCompressorName) 
{ 
   long index; 
   BSTR bstrCompressorName = SysAllocString(pszCompressorName); 
   pCompressors->Find(bstrCompressorName, &index); 
   pCompressors->put_Selection(index); 
   SysFreeString(bstrCompressorName); 
} 
 
//Helper method to wait until the conversion is done 
void WaitForCompletion(IltmmConvert* pConvert) 
{ 
   long lState = ltmmConvert_State_Running; 
   MSG msg; 
 
   pConvert->get_State(&lState); 
 
   while (lState != ltmmConvert_State_Stopped) 
   { 
      if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
      { 
         TranslateMessage(&msg); 
         DispatchMessage(&msg); 
      } 
 
      pConvert->get_State(&lState); 
   } 
} 

Run the Project

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

If the steps were followed correctly, the input video will be converted to a HEVC/H265 and a HEVC/H264 compressed video file and saved in the output path.

Wrap-up

This tutorial showed how to create a new C++ Project to use the LEADTOOLS Multimedia Convert Control to convert any input video to a HEVC/H.265 or HEVC/H.264 compressed video file.

See Also

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


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