tags:

views:

898

answers:

3

I need to capture the first frame of a video uploaded on an asp.net website. I really would like to do this with WPF (if it's even possible), but if anyone knows an easier way, I am open to that too.

I have seen examples of this using MediaPlayer and RenderTargetBitmap in WPF, but each example assumes the video is accessible via a URI. In my scenario, I only have the file bytes and I do not want to store the video directly on the FS.

http://blogs.msdn.com/delay/archive/2008/09/03/video-frame-grabbing-made-easy-how-to-quickly-capture-multiple-video-frames-with-wpf.aspx

Any help is greatly appreciated!

+2  A: 

I don't know that this is advisable at all, as it will more than likely require that you run a message pump of some kind, which is a really bad idea in an ASP.NET site.

Rather, I would use the DirectShow API to try and process the video. You should be able to stream the content as bytes using it, and you won't need a message loop to process the video.

You can access it through .NET using the DirectShow .NET wrapper, located here:

http://directshownet.sourceforge.net/

And you will want to look at the Sample Grabber Example on MSDN:

http://msdn.microsoft.com/en-us/library/ms787867(VS.85).aspx

Mind you that you might not necessarily want the first frame, as with a number of videos, they can be black and not really be a good candidate for a thumbnail. Rather you might want to do what Vista does and look for the first non-black frame.

casperOne
I'll take a look at this, sounds like it will be a lot of work :(
Page Brooks
@Page Brooks: It is, working with video frames is not easy. However, in order to do this processing in an environment where you don't have access to a desktop (like ASP, and the Media Player in WPF requires it), you have no choice.
casperOne
A: 

http://directshownet.sourceforge.net/

Use the MediaDet example or this one - http://wpfmediakit.codeplex.com/SourceControl/changeset/view/17356#245585

-Jer

A: 

I ended up using FFMpeg.exe (Downloaded from here) to capture the first frame of videos uploaded to my site. This probably isn't the most ideal solution, but I don't have any DirectShow experience and, in my opinion, this solution is much simpler than other suggestions mentioned.

I was not able to get around saving the file to the file system, so I simply wrote the uploaded bytes out to a temporary file on disk performed the work using FFMpeg and then removed each of the files generated during the process.

The processing for this is done on a separate thread from the request thread. If I find this to be an issue, I have a separate Windows service that I can offload the work to with no problem.

The syntax for obtaining a single frame is as follows:

FFMpeg.exe -i "c:\MyPath\MyVideo" -vframes 1 "c:\MyOutputPath\MyImage%d.jpg"

The %d is very important. If you do not include this, FFMpeg will throw an error. The %d will substitute a serial number when writing out the file.

I hope this helps!

Page Brooks