Get Thumbnail Image of Video File in C#

Get thumbnail image of video file in C#

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);
}

create thumbnail from video URL in C#

Using NReco to Create Video Thumbnail:

I found my problem:

1: I have not used Server.MapPath. I had just entered relativePath.

2: it is not necessary the Video file be local it can be hosted someWhere else.
NReco just download the desired portion of video then extract the Thumbnail.
your Video file should be in localHost directory of your local server. I mean if your site is under development and video file is in your local Computer folder it WON'T Work because NReco requires byte range support in HTTP response Header file.

Unacceptable Link: "http://localhost:81882/content/AVSEQ01.mp4"

so for my local test, I have moved my video file to Local IIS directory : ‪C:\inetpub\wwwroot\AVSEQ01.mp4

//sample remote video file
//string videoUrl = "http://phytonord.com/Film-Series/peik%20sahar/1.mp4";

//local video file
string localVideoFile = "http://localhost/AVSEQ01.mp4"
var ffMpeg = new NReco.VideoConverter.FFMpegConverter();
string thumbnailJPEGpath = Server.MapPath("~/Content/videoThumb.jpg");
ffMpeg.GetVideoThumbnail(videoUrl, thumbnailJPEGpath);

How do I get a Video Thumbnail in .Net?

I ended up rolling my own stand alone class (with the single method I described), the source can be viewed here. Media browser is GPL but I am happy for the code I wrote for that file to be Public Domain. Keep in mind it uses interop from the directshow.net project so you will have to clear that portion of the code with them.

This class will not work for DVR-MS files, you need to inject a direct show filter for those.

generating thumbnail image for video

Using the SDK you could try using the GetThumbnail method:

var video = new MediaItem(filePath);
using (var bitmap = video.MainMediaFile.GetThumbnail(
new TimeSpan(0, 0, 5),
new System.Drawing.Size(640, 480)))
{
// do something with the bitmap like:
bitmap.Save("thumb1.jpg");
}

How to get thumbnail from a video in Windows Store app?

StorageFile.GetThumbnailAsync() solved my issue. I tried to show image from video using below code. Hope it helps someone

bitmap = new BitmapImage();
bitmap.SetSource(await videoFile.GetThumbnailAsync(ThumbnailMode.SingleItem));


Related Topics



Leave a reply



Submit