title: Why i'm getting strange video file when using ffmpeg and pipes to create video file in real time?
body: <p>The goal here is to create a compressed mp4 video file in real time.
  I'm saving screenshots as bitmaps type on my hard disk.
  And i want to create mp4 file and compress the mp4 video file in real time.</p>
  
  <p>The problem is the end the video file i get looks very strange.
  This is the result: <a href="https://www.youtube.com/watch?v=fdxPus-Xv1k&amp;feature=youtu.be" rel="nofollow noreferrer">Strange Video File</a></p>
  
  <p>The class that i'm using the ffmpeg with arguments.</p>
  
  <pre><code>using System;
  using System.Windows.Forms;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Drawing;
  using System.IO.Pipes;
  using System.Runtime.InteropServices;
  using System.Diagnostics;
  using System.IO;
  using DannyGeneral;
  
  namespace Youtube_Manager
  {
      class Ffmpeg
      {
          NamedPipeServerStream p;
          String pipename = "mytestpipe";
          System.Diagnostics.Process process;
          string ffmpegFileName = "ffmpeg.exe";
          string workingDirectory;
  
          public Ffmpeg()
          {
              workingDirectory = Path.GetDirectoryName(Application.ExecutablePath);
              Logger.Write("workingDirectory: " + workingDirectory);
              if (!Directory.Exists(workingDirectory))
              {
                  Directory.CreateDirectory(workingDirectory);
              }
              ffmpegFileName = Path.Combine(workingDirectory, ffmpegFileName);
              Logger.Write("FfmpegFilename: " + ffmpegFileName);
          }
  
          public void Start(string pathFileName, int BitmapRate)
          {
              try
              {
  
                  string outPath = pathFileName;
                  p = new NamedPipeServerStream(pipename, PipeDirection.Out, 1, PipeTransmissionMode.Byte);
  
                  ProcessStartInfo psi = new ProcessStartInfo();
                  psi.WindowStyle = ProcessWindowStyle.Hidden;
                  psi.UseShellExecute = false;
                  psi.CreateNoWindow = false;
                  psi.FileName = ffmpegFileName;
                  psi.WorkingDirectory = workingDirectory;
                  psi.Arguments = @"-f rawvideo -pix_fmt yuv420p -video_size 1920x1080 -i \\.\pipe\mytestpipe -map 0 -c:v mpeg4 -r " + BitmapRate + " " + outPath;
                  //psi.Arguments = @"-framerate 1/5 -i -c:v libx264 -r 30 -pix_fmt yuv420p \\.\pipe\mytestpipe -map 0 -c:v mpeg4 -r" + BitmapRate + " " + outPath;
                  process = Process.Start(psi);
                  process.EnableRaisingEvents = false;
                  psi.RedirectStandardError = true;
                  p.WaitForConnection();
              }
              catch (Exception err)
              {
                  Logger.Write("Exception Error: " + err.ToString());
              }
          }
  
          public void PushFrame(Bitmap bmp)
          {
              try
              {
                  int length;
                  // Lock the bitmap's bits.
                  //bmp = new Bitmap(1920, 1080);
                  Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                  //Rectangle rect = new Rectangle(0, 0, 1280, 720);
                  System.Drawing.Imaging.BitmapData bmpData =
                      bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
                      bmp.PixelFormat);
  
                  int absStride = Math.Abs(bmpData.Stride);
                  // Get the address of the first line.
                  IntPtr ptr = bmpData.Scan0;
  
                  // Declare an array to hold the bytes of the bitmap.
                  //length = 3 * bmp.Width * bmp.Height;
                  length = absStride * bmpData.Height;
                  byte[] rgbValues = new byte[length];
  
                  //Marshal.Copy(ptr, rgbValues, 0, length);
                  int j = bmp.Height - 1;
                  for (int i = 0; i &lt; bmp.Height; i++)
                  {
                      IntPtr pointer = new IntPtr(bmpData.Scan0.ToInt32() + (bmpData.Stride * j));
                      System.Runtime.InteropServices.Marshal.Copy(pointer, rgbValues, absStride * (bmp.Height - i - 1), absStride);
                      j--;
                  }
                  p.Write(rgbValues, 0, length);
                  bmp.UnlockBits(bmpData);
              }
              catch(Exception err)
              {
                  Logger.Write("Error: " + err.ToString());
              }
  
          }
  
          public void Close()
          {
              p.Close();
          }
      }
  }
  </code></pre>
  
  <p>Then i'm using the method PushFrame here in this class:</p>
  
  <pre><code>public Bitmap GetScreenShot(string folder, string name)
      {
          _screenShot = new Bitmap(GetScreen());
          System.GC.Collect();
          System.GC.WaitForPendingFinalizers();
          string ingName = folder + name +  images.counter.ToString("D6") + ".bmp";
          _screenShot.Save(ingName,System.Drawing.Imaging.ImageFormat.Bmp);
          fmpeg.PushFrame(_screenShot);
          _screenShot.Dispose();
  
          return _screenShot;
      }
  </code></pre>
  
  <p>The Bitmaps on the hard disk are fine i can edit/open see them.
  When using command prompt and manually type ffmpeg command it does compress and create a mp4 video file.</p>
  
  <p>But when using the Ffmpeg class i did with the PushFrame method it's creating this strange video file.</p>
  
  <p>This is a link for my OneDrive with 10 screenshots images files for testing:</p>
  
  <p><a href="https://onedrive.live.com/redir?resid=EB1C71C44C3976D5!1345&amp;authkey=!AP6_vdjPjYPo-8Q&amp;ithint=file%2Crar" rel="nofollow noreferrer">screenshots rar</a></p>
  
  <p>Sample screenshot from the video file when playing the video file:
  Looks very choppy. The Bitmaps on the hard disk each one is 1920x1080 and Bit depth 32</p>
  
  <p>But it dosen't look like that in the video file:</p>
  
  <p><img src="https://i.stack.imgur.com/Z8Lkt.jpg" alt="choppy image"></p>
  
  <p>This is the arguments i'm using:</p>
  
  <pre><code>psi.Arguments = @"-f rawvideo -vcodec rawvideo -pix_fmt rgb24 -video_size 1920x1080 -i \\.\pipe\mytestpipe -map 0 -c:v mpeg4 -r " + BitmapRate + " " + outPath;
  </code></pre>
  
  <p>The video size is very small 1.24 MB</p>