Convert Video to HEVC / H265 and AVC / H264

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 DLL Application
IDE Visual Studio 2019
Runtime License Download LEADTOOLS

Create the Project

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.

Add the Multimedia Headers and LIB Files

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

C++
// Add LEADTOOLS Multimedia Headers and Libs 
#include "C:\leadtools 20\Include\ltmm.h" 
#include "C:\leadtools 20\Include\ltmm_Errors.h" 
 
//x86 libs 
#pragma comment (lib, "C:\\LEADTOOLS 20\\Lib\\ltmm.lib") 
#pragma comment (lib, "C:\\LEADTOOLS 20\\Lib\\ltmmuuid.lib") 
 
//x64 libs 
#pragma comment (lib, "C:\\LEADTOOLS 20\\Lib\\ltmmx.lib") 
#pragma comment (lib, "C:\\LEADTOOLS 20\\Lib\\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:

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

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:

C++
//This file contains the 'main' function. Program execution begins and ends here. 
 
#include "pch.h" 
#include "Convert-Video-to-HEVC-H265.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() 
{ 
   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:\\LEADTOOLS 20\\Media\\DaDa_CMP.avi")); 
   pConvert->put_SourceFile(bstr); 
   SysFreeString(bstr); 
 
   //Set the output file's location and name 
   bstr = ::SysAllocString(TEXT("C:\\LEADTOOLS 20\\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:\\LEADTOOLS 20\\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 20.0.2020.5.11
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS SDK Multimedia Tutorials Help