0

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);
2
  • Use can use ffmpeg instead of Matlab...
    – Rotem
    Jul 24, 2016 at 16:54
  • I don't know if there's a more optimised function in matlab or not, but this operation is definitely parallelisable, so that should cut down your processing time a bit. Jul 24, 2016 at 18:47

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.