How to grab frames from video files
Inside the How to play video files through DirectShow tutorial we have seen how to load and play a video clip with a video player embedded inside Active DJ Studio and we have also seen how to extract the audio track from the same video clip: now we will see how to extract single or multiple video frames and how to store them inside a picture file on the hard disk or inside a memory bitmap.
After having loaded the video clip, through one of the provided loading methods like VideoPlayer.Load, we need to initialize the frame grabber internal to a certain player through a call to the VideoPlayer.FrameGrabberInit method. At this point we must decide what we want to obtain:
|•||If we need to store grabbed frames into picture files, we will have to use the VideoPlayer.FrameGrabberExecToFile method.|
|•||If we need to store grabbed frames into memory bitmaps (accessible through their HBITMAP handle) we will have to use the VideoPlayer.FrameGrabberExecToMemory method.|
In both cases we have to set a range within the video clip where grabbing will occur; we certainly need to set a starting position (nStartPosition parameter) and then we need to set the final position where grabbing will end: for this purpose we have two options:
|•||we can grab a range expressed in milliseconds: this is obtained by setting the nStopType parameter to VIDEOGRABBER_STOP_POSITION and by setting the nEndPosition parameter to the position in milliseconds where we want to terminate the grabbing session; for example, if we would like to grab all of the frames contained in the range between 10 and 15 seconds of the video clip, we would set the nStartPosition parameter to 10000 and the nEndPosition parameter to 15000; in case we should need to grab all of the frames till the end of the video clip, we would have to set the nEndPosition parameter to -1.|
|•||we can grab a range expressed in number of frames: this is obtained by setting the nStopType parameter to VIDEOGRABBER_STOP_FRAMES_NUM and by setting the nNumberOfFrames parameter to the number of frames we want to grab starting from the initial position; for example, if we would like to grab one single frame positioned at 10 seconds of the video clip, we would have to set the nStartPosition parameter to 10000 and the nNumberOfFrames parameter to 1 while, if we would like to grab the first 10 frames, again starting from the same position, we would set the nNumberOfFrames parameter to 10; in case we should need to grab all of the frames till the end of the video clip, we would have to set the nNumberOfFrames parameter to -1.|
When the grabbing is started using the VideoPlayer.FrameGrabberExecToFile method we can also specify the graphical format of the pictures files created during the grabbing session; supported formats are the following: BMP, JPEG, PNG, GIF, TIFF and TIFF Multipage: when using "TIFF Multipage" all of the grabbed frames are obviously stored inside a single file.
After completing the grabbing session, we can check results through the following methods:
|•||we can know if the grabbing session stored frames in picture files or in memory bitmaps through the return value of the VideoPlayer.FrameGrabberResultsGetAvailableType method.|
|•||If the return value should be TYPE_OF_GRABBED_FRAME_FILES, this would mean that grabbed frames are now stored inside graphical picture files which can be enumerated through the combination of the VideoPlayer.FrameGrabberResultsGetFilesCount and VideoPlayer.FrameGrabberResultsGetFilePathname methods.|
|•||If the return value should be TYPE_OF_GRABBED_FRAME_BITMAPS, this would mean that grabbed frames are now stored inside memory bitmaps (HBITMAP) which can be enumerated through the combination of the VideoPlayer.FrameGrabberResultsGetBitmapsCount and VideoPlayer.FrameGrabberResultsGetBitmap methods.|
|•||In both cases, for each grabbed frame we can obtain the color of a specific pixel at given coordinates through the use of the VideoPlayer.FrameGrabberResultsGetPixelColor method.|
The component gives also the possibility to resize output pictures to custom dimensions respect to the original dimensions of the video frames through the VideoPlayer.FrameGrabberOutputResizeSet method.
In case you should simply need to take a screenshot of what it's currently being played on a specific video window, you may use the VideoPlayer.ScreenshotSaveToFile method.
An example of use of the frame grabber in Visual Basic 6 and Visual C++ 6 can be found inside the following sample installed with the product's setup package: