Thursday, October 9, 2014

Extract Video Frame (non-avi) in Matlab

The original author for this code can send me an email so that I can credit back to him/her.
Work for both avi and non-avi file

clc;
close all;

%% USE THIS IF - open a non-avi file
mov = mmreader('n3.mp4')
frame = read(mov,200); %LONG WAIT
mov.NumberOfFrames;

%% or USE THIS IF - Open an avi file
%filename = 'n3.wmv';
%mov = mmreader(filename);

%% continue here
% Output folder

outputFolder = fullfile(cd, 'frames');
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end

%getting no of frames

numberOfFrames = mov.NumberOfFrames;
numberOfFramesWritten = 0;
for frame = 1 : numberOfFrames

thisFrame = read(mov, frame);
outputBaseFileName = sprintf('%3.3d.png', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
imwrite(thisFrame, outputFullFileName, 'png');
progressIndication = sprintf('Wrote frame %4d of %d.', frame,numberOfFrames);
disp(progressIndication);
numberOfFramesWritten = numberOfFramesWritten + 1;
end
progressIndication = sprintf('Wrote %d frames to folder "%s"',numberOfFramesWritten, outputFolder);

disp(progressIndication);

No comments:

Post a Comment