Reputation: 91
I am trying to customize the playback of videos I have added to MatLab by setting them to play from a specific start point, rather than playing from the beginning.
By using MathWorks' VideoReader, I can determine the target start frames, duration, frame rate, etc.
How can I tell MatLab to play my video starting at, say, the 3 sec, or 5 sec mark? Or any other mark I choose?
Upvotes: 0
Views: 1025
Reputation: 681
if you are running 14b or higher, VideoReader has a property called CurrentTime that you can set.
So you can say
vidObj = VideoReader('xylophone.mpg');
vidObj.CurrentTime = 2; % 2 seconds;
readFrame(vidObj);
Hope this helps.
Upvotes: 0
Reputation: 396
you can do one thing by using the VideoReader you will get whole video which contain frames and audio. now you split by like this LET ASSUME 10 seconds video CONTAINS 200 FRAMES and starting time is 2 sec and ending time is 4 second. so 2 sec video = 40 frame and 4 sec video = 80 . now put one loop for frame 40 to 80 then store it in temp variable. afer then play that frames by using movie. I think below code will use for you.
sampling_factor = 8;
resizing_params = [100 120];
%%// Input video
xyloObj = VideoReader('xylophone.mpg');
%%// Setup other parameters
nFrames = floor(xyloObj.NumberOfFrame/sampling_factor); %%// xyloObj.NumberOfFrames;
vidHeight = resizing_params(1); %// xyloObj.Height;
vidWidth = resizing_params(1); %// xyloObj.Width;
% here i am play 4 sec movie to 2 to 3
info = get(xyloObj);
duration =info.Duration;
startframe =round( nFrames *2/duration); % 2 means starting duration in sec
endframe = round( nFrames *4/duration); % 4 means ending duration in sec
%// Preallocate movie structure.
temp(1:nFrames) = struct('cdata', zeros(vidHeight, vidWidth, 3, 'uint8'),'colormap',[]);
mov = temp(1:endframe-startframe) ;
indx =1;
%// Read one frame at a time.
for k = 1 :nFrames
if k >=startframe && k <=endframe
IMG = read(xyloObj, (k-1)*sampling_factor+1);
%// IMG = some_operation(IMG);
mov(indx).cdata = imresize(IMG,[vidHeight vidWidth]);
indx =indx +1;
end
end
%// Size a figure based on the video's width and height.
hf = figure;
set(hf, 'position', [150 150 vidWidth vidHeight])
%// Play back the movie once at the video's frame rate.
movie(hf, mov, 1, xyloObj.FrameRate);
Upvotes: 1