Split Video Into Segments - Console C# .NET 6

This tutorial shows how to split a video file into segments of equal duration with the ConvertCtrl in a C# Windows Console application using the LEADTOOLS Multimedia SDK.

The LEADTOOLS Multimedia Convert Control supports specifying a selected portion from a source file when converting to an output file. This can be used to split the source file to shorter clips by repeating the conversion process with different selected portions.

Overview  
Summary This tutorial covers how to split a video file to separate clips in a C# Windows Console application.
Completion Time 15 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform Windows C# Console Application
IDE Visual Studio 2022
Development License Download LEADTOOLS

Required Knowledge

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

Create the Project and add LEADTOOLS References

Start with a copy of the project created in the Convert Video to MP4 tutorial. If the project is not available, 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:

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:

Add the Split 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 and locate the ConvertVideo() method, then modify its code to implement splitting into portions as follows:

C#
static void ConvertVideo() 
{ 
   string inputFile = @"C:\LEADTOOLS23\Resources\Media\DaDa_CMP.avi"; 
   Console.Write("Enter number of segments: "); 
   int segments = int.Parse(Console.ReadLine()); 
 
   ConvertCtrl convert = new ConvertCtrl(true); 
 
   Console.WriteLine("Preparing to convert . ."); 
   for (int i = 1; i <= segments; i++) 
   { 
      string outputFile = $@"C:\LEADTOOLS23\Resources\Media\converted_part{i}.mp4"; 
      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; 
 
      // Split based on nearest key frame 
      convert.SelectionStartModifier = SelectionModifierType.NearestKeyFrame; 
      convert.SelectionEndModifier = SelectionModifierType.NearestKeyFrame; 
      convert.SelectionStart = (i - 1) * convert.Duration / segments; 
      convert.SelectionEnd = i * convert.Duration / segments; 
      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}"); 
   } 
   Console.WriteLine($"Done. Press any key to continue . . ."); 
   Console.ReadKey(true); 
} 

Note: Ensure that you set the input and output file paths accordingly to your use-case. For the purpose of this tutorial, the DaDa_H264.avi file is used, which is shipped with the LEADTOOLS v23 Multimedia SDK in the <INSTALL_DIR>\LEADTOOLS23\Resources\Media folder.

Run the Project

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

If the steps were followed correctly, the application starts and prompts the user to enter the number of required segments. The application then splits the input file into the requested number of MP4 (ISO) video clips.

Wrap-up

This tutorial showed how to split a video file into segments 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.