Random Video Writer Error on Close
显示 更早的评论
HI,
When I try to run my code I get his warning:
Warning: No video frames were written to this file. The file may be
invalid.
> In VideoWriter/close (line 282)
In postprocessing (line 194)
%% Experiment 13 - Heavy Hammer Plate 6 w\ROI(181:284,381:454)
fprintf("Starting Experiment 13 \n\n");
%Importing All Frames for 13
folder = "E:\Uni\Imaging\23-11\13";
files = dir(fullfile(folder, "*.mat"));
N_frames = size(files,1);
directory = 'Rec--000013_%d.mat';
t = 0:0.1:N_frames;
% Create a VideoWriter object to write the video out to a new, different file.
writerObj = VideoWriter("test_13.avi",'Uncompressed AVI');
writerObj.FrameRate = 10; % How many frames per second.
open(writerObj)
% Need to change from the default renderer to zbuffer to get it to work right.
% openGL doesn't work and Painters is way too slow.
set(gcf, 'renderer', 'zbuffer');
%% Create the movie.
% After this loop starts, BE SURE NOT TO RESIZE THE WINDOW AS IT'S SHOWING THE FRAMES, or else you won't be able to save it.
%Import frames
currenttemps = zeros(1,N_frames);
for frameIndex = 1:N_frames %To number of frames N_frames (after 500 frames are useless)
cla reset;
filename = sprintf(directory, frameIndex);
filename = append(folder,"\",filename);
load(filename, "Frame"); %Check if files exists
%Absolute Frame Post Process
currentFrame = Frame - min(Frame(:));
%ROI_temps = Frame(181:284,381:454);
%currenttemps(frameIndex) = mean(ROI_temps(:));
imagesc(currentFrame);
%hold on
%rectangle('Position',[381 181 73 103])
caption = sprintf('Experiment 13: Frame #%d of %d, t = %.1f', frameIndex, N_frames, t(frameIndex));
title(caption, 'FontSize', 15);
colorbar
thisFrame = getframe(gcf);
%Write this frame out to a new video file.
writeVideo(writerObj, thisFrame);
end
%hold off
close all;
close(writerObj); %THIS IS LINE 194
%% Plot Average Temps of ROI
tempFig13 = figure('Name','ROI_TEMPS');
plot(t(1:N_frames),currenttemps)
ylabel('Average Temp of ROI (C)');
xlabel('Time(s)')
xticks(0:5:max(t))
saveas(tempFig13,'ROItemps_13.png');
I use pretty much the exact same code but for a differente experiment earlier in the script and I have no issues. This works fine:
%% Experiment 08 - Water Jet (7) w\ROI(110:346,365:566)
fprintf("Starting Experiment 8 \n\n");
%Importing All Frames for Investigation 08
folder = 'G:\FYP\23-11\8';
files = dir(fullfile(folder, '*.mat'));
N_frames = size(files,1) -1;
directory = 'Rec--000008_10HZ_%d.mat';
t = 0:0.1:(N_frames-1);
% Create a VideoWriter object to write the video out to a new, different file.
writerObj = VideoWriter("test_8.avi",'Uncompressed AVI');
writerObj.FrameRate = 10; % How many frames per second.
open(writerObj);
% Need to change from the default renderer to zbuffer to get it to work right.
% openGL doesn't work and Painters is way too slow.
set(gcf, 'renderer', 'zbuffer');
%% Create the movie.
% After this loop starts, BE SURE NOT TO RESIZE THE WINDOW AS IT'S SHOWING THE FRAMES, or else you won't be able to save it.
%Import frames
currenttemps = zeros(1,N_frames);
for frameIndex = 1:N_frames %To number of frames N_frames (after 800 frames are useless)
cla reset;
filename = sprintf(directory, frameIndex);
filename = append(folder,"\",filename);
load(filename, "Frame"); %Check if files exists
%Absolute Frame Post Process
currentFrame = Frame - min(Frame(:));
ROI_temps = Frame(110:346,365:566);
currenttemps(frameIndex) = mean(ROI_temps(:));
imagesc(currentFrame);
hold on
rectangle('Position',[365 110 201 236])
caption = sprintf('Experiment 8: Frame #%d of %d, t = %.1f', frameIndex, N_frames, t(frameIndex));
title(caption, 'FontSize', 15);
colorbar
thisFrame = getframe(gcf);
%Write this frame out to a new video file.
writeVideo(writerObj, thisFrame);
end
hold off
close all;
close(writerObj);
%% Plot Average Temps of ROI
tempFig8 = figure('Name','ROI_TEMPS');
plot(t(1:N_frames),currenttemps)
ylabel('Average Temp of ROI (C)');
xlabel('Time(s)')
xticks(0:10:max(t))
saveas(tempFig8,'ROItemps_8.png');
Any suggestions on what might be causing this issue with the VideoWriter?
Thanks
回答(1 个)
Walter Roberson
2022-4-7
folder = "E:\Uni\Imaging\23-11\13";
files = dir(fullfile(folder, "*.mat"));
.mat files are looked for directly under "E:\Uni\Imaging\23-11\13\";
directory = 'Rec--000013_%d.mat';
%...
filename = sprintf(directory, frameIndex);
filename = append(folder,"\",filename);
load(filename, "Frame"); %Check if files exists
You do not load those files that were looked for. All you do is count all those .mat files. Then you look for files named with the pattern Rec--000013_%d.mat and try to load them out of "E:\Uni\Imaging\23-11\13\" But the .mat files that are actually present in that directory do not necessarily follow that pattern.
You would get the error message you go in the case where you wrote zero or one frames to the video (you need to write at least two frames to create a video.)
类别
在 帮助中心 和 File Exchange 中查找有关 Video Formats and Interfaces 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!