How can I cut every nth frame of a video and save it without those frames?
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Roberto
2014-4-29
I really don't know whats wrong with your code; but here's what I wrote:
%After your validations and calculations
original = vid ;
output = [vid '_cutted.avi'];
if exist(output,'file');
delete(output);
end
vidOriginal = VideoReader(original);
N = fix(Frame_vid/abs(dif_t)) ;
writerObj = VideoWriter(output);
writerObj.FrameRate = 240; % CAREFULL!!!
open(writerObj);
nFrames = vidOriginal.NumberOfFrames;
skipN = 1:N:nFrames ;
vidHeight = vidOriginal.Height;
vidWidth = vidOriginal.Width;
mov(1:nFrames - numel(skipN)) = struct('cdata', zeros(vidHeight, vidWidth, 3, 'uint8'),'colormap', []);
for i = 1:nFrames
j = find(i== skipN);
if any(j)
continue
end
mov(i).cdata = read(vidOriginal, i);
writeVideo(writerObj, mov(i).cdata);
end
close (writerObj);
clear writerObj
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!