Errors Using videoWriter and problems closing video writer

22 views (last 30 days)
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 Comments
Paul
Paul on 27 Jul 2023
@Walter Roberson This is on mac.
@Bruno Luong I tried removing the pause but with no luck.
Thank you for your contined time!
Walter Roberson
Walter Roberson on 27 Jul 2023
I did not have any trouble on my R2022b installation on Ventura.
Which MacOS release are you using?

Sign in to comment.

Answers (1)

Paul
Paul on 27 Jul 2023
Moved: Walter Roberson on 28 Jul 2023
@Walter Roberson Ventura 13.4. I just tried restarting my computer again, and after the restart, I was able to run the code successfully. I am not sure what changed, but I appreciate your help and time!
  1 Comment
Bruno Luong
Bruno Luong on 28 Jul 2023
Edited: Bruno Luong on 28 Jul 2023
"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.

Sign in to comment.

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!