I wrote a simple, yet disgusting video thumbnail image generator method—which uses the MediaToolkit .NET library.
The code is not self-explanatory whatsoever, and it's extremely inefficient.
It accepts a video byte array & MIME type—and writes it to a video file. Then, it obtains the video file's duration, stores the middle frame, and writes it to an image file. Finally, it retrieves the image file bytes and returns them ... after deleting the video & image file.
How can I make it more efficient?
public static byte[] GetVideoThumbnail(byte[] bytes, string mediaType)
{
var video = new MediaFile
{
Filename = Path.GetTempPath() + Guid.NewGuid() + MimeTypeMap.GetExtension(mediaType)
};
var image = new MediaFile { Filename = Path.GetTempPath() + Guid.NewGuid() + ".jpg" };
File.WriteAllBytes(video.Filename, bytes);
using (var engine = new Engine())
{
engine.GetMetadata(video);
engine.GetThumbnail(video, image,
new ConversionOptions { Seek = TimeSpan.FromSeconds(video.Metadata.Duration.TotalSeconds / 2) });
}
var thumbnail = File.ReadAllBytes(image.Filename);
File.Delete(video.Filename);
File.Delete(image.Filename);
return thumbnail;
}
FFMpegusage ? Problems and inefficient of your code is caused byMediaToolkit. \$\endgroup\$FFmpegis even more inefficient for doing this thanMediaToolkitis. \$\endgroup\$var image = new MediaFile { Filename = Path.GetTempPath() + Guid.NewGuid() + ".jpg" };,File.WriteAllBytes(video.Filename, bytes);and other read/write/delete operations in new threads. \$\endgroup\$NRecoright now. I found it last week and dismissed it. \$\endgroup\$