Play a snippet from an avi file
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a .avi file that is approximately 6 minutes of video. I need to be able to play only a snippet of it, after some data analysis is performed, for instance frame 2 to frame 30. My question, is there a function where I can specify the name of video file followed by frame numbers? I try to use mplay(filename), however it doesnt accept any other input parameters such as frame number. I am currently using a VideoReader to preallocate into a struct, then doing a movie2avi to create a smaller avi file and then doing a mplay(). This takes way to long to do at runtime.
댓글 수: 0
답변 (1개)
Jacob Mathew
2024년 9월 19일
Hey ARWA,
While there isn’t a direct function that can achieve the functionality you are looking for, we can use a combination of VideoReader and implay() function in Video Viewer to achieve this.
The approach is to use the VideoReader object, which itself is lightweight, to track and read the frames of the video we need. Once we have the frames, we can pass them to implay(), along with the frame rate to play it smoothly.
The following code illustrates this appoach:
% Specify the path to your AVI file
filename = 'sample.avi';
% Create a VideoReader object
video = VideoReader(filename);
% Define the range of frames you want to read
startFrame = 30;
endFrame = 90;
% Calculate the total number of frames to read
numFramesToRead = endFrame - startFrame + 1;
% Preallocate a structure to store the frames
frames(numFramesToRead) = struct('cdata', [], 'colormap', []);
% Set the starting frame
video.CurrentTime = (startFrame-1) / video.FrameRate;
% Loop through and read the frames
for k = 1:numFramesToRead
% readFrame automatically updates CurrentTime to the next frame
frames(k).cdata = readFrame(video);
end
% Play the frames as a video
implay(frames,video.FrameRate);
If the number of frames to be read is substantial, then you can replace for loop with parfor loop, which is parallel for loops to parallelise the reading of frames. You can refer to parfor loop documentation below:
Refer to the following for VideoReader documentation:
Refer to the following for implay() function’s documentation:
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Audio and Video Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!