필터 지우기
필터 지우기

How can I cut every nth frame of a video and save it without those frames?

조회 수: 5 (최근 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

채택된 답변

Roberto
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
  댓글 수: 1
Marco Branco
Marco Branco 2014년 4월 29일
hi Roberto, I've tried with a small vídeo and worked fine. Now I'll try with a larger one. Thanks a lot. Best regards, Marco Branco

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by