Convert Video to MP4 - C# .NET 6

This tutorial shows how to use the ConvertCtrl to perform a simplified conversion of a video file to the MP4 format in a C# .NET 6 Console application using the LEADTOOLS Multimedia SDK.

Overview  
Summary This tutorial covers how to convert a video file in a C# .NET 6 Console application.
Completion Time 15 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or Higher
Development License Download LEADTOOLS
Try it in another language

The LEADTOOLS Multimedia Convert Control contains many advanced features that simplify 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 this tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

This tutorial requires the following local DLLs, which are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net:

For a complete list of which DLL files are required for your LEADTOOLS Multimedia application, refer to Multimedia Files to be Included 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:

Add the Convert Video Code

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

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

C#
using Leadtools; 
using Leadtools.Multimedia; 

Add a new method called ConvertVideo, and call it in the Main method after the InitLEAD method call.

C#
static void ConvertVideo() 
{ 
   string inputFile = @"C:\LEADTOOLS23\Resources\Media\DaDa_CMP.avi"; 
   string outputFile = @"C:\LEADTOOLS23\Resources\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 the LEADTOOLS v23 Multimedia SDK in the <INSTALL_DIR>\LEADTOOLS23\Resources\Media folder. Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application 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 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.