I want to display thumbnails for videos listed on my site, I want to fetch a single frame from a video (from a particular time) and display them as thumbnails.
I have try this http://ramcrishna.blogspot.com/2008/09/playing-videos-like-youtube-and.html but is not working.
Is that possible using .NET 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);
}
For people don't want to use FFMpeg as its trouble in Commercial software. I have an old solution here:
ShellFile shellFile = ShellFile.FromFilePath(VideoFileName);
Bitmap bm = shellFile.Thumbnail.Bitmap;
Then you will get a Bitmap object that can be used in drawing. If you want a file, just do:
bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
if you want a BitmapImage that you can use it in Xaml binding. just transfer the Bitmap to BitmapImage. Here is an example:
public static BitmapImage ConvertBitmapToBitmapImage(Bitmap bitmap)
{
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
BitmapImage image = new BitmapImage();
image.BeginInit();
ms.Seek(0, SeekOrigin.Begin);
image.StreamSource = ms;
image.EndInit();
return image;
}
Xabe.FFmpeg - free (for non-commercial use), 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With