Errors Using videoWriter and problems closing video writer
显示 更早的评论
EDIT
I have added a basic, minimum working example to highlight my problem below.
% test out the movie making
clear;
start = -1;
ending = 1;
f1 = figure(1);
ax1 = axes;
video = VideoWriter('test.mp4','MPEG-4');
video.FrameRate = 5;
open(video);
for i = 1:100;
start = start - .1;
ending = ending + .1;
xDomain = start:0.1:ending;
y = xDomain.^2;
pt1 = plot(ax1,xDomain,y);
drawnow
pause(.1)
F = getframe(f1);
writeVideo(video,F);
end
close(video)
When trying to run this code, I run into the same error as before:
Error using VideoWriter/writeVideo
Could not write a video frame.
Thank you to those who have responded so far, and I appreciate any further insights people may have.
Thank you!
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ORIGINAL POST
Hello,
I have a script that iterates through a while loop and with each iteration creates a figure that is then saved as a frame and ultimately added to a video. A couple weeks ago this script worked just fine for me, but today I found that I am no longer able to save a video using videoWriter. I haven't made any modifications to the script since it was previously working.
The code to create the figures is:
while t<=tmax+dt/2
q1=real(ifft2(qh1.*trunc));
ph1=invert(qh1,wv2inv);
[u1,v1]=caluv(ph1,k,l,trunc);
dqh1dt=-advect(q1+top,u1+U1,v1,k,l)-beta1*i*k.*ph1-rek*qh1+forcingSpec; % removed the fft2(r.*q1) term
if(rem(tc,tpl)==0)
ts=[ts,t];
stat=[stat,[0.5*sqrt(mean(vec(u1).^2+vec(v1).^2));sqrt(mean(mean(u1.^2))/mean(mean(v1.^2)));max(max(q1));min(min(q1))]];
figure(1)
contourf(x,y,q1,8);title(sprintf('q : t = %g',t));
colorbar
drawnow;
pause(0.1)
F(counter) = getframe(gcf); % save the frame to a variable F
counter = counter + 1; % get ready for the next frame
drawnow;
end
And the code for writing the frames to a video is
%Make a video
video = VideoWriter('withBeta.mp4','MPEG-4');
video.FrameRate = 20;
open(video);
writeVideo(video,F);
close(video);
After the script runs in its entirety, I get the following error:
Error using VideoWriter/writeVideo
Could not write a video frame.
Error in qg1p_step_12810mods (line 98)
writeVideo(video,F);
Futhermore, when testing on the code on just a small subset of the frames, I seem to be able to successfully run
open(video);
for i=1:20; %total number of frames is 50
i
writeVideo(video,F(i));
end
close(video);
for about 10 or so, but then I arrive at the same error as before.
Additionally, whenever I try to call
close(video);
I appear to get stuck in an infinite loop in lines 282-292 of VideoWriter.close
while obj(ii).InternalFramesWritten ~= ...
obj(ii).Profile.VideoProperties.FrameCount
drawnow('limitrate');
end
Any advice or suggestions is much appreciated! As I mentioned before, the code worked just fine a couple weeks ago, and I have not made any modifications since then.
14 个评论
Walter Roberson
2023-7-26
Out of disk space?
Paul
2023-7-26
VBBV
2023-7-26
It seems that all frames have not been recorded /captured correctly to F due to loop conditions. Some frames are just empty or nonexistent. Check the loop conditions so that all frames are present in F
Paul
2023-7-26
VBBV
2023-7-26
Ok. That's something strange.
Bruno Luong
2023-7-26
编辑:Bruno Luong
2023-7-26
F = getframe(gcf);
That is not robust, since gcf can change if user clicks on another figure.
Also makesure you plot in the right figure (contour with proper target handle). I see you do not control any target handle in your code. I never do such thing in mine.
IIRC, if you can the size of the figure during the loop, writeVideo might also fails.
Paul
2023-7-26
Walter Roberson
2023-7-26
perhaps you do not have write permission for the current directory?
Paul
2023-7-27
Bruno Luong
2023-7-27
编辑:Bruno Luong
2023-7-27
Your MWE code works fine on my PC, meaning no error occurs.
Shooting in the dark, I can see 2 potential causes on your PC:
- Haddware issue
- Some SW/services (may be OS; antivirus) running in the background takes over the file that beeing created and preventing videowrite to work.
Walter Roberson
2023-7-27
When I look through what has been posted, I cannot be sure that this is Windows. If it were Linux there would be additional potential problems with shared libraries.
If it is Windows then maybe there is a problem with Windows Media Services.
Bruno Luong
2023-7-27
编辑:Bruno Luong
2023-7-27
Paul
2023-7-27
Walter Roberson
2023-7-27
I did not have any trouble on my R2022b installation on Ventura.
Which MacOS release are you using?
回答(1 个)
Paul
2023-7-27
移动:Walter Roberson
2023-7-28
0 个投票
1 个评论
Bruno Luong
2023-7-28
编辑:Bruno Luong
2023-7-28
"I am not sure what changed"
It could be that the same file are opened by dead zombie videobject that open but not close.
To prevent such thing happens, instead of
open(video);
...
close(video)
do
open(video);
close_trigger = onCleanup(@() close(video));
...
clear close_trigger
If third party SW opens the file without closing it properly, then either you chase to find the culprit or restart your computer.
类别
在 帮助中心 和 File Exchange 中查找有关 Image Preview and Device Configuration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!