Convert Video to MP4 - Console C#

This tutorial shows how to use the LEADTOOLS Multimedia SDK to create a C# Windows Console application that uses the ConvertCtrl 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 Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform Windows C# Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS

The LEADTOOLS Multimedia Convert Control contains many advanced features that simplify many multimedia tasks such as media conversion, streaming and transcoding. The toolkit is shipped with various demos that make use of these features, such as the main Multimedia Convert demo that has different editions in multiple programming languages.

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Convert Video to MP4 - Console C# tutorial.

Create the Project and Add LEADTOOLS References

In Visual Studio, create a new C# Windows Console project, and add the below necessary LEADTOOLS references.

This tutorial requires the following local DLLs, which are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64:

Note

Different SDK features require different references. For a complete list, refer to Multimedia Files You Must Include With Your 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 NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Convert Video Code

With the project created, the references added, the toolkit unlocked, and the license set, coding can begin.

In Solution Explorer, open Program.cs, then add using Leadtools; and using Leadtools.Multimedia; to the using block at the top.

C#
// Using block at the top 
using System.Windows.Forms; 
using Leadtools; 
using Leadtools.Multimedia; 

Add a new method called ConvertVideo(), and call it in the Main method after the UnlockMultimedia() method call.

C#
static void Main(string[] args) 
{ 
   UnlockMultimedia(); 
   ConvertVideo(); 
} 
 
static void ConvertVideo() 
{ 
   string inputFile = @"C:\LEADTOOLS21\Media\DaDa_CMP.avi"; 
   string outputFile = @"C:\LEADTOOLS21\Media\converted.mp4"; 
   ConvertCtrl convert = new ConvertCtrl(true); 
 
 
   Console.WriteLine("Preparing to convert.."); 
 
   convert.SourceFile = inputFile; 
   convert.TargetFile = outputFile; 
   convert.TargetFormat = TargetFormatType.ISO; 
 
   // select suitable compressors 
   convert.VideoCompressors.H264.Selected = true; 
   convert.AudioCompressors.AAC.Selected = true; 
 
   convert.AllowedStreams = StreamFormatType.AudioVideo; 
 
   Console.WriteLine("Converting. Please wait..."); 
   convert.StartConvert(); 
   int previousPercent = 0; 
   while (convert.State != ConvertState.Stopped) 
   {             
      System.Windows.Forms.Application.DoEvents(); 
      if (convert.PercentComplete - previousPercent >= 1) // print a dot every 1 percent or so 
      { 
         previousPercent = convert.PercentComplete; 
         Console.Write(". "); 
      } 
   } 
   convert.ResetSource(); 
   convert.ResetTarget(); 
          
   Console.WriteLine($"\nFinished conversion to file {outputFile}. Press any key to continue..."); 
   Console.ReadKey(true); 
} 

Run the Project

Make sure the input and output file names are set correctly. For this tutorial, the file DaDa_H264.avi can be used as input, which is shipped with LEADTOOLS 21 Multimedia SDK in the <INSTALL_DIR>\LEADTOOLS21\Media folder. Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application starts and converts the input file to a new MP4 (ISO) file.

Wrap-up

This tutorial showed how to convert a video file using the ConvertCtrl class.

See Also

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


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