Search code examples
matlabvideovideo-processing

extracting frames of video with matlab


I'm trying to extract all frames of some videos (each of them contains around 13000 frames) with this code but it take gets too long to extract all of the frames. is there any more quick way?

    filename = 'S.avi';
mov = VideoReader(filename);

% Output folder

outputFolder = fullfile(cd, 'S');
mkdir(outputFolder);


%getting no of frames
framee=100000;
numberOfFrames = mov.NumberOfFrames;
numberOfFramesWritten = 0;
for frame =       1:12525

    thisFrame = read(mov, frame);
    framee=framee+1;
    outputBaseFileName = sprintf('%3.3d.png', framee);
    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);

Solution

  • Conceptually this is not very different to what you're doing, but you have a number of roundabout steps in your code.

    Presumably you could just do:

    mov = VideoReader(filename);
    vidFrames = read(mov);
    

    To collect your movie as a 4D array, and then just do

    for frame = 1 : size(vidFrames, 4)
        outputBaseFileName = sprintf('%3.3d.png', frame);
        outputFullFileName = fullfile(outputFolder, outputBaseFileName);
        imwrite( vidFrames(:,:,:,frame), outputFullFileName, 'png');
    end
    

    And it would probably be faster. Also, you could probably use parfor (or spmd with a counter function handle instead) to parallelise even further