How can I cut every nth frame of a video and save it without those frames?
Show older comments
Hi, I'm trying to save a vídeo without every nth frame. Because I have two vídeos with different frame rates and in order to make the vídeos with the same total number of frames after syncronize them. The code I have now is below. I have some problem in the code because the pc freezes in the end of the vídeo. Could anyone find the problem? Thanks in advance.
Te code is:
close all; clear all; clc;
% file name
name_vid1='firstvideo.avi';
%***** read vídeo *****
vid1 = VideoReader(name_vid1);
name_vid2=secondvideo;
%***** read vídeo *****
vid2 = VideoReader(name_vid2);
frame_vid1=vid1.NumberOfFrames;
frame_vid2=vid2.NumberOfFrames;
dif_t=frame_vid1-frame_vid2;
if dif_t>0
vid=name_vid1;
elseif dif_t<0
vid=name_vid2;
else
disp(' vídeos have the same number of frames.')
end
clear vid1;
clear vid2;
vid_file= VideoReader(vid);
Frame_vid = get(vid_file, 'NumberOfFrames');
n_frame=fix(Frame_vid/abs(dif_t)); % this calculates every nth frame to remove from video
%***** write a new vídeo *****
writerObj = VideoWriter([vid '_cutted.avi']);
%***** change frame rate*****
writerObj.FrameRate = 240;
%***** open new vídeo file
open(writerObj);
nFrames = vid_file.NumberOfFrames;
vidHeight = vid_file.Height;
vidWidth = vid_file.Width;
mov(1:nFrames) = struct('cdata', zeros(vidHeight, vidWidth, 3, 'uint8'),'colormap', []);
w=1;
while w<=nFrames;
last_frames=nFrames-w;
open(writerObj);
if last_frames>=1 && last_frames<n_frame
for k = w : nFrames
mov(k).cdata = read(vid_file, k);
writeVideo(writerObj, mov(k).cdata);
end
else
for k = w : w+n_frame-1
mov(k).cdata = read(vid_file, k);
writeVideo(writerObj, mov(k).cdata);
end
end
fclose all
w=w+n_frame+1;
end
close (writerObj);
clear writerObj
Accepted Answer
More Answers (0)
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!