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
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!