title: Save Video Stream in Chunks From Camera
body: <p>I currently have a MJPEG camera that I need to capture and save its video in hour intervals.</p>
  
  <p>I have tried to achieve this by chunking the incoming data and saving it all to a file, but the file that is creates is just a corrupted version of the video stream about 1 second long.</p>
  
  <p>Here is what I have</p>
  
  <pre><code>request = WebRequest.Create("http://MYVIDEOURL.videostream.cgi");
  
  request.Credentials = new NetworkCredential("USERNAME", "PASSWORD");
  
  request.Method = "POST";
  
  WebResponse response = request.GetResponse();
  
  using(var fs = new FileStream(@"C:\TestVideo.avi", FileMode.Create)) {
  
      using(var repStream = response.GetResponseStream()) {
  
           while (!bw.CancellationPending)//this is for the background worker
           {
  
               byte[] b = new byte[4096];
  
               int bytesRead = repStream.Read(b, 0, b.Length);
  
               repStream.Read(b, 0, bytesRead);
  
               fs.Write(b, 0, bytesRead);
  
  
           }//end while
  
           fs.Close();
  
      }//end using
  
  }//end using
  </code></pre>
  
  <p>All of that is in a backgroundworker and I have a timer in a thread that runs it for the amount of time specified.</p>
  
  <p>This method I am using is connecting to the stream, but as I said only saving one second of video, and the video is corrupted.</p>