title: Matlab: Why can't I create an mp4 file using VideoWriter?
body: <p>I want to remove the background of a video. After that I want to put it into another video and then save it as a new mp4-file. I'm using Windows 10 and I have the following problem that I can't create a new mp4-file after I run my code.</p>
  
  <p>Idea:
  I have a car with a greenscreen in the back. I want to cut this car out and put it in front of the street video.
  result should be a car driving on the street(video).</p>
  
  <p>code from myScript:</p>
  
  <pre><code>clear
  close all
  v = VideoReader(fullfile('videos', 'carWithGreenscreen.mp4'));
  s = VideoReader(fullfile('videos','street.mp4'));
  
  video=zeros(v.Height,v.Width,3,floor(v.Duration/(1/v.FrameRate))-2,'uint8');
  t=1;
  car = 0.6 ;
  values=[0;1;0];
  while hasFrame(v)
      Img = im2double(readFrame(v));
      mask = sqrt((values(1)-Img(:, :, 1)).^2+(values(2)-Img(:, :, 2)).^2+(values(3)-Img(:, :, 3)).^2)&lt;car;
      video(:,:,:,t) = imoverlay(im2uint8(Img),mask,[0 0 0]);
      imshow(video(:,:,:,t));
      t=t+1;
  end
  
  videoOut = VideoWriter('result.mp4','MPEG-4'); %here I want to create a video%
  videoOut.FrameRate=v.FrameRate;
  open(videoOut);
  moveX=0;
  for t=1:1:size(video,4)
      im = video(:,:,:,t);
      mask = im(:,:,1)+im(:,:,2)+im(:,:,3)==0;
      frame=imoverlay(readFrame(s),~mask,[0 0 0]);
      frame=frame+im;
      writeVideo(videoOut,frame);
      imshow(frame);
  end
  close(videoOut);
  </code></pre>
  
  <p>After I run this code following error occurs:</p>
  
  <pre><code>Error using myScript (line 27)
  No more frames available to read from file.
  line 27: frame=imoverlay(readFrame(s),~mask,[0 0 0]);
  </code></pre>
  
  <p>But at the end it doesn't create a mp4 file... I have tried it out with avi but I only creates an empty .avi file with 0KB...</p>