Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get thumbnail image of video file in C# [closed]

Tags:

c#

.net

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#?

like image 778
meer Avatar asked Mar 29 '13 11:03

meer


4 Answers

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);
like image 174
Vitaliy Fedorchenko Avatar answered Nov 18 '22 23:11

Vitaliy Fedorchenko


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);
}
like image 41
markyd13 Avatar answered Nov 18 '22 23:11

markyd13


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;
        }
like image 11
Ken Avatar answered Nov 19 '22 00:11

Ken


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

like image 10
viewbevy Avatar answered Nov 19 '22 01:11

viewbevy