Errors Using videoWriter and problems closing video writer
Show older comments
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
Walter Roberson
on 26 Jul 2023
Out of disk space?
Paul
on 26 Jul 2023
VBBV
on 26 Jul 2023
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
on 26 Jul 2023
VBBV
on 26 Jul 2023
Ok. That's something strange.
Bruno Luong
on 26 Jul 2023
Edited: Bruno Luong
on 26 Jul 2023
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
on 26 Jul 2023
Walter Roberson
on 26 Jul 2023
perhaps you do not have write permission for the current directory?
Paul
on 27 Jul 2023
Bruno Luong
on 27 Jul 2023
Edited: Bruno Luong
on 27 Jul 2023
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
on 27 Jul 2023
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
on 27 Jul 2023
Edited: Bruno Luong
on 27 Jul 2023
Paul
on 27 Jul 2023
Walter Roberson
on 27 Jul 2023
I did not have any trouble on my R2022b installation on Ventura.
Which MacOS release are you using?
Answers (1)
Paul
on 27 Jul 2023
Moved: Walter Roberson
on 28 Jul 2023
0 votes
1 Comment
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.
Categories
Find more on Image Preview and Device Configuration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!