Get Frames/Images From Video in Asp.Net



Introduction:

In this article we will see how to get frames from a running video in ASP.Net. To run the video I used the third party control as AspNetVideo.Net3, which required only the path of a video file to play the video.

For getting the first frame from the video file I used JockerSoft.Media library which has a 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:

I'm just calling the methods of JockerSoft.Media library; for getting a frame from a 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 also save the frame to a specified location that requires 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.

Up Next
    Ebook Download
    View all
    Learn
    View all