Get Frames/Images From Video In Asp.Net
Introduction:
In this article we will see how to get the frames from running video in asp.net. For run the video I used the third party control as AspNetVideo.Net3. Which required only the path of video file to play the video.
For getting first frame from the video file I used JockerSoft.Media library which have the set of methods for operations like Crop the frame, save the frame from video file. This JockerSoft.Media has the following methods.
public static Bitmap GetFrameFromVideo(string videoFile, double percentagePosition);
public static Bitmap GetFrameFromVideo(string videoFile, double percentagePosition, Size target);
public static Bitmap GetFrameFromVideo(string videoFile, double percentagePosition, out double streamLength, Size target);
public static Size GetVideoSize(string videoFile);
public static void SaveFrameFromVideo(string videoFile, double percentagePosition, string outputBitmapFile);
public static void SaveFrameFromVideo(string videoFile, double percentagePosition, string outputBitmapFile, Size target);
public static void SaveFrameFromVideo(string videoFile, double percentagePosition, string outputBitmapFile, out double streamLength, Size target);
public static void SaveFramesFromVideo(string videoFile, double[] positions, string outputBitmapFiles);
For getting frame I used the Bitmap return type Method GetFrameFromVideo() which takes the videofiles path and the percentage for frame and return the bitmap object. You can save also this bitmap and this JockerSoft provide method for getting and saving the frame on specified location also.
Using Code:
Just I’m calling the methods of JockerSoft.Media library For getting frame from video you can call this like
// Path of the video and frame storing path
string _videopath = Server.MapPath("~/Video/search.AVI");
string _imagepath = Server.MapPath("~/Frames/CropedImage.jpg");
// Getting Frame From Video only not storing.
Bitmap bmp = FrameGrabber.GetFrameFromVideo(_videopath, 0.2d);
//Storing return bmp from FrameGrabber Class.
bmp.Save(_imagepath, System.Drawing.Imaging.ImageFormat.Gif);
You get the frame and can save also this frame to specified location like which require the path of video and the image path.
// Path of the video and frame storing path
string _videopath = Server.MapPath("~/Video/search.AVI");
string _imagepath = Server.MapPath("~/Frames/CropedImage.jpg");
FrameGrabber.SaveFrameFromVideo(_videopath, 0.2d, _imagepath);
bmp.Save(_imagepath, System.Drawing.Imaging.ImageFormat.Gif);
You can get more than one frame also with SaveFramesFromVideo(string videoFile, double[] positions, string outputBitmapFiles) which takes video file ,position array and output file path for storing those frames on the specified location.
Conclusion:
In this way you can use the JockerSoft.Media and can crop the frames from video and can save those frames. Comments and doubts are heartly welcome.