Get thumbnail image of video file in C#

FFMpeg is a right tool that can be used to extract video frame at some position. You can invoke ffmpeg.exe as mentioned above or just use existing .NET wrapper (like Video converter for .NET (it's free) to get thumbnail with just one line of code:

var ffMpeg = new NReco.VideoConverter.FFMpegConverter();
ffMpeg.GetVideoThumbnail(pathToVideoFile, thumbJpegStream,5);

You can programmatically execute FFmpeg to generate a thumbnail image file. Then open the image file to use it however you wish.

Here is some sample code:

public static Bitmap GetThumbnail(string video, string thumbnail)
{
    var cmd = "ffmpeg  -itsoffset -1  -i " + '"' + video + '"' + " -vcodec mjpeg -vframes 1 -an -f rawvideo -s 320x240 " + '"' + thumbnail + '"';

    var startInfo = new ProcessStartInfo
    {
        WindowStyle = ProcessWindowStyle.Hidden,
        FileName = "cmd.exe",
        Arguments = "/C " + cmd
    };

    var process = new Process
    {
        StartInfo = startInfo
    };

    process.Start();
    process.WaitForExit(5000);

    return LoadImage(thumbnail);
}

static Bitmap LoadImage(string path)
{
    var ms = new MemoryStream(File.ReadAllBytes(path));
    return (Bitmap)Image.FromStream(ms);
}

Xabe.FFmpeg - free, open source and cross-platform library. Provides fluent API to FFmpeg. Generating thumbnail from video in Xabe.F

    string output = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + FileExtensions.Png);
    IConversionResult result = await Conversion.Snapshot(Resources.Mp4WithAudio, output, TimeSpan.FromSeconds(0))
                                               .Start();

It requires FFmpeg executables like in other answer but you can download it by

    FFmpeg.GetLatestVersion();

Full documentation available here - Xabe.FFmpeg Documentation

Tags:

C#

.Net