Hi,
I'm trying to play multiple avi videos in one subplot, and borrowed online code but always showing me the following error:
Error using VideoReader/read (line 134)
Cannot call READ method after using READFRAME or HASFRAME methods or setting the CURRENTTIME property. Recreate the object to read a frame at a
specific frame index.
Error in videoplay (line 18)
v1frame = v1.read(i1);
And here is the code:
clc;clear all;
v1= VideoReader('\\cab-fs07.mae.virginia.edu\NewData\NHTSA\2016-AutomatedVehicle\1Simulation\Project\RESULTV3.5\M50-OS_Standard_Reclined_0_Int_0_RMDB\1_cut.avi');
v1.CurrentTime = 1.5;
v1_numFrames = ceil(v1.FrameRate*v1.Duration);
v2= VideoReader('\\cab-fs07.mae.virginia.edu\NewData\NHTSA\2016-AutomatedVehicle\1Simulation\Project\RESULTV3.5\M50-OS_Standard_Reclined_0_Int_0_RMDB\2_top.avi');
v2.CurrentTime = 1.5;
v2_numFrames = ceil(v2.FrameRate*v2.Duration);
i1 = 0;
i2 = 0;
fig = gcf;
ax1 = subplot(2,2,1, 'Parent', fig);
ax2 = subplot(2,2,2, 'Parent', fig);
currAxes = axes;
while i1 < v1_numFrames && i2 < v2_numFrames
if i1 < v1_numFrames
i1 = i1+1;
v1frame = v1.read(i1);
if isempty(v1frame) %unexpected!
warning(sprintf('skipped v1 empty frame #%d', i1));
elseif ishandle(ax1)
image(v1frame, 'Parent', ax1);
else
break; %axes is gone, figure is probably gone too
end
end
if i2 < v2_numFrames
i2 = i2+1;
v2frame = v2.read(i2);
if isempty(v2frame) %unexpected!
warning(sprintf('skipped v2 empty frame #%d', i2));
elseif ishandle(ax2)
image(v2frame, 'Parent', ax2);
end
end
drawnow
end
Thanks

 채택된 답변

Walter Roberson
Walter Roberson 2018년 1월 9일

0 개 추천

You use v1.read(i1) after v1.CurrentTime = 1.5 . You can only use read() if you have never set the CurrentTime. You will need to switch to readFrame()
Warning: multiplying framerate by duration has round-off error even for fixed framerates, and is just plain wrong for variable frame rates. The only way to accurately know the number of frames is to loop reading frames and counting them.
I recommend that you do not bother to try to estimate frames and instead use a structure like
while true
got_one = false;
had_problem = false;
if hasFrame(v1)
v1frame = readFrame(v1);
if isvalid(ax1)
image(v1frame, 'Parent', ax1);
got_one = true;
else
had_problem = true;
end
end
if had_problem; break; end
if hasFrame(v2)
v2frame = readFrame(v2);
if isvalid(ax2)
image(v2frame, 'Parent', ax2);
got_one = true;
else
had_problem = true;
end
end
if had_problem; break; end
if ~got_one; break; end
drawnow();
end
Except that I would recommend changing the CData of the image objects instead of using image() calls, for performance reasons.

댓글 수: 4

Hongnan Lin
Hongnan Lin 2018년 1월 9일
Thanks Walter! It's really helpful. However, I have another problem which is if I don't Ctrl+C, the code will never stop running and were displayed. Is there any way to solve it? Thanks!
Walter Roberson
Walter Roberson 2018년 1월 9일
The got_one logic is designed to end as soon as both objects run out of frames. Could you do a bit of debugging to see which of the two objects is thinking it has remaining frames? The code I posted should end as soon as the longer video ends.
Hongnan Lin
Hongnan Lin 2018년 1월 10일
Thanks for the reply Walter. Actually the videos I want to play are of the same frame numbers.
Walter Roberson
Walter Roberson 2018년 1월 10일
When you use the debugger, is hasFrame(v1) or hasFrame(v1) returning true?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Hongnan Lin
Hongnan Lin 2018년 1월 10일
편집: Walter Roberson 2018년 1월 10일

0 개 추천

Thanks Walter!
Here is another problem:
As attached is the figure to show, I'd rather hide all the axis however, there are only axis in the last plot hidden.
Following is the code:
while true
thisFrame = getframe(gcf);
i1 = i1+1
got_one = false ;
had_problem = false ;
if hasFrame(v1)
v1frame = readFrame(v1);
if isvalid(ax1)
image(v1frame, 'Parent', ax1);
axis off;
got_one = true ;
else
had_problem = true ;
end
end
if had_problem;
break;
end
if hasFrame(v2)
v2frame = readFrame(v2);
if isvalid(ax2)
image(v2frame, 'Parent', ax2);
axis off;
got_one = true ;
else
had_problem = true ;
end
end
if had_problem;
break;
end;
if ~got_one;
break;
end
image(v3,'Parent', ax3);%for .png
axis off;
% set(gcf,'PaperUnits','inches','Position',[1 1 1500 400]);
writeVideo(v, thisFrame);
Thanks
Lin

댓글 수: 1

Walter Roberson
Walter Roberson 2018년 1월 10일
편집: Walter Roberson 2018년 1월 10일
axis(ax1, 'off');
and
axis(ax2, 'off');
and
axis(ax3, 'off')
Note: it is more efficient to update the CData property of an existing image than to image() and axis()

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Convert Image Type에 대해 자세히 알아보기

질문:

2018년 1월 9일

편집:

2018년 1월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by