How to extract frames from a .mp4 video?

98 ビュー (過去 30 日間)
Anthony Andrews
Anthony Andrews 2019 年 11 月 30 日
回答済み: Boyuan Li 2020 年 5 月 14 日
Hi I am trying to combine two videos in matlab. The first video is a clock moving with a green background. The second video is a black video moving in what it looks like is space. I am trying to extract the frames and make the second video the background to the first video. Using the code below only extracts the first frame, how can i extract all frames?
outputFolder1=('video');
v1=VideoReader('Clock.mp4');
vid1Frames=readFrame(v1);
for frame=1:size(vid1Frames,4)
outputBaseFileName=sprintf('%3.3d.png',frame);
outputFullFileName=fullfile(outputFolder1,outputBaseFileName);
imwrite(vid1Frames(:,:,:,frame),outputFullFileName,'png');
end

回答 (2 件)

Stephan
Stephan 2019 年 12 月 1 日
編集済み: Stephan 2019 年 12 月 1 日
outputFolder1=('video');
v1=VideoReader('Clock.mp4');
vid1Frames=read(v1);
for frame=1:size(vid1Frames,4)
outputBaseFileName=sprintf('%3.3d.png',frame);
outputFullFileName=fullfile(outputFolder1,outputBaseFileName);
imwrite(vid1Frames(:,:,:,frame),outputFullFileName,'png');
end

Boyuan Li
Boyuan Li 2020 年 5 月 14 日
Somehow the function readFrame() only read the first available frame instead of the whole video.
outputFolder1=('video');
v1=VideoReader('Clock.mp4');
while hasFrame(v1)
frame=readFrame(v1);
outputBaseFileName=sprintf('%3.3d.png',frame);
outputFullFileName=fullfile(outputFolder1,outputBaseFileName);
imwrite(frame,outputFullFileName,'png');
end

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by