c# – How to Generate Thumbnail Using MediaToolKit

Question:

I need to generate a thumbnail from a video when uploading it to my application, I would like an explanation of how to use the MediaToolKit package to do this, I appreciate it.

Answer:

To install the package, I recommend downloading it via nuget, but that's a matter of preference.

PM> Install-Package MediaToolkit

After downloading and referencing the package in the project, you can generate a thumb using the GetThumbnail() method of the Engine class

using MediaToolkit;
using MediaToolkit.Model;
using MediaToolkit.Options; 

/* Resto do código */

private void SalvarThumb(string caminhoVideo)
{
    var caminhoThumb  = Path.Combine("C:\DestinoThumb", "nomeArquivo.jpg");
    var inputFile  = new MediaFile { Filename = caminhoVideo };
    var outputFile = new MediaFile { Filename = thumbPath };

    using (var engine = new Engine())
    {
        engine.GetMetadata(inputFile);

        // A propriedade Seek define em qual momento do vídeo você pretende tirar o "snapshot"
        var options = new ConversionOptions { Seek = TimeSpan.FromSeconds(05) };
        engine.GetThumbnail(inputFile, outputFile, options);
    }
}
Scroll to Top