Capture from Video Source to File - Windows C++

This tutorial shows how to use the LEADTOOLS Multimedia SDK to create a Windows C++ application that uses the CaptureCtrl to perform simplified capture of a video file.

Overview  
Summary This tutorial shows how to use the LEADTOOLS Multimedia SDK to capture from a source to a video file.
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 Capture from Video Source to File - 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 capture 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 "<INSTALL DIR>\LEADTOOLS23\Bin\CDLL\x64\ltmm23x.dll" no_namespace, named_guids 

NoteFor 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: 
IltmmCapture* pCapture = NULL; 

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_ltmmCapture, NULL, CLSCTX_INPROC_SERVER, IID_IltmmCapture, (void**)&pCapture); 
   if FAILED(hr) 
   { 
      MessageBox(hWnd, TEXT("Error creating Capture control..\nAborting"), TEXT("LEADTOOLS Demo"), MB_ICONERROR); 
      return -1; 
   } 
   pCapture->VideoWindowFrame = (long)hWnd; 
   pCapture->VideoWindowSizeMode = ltmmSizeMode::ltmmNormal; 
   break; 
} 

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

case WM_DESTROY: 
   CoUninitialize(); 
   PostQuitMessage(0); 
   break; 

Add the Capture Video Code

The steps below are for Visual Studio 2019 and above; 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 &Capture. Drag the new item above Exit. Ensure the item's ID is ID_FILE_CAPTURE.

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_CAPTURE: 
   { 
      TCHAR szFileName[260] = TEXT(""); // File name 
      // Select video source 
      int nVideoDevice = -1; 
      for (int i = 0; i < pCapture->VideoDevices->Count; ++i) 
      { 
         TCHAR message[1024]; 
         TCHAR* deviceName = pCapture->VideoDevices->Item(i)->FriendlyName; 
         wsprintf(message, TEXT("Video Source %d: %s\nUse this device?"), i, deviceName); 
         if (MessageBox(hWnd, message, TEXT("Capture Demo"), MB_YESNO) == IDYES) 
         { 
            nVideoDevice = i; 
            break; 
         } 
      } 
      if (nVideoDevice == -1) // No device chosen 
         break; 
      pCapture->VideoDevices->Selection = nVideoDevice; 
 
      // Enable Preview 
      pCapture->PreviewSource = ltmmCapture_Preview_Video; 
      pCapture->Preview = TRUE; 
 
      // Choose output file name 
      if (!GetCaptureFileName(hWnd, szFileName, ARRAYSIZE(szFileName))) 
         break; 
      pCapture->TargetFormat = ltmmCapture_TargetFormat_Avi; 
      // Select the LEAD H264 video codec 
      int nVideoCodec = pCapture->VideoCompressors->Find("@device:sw:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\\LEAD H264 Encoder (4.0)"); 
      pCapture->VideoCompressors->Selection = nVideoCodec; 
 
      pCapture->TargetFile = szFileName; 
      pCapture->StartCapture(ltmmCapture_Mode_Video); 
      MessageBox(hWnd, TEXT("Capturing.. Click OK to stop"), TEXT("Capture Demo") , MB_ICONINFORMATION); 
      pCapture->StopCapture(); 
      MessageBox(hWnd, TEXT("Finished capturing."), TEXT("Capture Demo"), MB_ICONINFORMATION); 
   } 
   break; 
   // Keep rest of the code as is 

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

bool GetCaptureFileName(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("AVI File\0*.avi\0"); 
   OpenFileName.lpstrDefExt = TEXT("avi"); 
   OpenFileName.nFilterIndex = 1; 
   OpenFileName.lpstrFileTitle = NULL; 
   OpenFileName.nMaxFileTitle = 0; 
   OpenFileName.lpstrInitialDir = NULL; 
   OpenFileName.Flags = OFN_PATHMUSTEXIST; 
 
   // Show the File Save dialog box 
   if (!GetSaveFileName(&OpenFileName)) 
      return false; 
   return true; 
} 

Run the Project

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

If the steps were followed correctly, the application runs. Select File -> Capture to enable the user to capture a video file and save it as AVI file format from any multimedia capture source that is supported by the LEADTOOLS Multimedia SDK available on the machine.

Wrap-up

This tutorial covered how to create an instance of the Capture control and use it to record video files.

See Also

Help Version 23.0.2024.5.22
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.