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 Comments

My computer is relatively new and according to my storage report I have several hundred Gb available, so I don't think that is the culprit.

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

Ok thank you for the suggestion. I modified the code so that
F = getframe(gcf);
writeVideo(video,F)
is called inside the loop, immediately after I call
contourf(x,y...)
With my thinking that this would ensure that F is never empty or nonexistant, assuming the the contour call is done correctly (which is appears is the case). Does that seem reasonable?
Even with the change however, I still run into the same error.
Ok. That's something strange.
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.
Thanks for the reply. I have just edited my original post to include a basic, minumum working example. Even when I make sure to not change the size of the figure or click on any other figures, I still run into the same problem.
perhaps you do not have write permission for the current directory?
I have tried in several different directories and have been able to write other files to those directories, just not videos. I am quite puzzled at this point haha.
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.
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.
@Paul Can your drive your code by removing the pause?
@Walter Roberson This is on mac.
@Bruno Luong I tried removing the pause but with no luck.
Thank you for your contined time!
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

"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.

Products

Release

R2022b

Tags

Asked:

on 26 Jul 2023

Edited:

on 28 Jul 2023

Community Treasure Hunt

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

Start Hunting!