askvity

How to Read Video File in MATLAB?

Published in MATLAB Video Processing 5 mins read

To read a video file in MATLAB, you primarily use the VideoReader object. This object allows you to open a video file and access its properties and frames.

Using the VideoReader Object

The fundamental step is to create a VideoReader object linked to your video file.

v = VideoReader( filename ) creates object v to read video data from the file named filename. This is the basic syntax where filename is a string containing the path to your video file (e.g., 'myvideo.mp4', 'C:\videos\clip.avi').

You can also specify properties when creating the object using name-value arguments:

v = VideoReader( filename , Name,Value ) sets the properties CurrentTime , Tag , and UserData using name-value arguments. This is useful if you want to start reading from a specific point in the video or associate custom data with the reader object.

For example, VideoReader('myfile. mp4','CurrentTime',1.2) starts reading 1.2 seconds into the video. This sets the starting position for reading frames.

Once the VideoReader object is created, you can then read frames from it.

Steps to Read a Video File

  1. Create the VideoReader object: Link MATLAB to your video file.
  2. Read Frames: Extract individual frames or a range of frames from the video.
  3. (Optional) Close the Object: Release the file resource (though MATLAB often handles this automatically when the object goes out of scope).

Let's look at a common workflow:

% Step 1: Create the VideoReader object
videoFile = 'path/to/your/video.mp4'; % Replace with your video file path
v = VideoReader(videoFile);

% Display some video properties
fprintf('Video Duration: %.2f seconds\n', v.Duration);
fprintf('Frame Rate: %.2f fps\n', v.FrameRate);
fprintf('Number of Frames: %d\n', v.NumFrames);
fprintf('Video Format: %s\n', v.VideoFormat);
fprintf('Video Resolution: %d x %d pixels\n', v.Width, v.Height);

% Step 2: Read frames
% Option A: Read frame by frame using a loop
figure; % Open a figure window to display frames
while hasFrame(v)
    videoFrame = readFrame(v);
    imshow(videoFrame); % Display the current frame
    title(sprintf('Time: %.2f sec', v.CurrentTime)); % Show current time
    drawnow; % Update the display
end

% Option B: Read all frames at once (requires sufficient memory)
% videoData = read(v); % Reads all frames into a single array

% Option C: Read a specific range of frames (e.g., first 100 frames)
% videoSubset = read(v, [1, 100]); % Reads frames 1 through 100

% Option D: Start reading from a specific time (as per reference)
% v2 = VideoReader(videoFile, 'CurrentTime', 5.0); % Start at 5 seconds
% while hasFrame(v2) && v2.CurrentTime < 10.0 % Read for 5 seconds
%     frame = readFrame(v2);
%     imshow(frame);
%     title(sprintf('Time: %.2f sec', v2.CurrentTime));
%     drawnow;
% end
% clear v2; % Optional: Clear the object when done

% Step 3: (Optional) Clear the object when done reading
clear v;

Key Methods and Properties

Here's a quick reference for some important aspects of the VideoReader object:

Item Description How to Use
Object Creation Establishes connection to the video file. Uses the reference information. v = VideoReader('filename.mp4');
filename The path and name of the video file. Part of VideoReader() syntax.
Name,Value Optional arguments to set properties like start time. Uses the reference information. VideoReader('file.avi', 'CurrentTime', 10);
readFrame(v) Reads the next available frame from the video object v. frame = readFrame(v);
read(v) Reads all frames from the video object v. allFrames = read(v);
read(v, index) Reads a specific frame by index. frame50 = read(v, 50);
read(v, [start, end]) Reads a range of frames by index. frames10to20 = read(v, [10, 20]);
hasFrame(v) Returns true if there is another frame to read. Useful for loops. while hasFrame(v)
v.CurrentTime Gets the current time in seconds. currentTime = v.CurrentTime;
v.Duration Gets the total duration of the video in seconds. videoDuration = v.Duration;
v.NumFrames Gets the total number of frames in the video. numberOfFrames = v.NumFrames;
v.FrameRate Gets the frame rate of the video in frames per second (fps). fps = v.FrameRate;
v.Width Gets the width of the video frames in pixels. frameWidth = v.Width;
v.Height Gets the height of the video frames in pixels. frameHeight = v.Height;

Using the VideoReader object and its methods like readFrame or read allows you to effectively access and process video data frame by frame or in chunks within your MATLAB environment.

Related Articles