Install MediaToolkit via NuGet
masterYou can install MediaToolkit using the NuGet Package Manager Console.
PM> Install-Package MediaToolkitrepository·master·Indexed 20 days ago
https://github.com/aydinadn/mediatoolkitA .NET wrapper for FFmpeg that provides a simplified interface for media tasks, including converting, slicing, and editing audio and video files. It supports retrieving media metadata, extracting thumbnails, transcoding with specific parameters, and monitoring conversion progress via events.
You can install MediaToolkit using the NuGet Package Manager Console.
PM> Install-Package MediaToolkitTo grab a frame from a video, use engine.GetThumbnail. You can specify the exact timestamp for the frame using ConversionOptions.Seek.
var inputFile = new MediaFile {Filename = @"C:\Path\To_Video.flv"};
var outputFile = new MediaFile {Filename = @"C:\Path\To_Save_Image.jpg"};
using (var engine = new Engine())
{
engine.GetMetadata(inputFile);
// Saves the frame located on the 15th second of the video.
var options = new ConversionOptions { Seek = TimeSpan.FromSeconds(15) };
engine.GetThumbnail(inputFile, outputFile, options);
}Use the Engine.Convert method to perform a simple conversion from one media format to another. This requires an input MediaFile and an output MediaFile.
var inputFile = new MediaFile {Filename = @"C:\Path\To_Video.flv"};
var outputFile = new MediaFile {Filename = @"C:\Path\To_Save_New_Video.mp4"};
using (var engine = new Engine())
{
engine.Convert(inputFile, outputFile);
}The Engine class provides events to monitor the conversion process. You can subscribe to ConvertProgressEvent for real-time updates (bitrate, FPS, etc.) and ConversionCompleteEvent when the task finishes.
public void StartConverting()
{
var inputFile = new MediaFile {Filename = @"C:\Path\To_Video.flv"};
var outputFile = new MediaFile {Filename = @"C:\Path\To_Save_New_Video.mp4"};
using (var engine = new Engine())
{
engine.ConvertProgressEvent += ConvertProgressEvent;
engine.ConversionCompleteEvent += engine_ConversionCompleteEvent;
engine.Convert(inputFile, outputFile);
}
}
private void ConvertProgressEvent(object sender, ConvertProgressEventArgs e)
{
Console.WriteLine("Bitrate: {0}", e.Bitrate);
Console.WriteLine("Fps: {0}", e.Fps);
// ... other properties like e.Frame, e.ProcessedDuration, e.SizeKb, e.TotalDuration
}
private void engine_ConversionCompleteEvent(object sender, ConversionCompleteEventArgs e)
{
Console.WriteLine("Conversion complete!");
// ... properties like e.Bitrate, e.Fps, etc.
}To extract a specific segment of a video, use the CutMedia method on a ConversionOptions object. The first parameter is the starting position, and the second is the duration of the segment to keep.
var inputFile = new MediaFile {Filename = @"C:\Path\To_Video.flv"};
var outputFile = new MediaFile {Filename = @"C:\Path\To_Save_ExtractedVideo.flv"};
using (var engine = new Engine())
{
engine.GetMetadata(inputFile);
var options = new ConversionOptions();
// This example will create a 25 second video, starting from the
// 30th second of the original video.
options.CutMedia(TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(25));
engine.Convert(inputFile, outputFile, options);
}To access metadata such as duration, call engine.GetMetadata(inputFile) on the input MediaFile. Once called, the Metadata property of the MediaFile object will be populated.
var inputFile = new MediaFile {Filename = @"C:\Path\To_Video.flv"};
using (var engine = new Engine())
{
engine.GetMetadata(inputFile);
}
Console.WriteLine(inputFile.Metadata.Duration);Use ConversionOptions to fine-tune transcoding parameters such as resolution, aspect ratio, and sample rate.
var inputFile = new MediaFile {Filename = @"C:\Path\To_Video.flv"};
var outputFile = new MediaFile {Filename = @"C:\Path\To_Save_New_Video.mp4"};
var conversionOptions = new ConversionOptions
{
MaxVideoDuration = TimeSpan.FromSeconds(30),
VideoAspectRatio = VideoAspectRatio.R16_9,
VideoSize = VideoSize.Hd1080,
AudioSampleRate = AudioSampleRate.Hz44100
};
using (var engine = new Engine())
{
engine.Convert(inputFile, outputFile, conversionOptions);
}You can target specific physical formats like DVD by configuring ConversionOptions. You can also specify the TargetStandard (e.g., PAL or NTSC).
var inputFile = new MediaFile {Filename = @"C:\Path\To_Video.flv"};
var outputFile = new MediaFile {Filename = @"C:\Path\To_Save_New_DVD.vob"};
var conversionOptions = new ConversionOptions
{
Target = Target.DVD,
TargetStandard = TargetStandard.PAL
};
using (var engine = new Engine())
{
engine.Convert(inputFile, outputFile, conversionOptions);
}