|
Came across an old project where I had to solve this need, and thought I'd archive it on here for search engine purposes. This can be useful for scenarios like corporate training video directories, where you let the trainers upload videos and it automatically creates a thumbnail for clients to browse in a webapp, for example. The first thing you'll need is an interop assembly to allow you to use the DirectShow COM objects from within .NET. You could either add a COM reference to your project, or better still use the command line tlbimp.exe framework utility to create the interop - the latter is preferred, as you'll want a strongly named interop assembly, which you can accomplish by specifying a keyfile with the tlbimp.exe /keyfile: parameter (specifying a key file that you created with the sn.exe framework utility). Add the newly created interop assembly as a reference in your project - barring specifying parameters otherwise, it'll be called Interop.DexterLib.dll, and once referenced will appear in the references as, of course, Interop.DexterLib. There are a couple of structures that we need to define as we'll need them to communicate with DirectShow. [StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)] The following code presumes that a couple of variables exist (for instance as parameters to a function)
In your extraction function, create a MediaDet instance, set the source video file path via the Filename property, and search for a video stream (there can be multiple streams. We search for a video stream by looking for one identified by the GUID 05589f80-c356-11ce-bf01-00aa0055595a). The following presumes that the unit has a using Interop.DexterLib; in it. MediaDet mediaInt = new
Interop.DexterLib.MediaDet(); System.Guid videoHeader = new Notice that we're setting the CurrentStream property on each iteration, so when a video stream is found, the MediaDet object will already have it selected as the active stream. If we found a video stream in the file, retrieve the properties of the video frame so we can grab a full frame, and then direct it to save a frame at the specified time offset to our specified destination file. if (videoStreamFound) Now ensure that the COM object is predictably freed now. Marshal.FinalReleaseComObject(mediaInt); Note that there are pointers (scary!) used above, so this code needs to be compiled with /unsafe. Don't worry - It's safe. This technique works for pretty much any non-DRM multimedia file that contains a video stream, for which there is a DirectShow compatible codec installed on the system. One note about this entry - I would use the PRE element and layout the code better, however Radio Userland exhibits a trait that drives me nuts with software: It is too clever, in a way that is often very detrimental. From removing attribute formatting, to completely reformatting PRE blocks, to auto-linking links that it shouldn't link, I seem to spend too much time trying to avoid it's "helpful" logic. This seems to be the case with too much software out there.
|
(C) Dennis Forbes 2007