How to read a video file and save it to multiple frames in numerical order? Because I keep getting back random frames not in order of the video progression

2 次查看(过去 30 天)
My current code is
vid = VideoReader('vid.mp4');
numrames = vid.NumberofFrames;
n = numFrames;
for i = 1:1:n
frames = read(vid,i);
imwrite(frames,['mydirectory\'int2str(i),'.jpg']);
im(i) = image(frames);
end
I know that we can use natsortfiles to read a folder of images in numerical order, but how do you do it with a video file?
Thanks in advance

采纳的回答

Geoff Hayes
Geoff Hayes 2020-4-23
Manuella - does the because I keep getting back random frames not in order of video propression refer to the files that you have created or the images you see in your figure? If the former, try padding some zeros to the integer file name as
imwrite(frames,sprintf('mydirectory\%05d.jpg', i));
so that the file names are like 00001.jpg, 00002.jpg, etc.
If the latter (images you see are in random order) you may need to pause after displaying the image. The pause should be the inverse of the frame rate
vid = VideoReader('vid.mp4');
numFrames = vid.NumberofFrames;
frameRate = vid.FrameRate;
n = numFrames;
for i = 1:1:n
frames = read(vid,i);
imwrite(frames,sprintf('mydirectory\%05d.jpg', i));
im(i) = image(frames);
pause(1/frameRate);
end
Also, depending upon your version of MATLAB, consider using the readFrame function.

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by