movie2avi has been removed
    14 views (last 30 days)
  
       Show older comments
    
    Siddharth Kurkure
 on 26 Oct 2018
  
    
    
    
    
    Commented: Walter Roberson
      
      
 on 2 Dec 2022
            Hi, I was trying to use movie2avi but it was removed in the 2016 release (I am working on 2017b) and I need some help trying to use the VideoWriter. The below code gave me the following error: all 'cdata' fields in FRAMES must be the same size. What exactly does that mean? How would I be able to do what movie2avi does, but in such a compatibility with 2016/later releases, since I am getting errors in my implementation?
 data = fread(fid, [1692, 480], '*uchar');    %read in the data
    imageData=data';
    imshow(uint8(imageData), 'Border', 'tight');
    set(fig,'Position',[1,1,1692,480]);
    M(:,:,count)=getframe(fig);%getframe(data);
    frame=frame+1;
    if(count==100)
        count=1;
        %movie2avi(M,'framefile.avi','fps', 15, 'compression', 'None');
        %movie2avi(M,'filename.avi','fps', 35, 'compression', 'Cinepak');
        vw = VideoWriter('filename.avi');
        open(vw);
        writeVideo(vw,M);
        close(vw);
        %fileName = sprintf('video%d.avi',videoId); 
        %movefile('filename.avi',fileName);
        videoId=videoId+1;
        clear M;
    else
0 Comments
Accepted Answer
  Guillaume
      
      
 on 30 Oct 2018
        The code you're showing is obviously meant to be in a loop which you haven't showned. That makes it harder to pinpoint the cause of the problem.
A red flag is this line:
M(:,:,count)=getframe(fig);
getframe always return a scalar structure so it's unclear why it's being put into a 3D array. At best, dimensions 1 and 2 of the existing M are singleton, in which case the above is the same as
M(1, 1, count) = getframe(fig);
If they're not singleton, then the scalar structure will be replicated as many times as necessary to fill these dimensions. A complete waste. It looks like the code was written expecting that getframe returns a 2D image instead of the structure it returns.
The error tells you that one of the cdata field of M is not the same size as the others. So look through that structure array, see which frame has a different size, then find out why. Since we don't see how you initialise M, it's possible that you're using a M from a previous run that had a different size.
Inferring from the code you show, it looks like you never preallocate M, just let it grow to 100, then clear it. That's very inefficient and error prone. Your loop should be more something like:
%naming the variable frames instead of M since the name M doesn't say anything about the variable content
frames = struct('cdata', cell(1, 100), 'colormap', cell(1, 100));  %preallocate structure with 100 elements
count = 1;
while ...   %whatever the loop code is
  %...
  %code to read the image and display it. Unchanged
  frames(count) = getframe(fig);  %acquire frame
  if count == 100
      vw = VideoWriter(sprintf('filename%d.avi', videoId));  %taking a guess that you intend to modify the filename each time you write a video
      open(vw);
      writeVideo(vw, frames);
      videoId = videoId + 1;
  end
  count = mod(count, 100) + 1;
end
More Answers (3)
  Harish Ramachandran
    
 on 29 Oct 2018
        As you mentioned, movie2avi has been deprecated and instead replaced with VideoWriter.
Can you verify if getframe CDATA is consistent in dimensions? The error message points to an issue there?
Also, a sample example code would be:
v = VideoWriter ('test.avi');
open (v);  
II = 0;
for I = 0: 1: 50
II = II + 1;  
X = 0: 0.1: 10;% X coordinate
XWT = X + 0.2 * I;% X + 0.2 I
Y = sin (XWT);  
plot (X, Y); 
hold on
drawnow;
frame = getframe (gcf);
writeVideo (v, frame);  
hold off;
end  
close (v)
2 Comments
  Guillaume
      
      
 on 30 Oct 2018
				The error message is telling you that one of the image you're trying to write to the video has a different size than the previous one(s). Whether it's VideoWriter or movie2avi all the frames you write to the video must be the same size.
The image acquired by getframe is stored in the cdata field of the structure it retuns. So some of the M.cdata images in your code do not have the same size.
  Dominique
 on 12 Feb 2022
        
      Edited: Walter Roberson
      
      
 on 12 Feb 2022
  
      % 2022-02-11 : copié-collé par Dominique.Beaulieu@gmail.com
%
% Source : aide Matlab.
% But : pour compenser la disparition de movie2avi.
%
% Programmé en mode "quick and dirty".
%
% Entrées :
%   StrMovie : structure Matlab contenant le "movie"
%       Exemple : 
%                   StrMov = MonMovie
%                   MonMovie ==> 1×124 struct array with fields:
%       Champs :
%                   cdata: [428×531×3 uint8]
%                   colormap: []
%
%   sAvi : chaîne de caractères (s pour string) contenant le nom du fichier
%       Exemple :
%                   sAvi = 'MonAnimation.avi'
%
% Fichier : Mov2Avi.m
function Mov2Avi(StrMov, sAvi)
N = size(StrMov,2);
% Prepare the new file.
vidObj = VideoWriter(sAvi);
open(vidObj);
% Create an animation.
axis tight manual;
set(gca,'nextplot','replacechildren');
for k = 1:N
    h=figure;
    imshow(StrMov(k).cdata);
    currFrame = getframe(h);
    writeVideo(vidObj,currFrame);
    close(h);
end
% Close the file.
close(vidObj);
1 Comment
  Walter Roberson
      
      
 on 12 Feb 2022
				Displaying the movie and capturing frames is unnecessary and will degrade the images.
function Mov2Avi(StrMov, sAvi)
N = size(StrMov,2);
% Prepare the new file.
vidObj = VideoWriter(sAvi);
open(vidObj);
for k = 1:N
    currFrame = StrMov(k).cdata;
    cmap = StrMov(k).colormap;
    if ~isempty(cmap); currFrame = ind2rgb(currFrame, cmap); end
    writeVideo(vidObj,currFrame);
end
% Close the file.
close(vidObj);
  Sergio Ramirez Seañez
 on 2 Dec 2022
        
      Edited: Walter Roberson
      
      
 on 2 Dec 2022
  
      Hi, im trying to use this code: https://la.mathworks.com/matlabcentral/fileexchange/27212-animated-double-pendulum?s_tid=srchtitle_double%2520pendulum_2  in Matlab 2020, but only this window appears like this:(

1 Comment
  Walter Roberson
      
      
 on 2 Dec 2022
				function movie2avi(StrMov, sAvi, varargin)
N = size(StrMov,2);
% Prepare the new file.
vidObj = VideoWriter(sAvi, varargin{:});
open(vidObj);
for k = 1:N
    currFrame = StrMov(k).cdata;
    cmap = StrMov(k).colormap;
    if ~isempty(cmap); currFrame = ind2rgb(currFrame, cmap); end
    writeVideo(vidObj,currFrame);
end
% Close the file.
close(vidObj);
See Also
Categories
				Find more on Audio and Video Data 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!



