I was looking to implement a function that retrieves a single frame from an input video, which can be used as a thumbnail. I wanted to display thumbnails for videos listed on my site, I wanted to fetch a frame from a video (from a particular time) and display it as thumbnail.
This was not an easy task as there is no API in .NET Framework related to this without being dependent on codecs. Luckily I stumpled upon .NET Video Thumbnailer library from GleamTech. VideoUltimate has VideoThumbnailer class that can quickly generate a meaningful thumbnail for a video. It’s very easy to use:
using (var videoThumbnailer = new VideoThumbnailer(@"C:\Video.mp4"))
{
using (var thumbnail = videoThumbnailer.GenerateThumbnail(300))
thumbnail.Save(@"C:\Thumbnail.jpg", ImageFormat.Jpg);
}
GenerateThumbnail method smartly generates a meaningful thumbnail for the video by seeking to a sensible time position and avoiding blank frames. Every call returns a new Bitmap instance which should be disposed by the caller. Once the smart time position is determined after the first call, the consecutive calls will be faster and can be used to retrieve Bitmap instances in different sizes. First parameter (300 pixels in above sample) is the maximum width or height of the thumbnail, aspect ratio is preserved:
It’s also possible to overlay the duration of the video on the bottom-right corner (similar to youtube) by passing true to the second parameter:
using (var videoThumbnailer = new VideoThumbnailer(@"C:\Video.mp4"))
{
using (var thumbnail = videoThumbnailer.GenerateThumbnail(300, true))
thumbnail.Save(@"C:\ThumbnailWithDuration.jpg", ImageFormat.Jpg);
}
Unlike other libraries, these classes can also read video files from streams, an Azure Blob container, an Amazon S3 bucket, i.e. when you don’t have the video file on disk, you can use the constructors with Stream or FileProvider parameter.
You can even get the thumbnail of a video file directly from a URL:
using (var videoThumbnailer = new VideoThumbnailer("http://vjs.zencdn.net/v/oceans.mp4"))
using (var thumbnail = videoThumbnailer.GenerateThumbnail(300, true))
thumbnail.Save(@"C:\ThumbnailFromUrl.jpg", ImageFormat.Jpg);
VideoUltimate is really a nice time-saving library. It was able to read any video file format I threw at it. I also liked that it’s a managed DLL which works both on 32-bit and 64-bit machines.
Top comments (0)