28

I'm looking to implement a function that retrieves a single frame from an input video, so I can use it as a thumbnail.

Something along these lines should work:

// filename examples: "test.avi", "test.dvr-ms"
// position is from 0 to 100 percent (0.0 to 1.0)
// returns a bitmap
byte[] GetVideoThumbnail(string filename, float position)
{
}

Does anyone know how to do this in .Net 3.0?

The correct solution will be the "best" implementation of this function. Bonus points for avoiding selection of blank frames.

Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
  • any full source code sample ? – Kiquenet Nov 29 '13 at 18:36
  • although this question was asked first, but the answers at this [possiblely duplicated question](https://stackoverflow.com/questions/15702031/get-thumbnail-image-of-video-file-in-c-sharp) are more useful to me. – Weihui Guo Aug 31 '18 at 12:02

5 Answers5

10

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.

Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
  • Hi - this is really useful thanks, however I get some issues without any actual errors. For WMV files (which are not supported) I get an outputstream from the IMediaDet.get_OutputStreams() call - but the IMediaDet.WriteBitmapBits() then does nothing. However for AVI files, the IMediaDet.get_OutputStreams() call returns nothing, and so the method simply returns without doing anything. I've ruled out basics file I/O issues and permissions etc (the same app uploads the video, and as mentioned also get the outputstream for certain formats). Any pointers/suggestions you might have would be great! – user369142 Nov 14 '12 at 23:33
  • personally I would look at ffdshow now, the ms direct show interfaces are so shakey – Sam Saffron Nov 15 '12 at 10:35
  • 2
    @SamSaffron Any recommendations on doing this in 2015? I cant find the MediaBrowser library on nuget so I cant test your Gist. – M H Feb 10 '15 at 17:06
7

This project will do the trick for AVIs: http://www.codeproject.com/KB/audio-video/avifilewrapper.aspx

Anything other formats, you might look into directshow. There are a few projects that might help:
http://sourceforge.net/projects/directshownet/
http://code.google.com/p/slimdx/

Joel Martinez
  • 46,929
  • 26
  • 130
  • 185
5

1- Get latest version of ffmpeg.exe from : http://ffmpeg.arrozcru.org/builds/

2- Extract the file and copy ffmpeg.exe to your website

3- Use this Code:

Process ffmpeg;

string video;
string thumb;

video = Server.MapPath("first.avi");
thumb = Server.MapPath("frame.jpg");

ffmpeg = new Process();

ffmpeg.StartInfo.Arguments = " -i "+video+" -ss 00:00:07 -vframes 1 -f image2 -vcodec mjpeg "+thumb;
ffmpeg.StartInfo.FileName = Server.MapPath("ffmpeg.exe");
ffmpeg.Start();
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Ahmad Behjati
  • 536
  • 6
  • 4
  • Easier to use in cmd: ffmpeg.exe -i "File name.mp4" -ss 00:00:02 -vframes 1 -f image2 -vcodec mjpeg frame.jpg – Gennady G Mar 09 '15 at 11:48
  • 1
    This answer is amazing. ffmpeg has binary for every platform and supports almost every codec in the world. Other answers are platform dependent and have limited codec supports. Thank Ahmad. – Emad Apr 04 '18 at 09:54
0

This is also worth to see:

http://www.codeproject.com/Articles/13237/Extract-Frames-from-Video-Files

Maciej
  • 10,423
  • 17
  • 64
  • 97
0

There are some libraries at www.mitov.com that may help. It's a generic wrapper for Directshow functionality, and I think one of the demos shows how to take a frame from a video file.

GuyWithDogs
  • 683
  • 1
  • 4
  • 18