Click or drag to resize

VideoFrameReader Class

Reads a video frame by frame, also allows seeking and provides info on the video.
Inheritance Hierarchy
SystemObject
  GleamTech.VideoUltimateVideoFrameReader

Namespace:  GleamTech.VideoUltimate
Assembly:  GleamTech.VideoUltimate (in GleamTech.VideoUltimate.dll) Version: 1.9.12
Syntax
public class VideoFrameReader : IEnumerable<Bitmap>, 
	IDisposable, IEnumerable

The VideoFrameReader type exposes the following members.

Constructors
  NameDescription
Public methodVideoFrameReader(Stream, VideoUltimateConfiguration)
Initializes a new instance of the VideoFrameReader class from the specified video stream.
Public methodVideoFrameReader(String, VideoUltimateConfiguration)
Initializes a new instance of the VideoFrameReader class from the specified video file.
Top
Properties
  NameDescription
Public propertyBitRate
Gets the average bitrate of the video in kbps.
Public propertyCodecDescription
Gets the descriptive name of the codec used in the video, meant to be more human readable than name.
Public propertyCodecName
Gets the name of the codec used in the video.
Public propertyCodecTag
Gets the tag of the codec used in the video, also known as FourCC code.
Public propertyCurrentFrameNumber
Gets the coded picture number of the current frame. Note that this number may be occasionally in wrong order because it is in the coded order, not in the display order. So if you are reading a video from beginning to the end, you may also use a separate variable incremented in your loop.
Public propertyDuration
Gets the duration of the video.
Public propertyFrameRate
Gets the frame rate of the video, expressed in frames per second (FPS).
Public propertyHeight
Gets the height of the video in pixels.
Public propertyMetadata
Gets a dictionary representing metadata stored in the video.
Public propertyWidth
Gets the width of the video in pixels.
Top
Methods
  NameDescription
Public methodDispose
Releases all resources used by this instance.
Public methodGetEnumerator
Returns an enumerator that iterates through the frames in the video.
Public methodGetFrame
Gets the frame at current position as Bitmap. Every call returns a new Bitmap instance which should be disposed by the caller.
Public methodRead
Reads and decodes the next frame from the video. If successful, then GetFrame can be called to get a Bitmap instance.
Public methodSeek
Seeks the video to the given time position in seconds.
Public methodSetFrameHeight
Set the frame height to be used for Bitmap instances returned by GetFrame. The width will be automatically calculated according to the video aspect ratio. This method can be called if a different frame size is required than the original video frame size.
Public methodSetFrameSize
Set the frame size to be used for Bitmap instances returned by GetFrame. This method can be called if a different frame size is required than the original video frame size.
Public methodSetFrameWidth
Set the frame width to be used for Bitmap instances returned by GetFrame. The height will be automatically calculated according to the video aspect ratio. This method can be called if a different frame size is required than the original video frame size.
Top
Examples

Read first frame from a video file and save it as an image file:

using (var videoFrameReader = new VideoFrameReader(@"C:\Video.mp4"))
{
    if (videoFrameReader.Read()) //Only if frame was read successfully
    {
        //Get a System.Drawing.Bitmap for the current frame
        //You are responsible for disposing the bitmap when you are finished with it.
        //So it's good practice to have a "using" statement for the retrieved bitmap.
        using (var frame = videoFrameReader.GetFrame())
            //Reference System.Drawing and use System.Drawing.Imaging namespace for the following line.
            frame.Save(@"C:\Frame1.jpg", ImageFormat.Jpeg);
    }
}

Read a video file from a stream:

using (Stream videoStream = OpenYourVideoStream())
using (var videoFrameReader = new VideoFrameReader(videoStream))
{
    //Process..
}

Read a video file from a URL:

using (var videoFrameReader = new VideoFrameReader("http://vjs.zencdn.net/v/oceans.mp4"))
{
    //Process..
}

Loop through all frames of a video file:

using (var videoFrameReader = new VideoFrameReader(@"C:\Video.mp4"))
{
    while (videoFrameReader.Read())
    {
        //Do something with current frame
    }
}

Loop through all frame bitmaps of a video file. This combines Read and GetFrame method calls:

using (var videoFrameReader = new VideoFrameReader(@"C:\Video.mp4"))
{
    foreach (var frame in videoFrameReader)
    {
        using (frame)
        {
            //Do something with current frame bitmap
        }
    }
}

Seek to the middle of a video file:

using (var videoFrameReader = new VideoFrameReader(@"C:\Video.mp4"))
{
    videoFrameReader.Seek(videoFrameReader.Duration.TotalSeconds / 2);

    if (videoFrameReader.Read())
        Console.WriteLine("Seeked Frame Number: " + videoFrameReader.CurrentFrameNumber);
    else
        Console.WriteLine("Frame read failed!");
}

Retrieve info or metadata of a video file:

using (var videoFrameReader = new VideoFrameReader(@"C:\Video.mp4"))
{
    Console.WriteLine("Info:");
    Console.WriteLine("----------------------------");
    Console.WriteLine("Duration: " + videoFrameReader.Duration);
    Console.WriteLine("Width: " + videoFrameReader.Width);
    Console.WriteLine("Height: " + videoFrameReader.Height);
    Console.WriteLine("CodecName: " + videoFrameReader.CodecName);
    Console.WriteLine("CodecDescription: " + videoFrameReader.CodecDescription);
    Console.WriteLine("CodecTag: " + videoFrameReader.CodecTag);
    Console.WriteLine("BitRate: " + videoFrameReader.BitRate);
    Console.WriteLine("FrameRate: " + videoFrameReader.FrameRate);

    Console.WriteLine();
    Console.WriteLine("Metadata:");
    Console.WriteLine("----------------------------");
    foreach (var entry in videoFrameReader.Metadata)
    {
        Console.WriteLine("Key: " + entry.Key);
        Console.WriteLine("Value: " + entry.Value);
        Console.WriteLine();
    }
}

Get the first non-blank frame of a video file:

using (var videoFrameReader = new VideoFrameReader(@"C:\Video.mp4"))
{
    var attempts = 0;

    //Loop through frames but limit to first 20 times
    while (videoFrameReader.Read() && attempts++ < 20)
    {
        //Skip first 3 seconds (e.g. possible logo intro) if video is longer than 5 seconds
        if (videoFrameReader.Duration.TotalSeconds > 5)
            videoFrameReader.Seek(3);

        using (var frame = videoFrameReader.GetFrame())
        {
            //IsBlank is our useful extension method for System.Drawing.Bitmap
            //The tolerance parameter determines the image blankness.
            //Higher the value higher the number of matches. 
            //Default value is 1 which means image must be very blank to return true. 
            //For example 5 can be used for a frame with a logo only to be detected as blank. 
            if (!frame.IsBlank(5))
            {
                frame.Save(@"C:\NonBlankFrame.jpg", ImageFormat.Jpeg);
                break;
            }
        }
    }
}
See Also