title: FFmpeg extracts black image from H264 stream
body: <p>I have a C# application that receives H264 stream through a socket. I want to continuously get the latest image from that stream.</p>
  <p>Here's what I did with FFmpeg 5.0.1, just a rough sample to get ONE latest image, how I start FFmpeg:</p>
  <pre><code>var ffmpegInfo = new ProcessStartInfo(FFMPEG_PATH);
  ffmpegInfo.RedirectStandardInput = true;
  ffmpegInfo.RedirectStandardOutput = true;
  ffmpegInfo.RedirectStandardError = true;
  ffmpegInfo.UseShellExecute = false;
  
  ffmpegInfo.Arguments = &quot;-i pipe: -f h264 -pix_fmt bgr24 -an -sn pipe:&quot;;
  
  ffmpegInfo.UseShellExecute = false;
  ffmpegInfo.CreateNoWindow = true;
  
  Process myFFmpeg = new Process();
  myFFmpeg.StartInfo = ffmpegInfo;
  myFFmpeg.EnableRaisingEvents = true;
  myFFmpeg.Start();
  
  var inStream = myFFmpeg.StandardInput.BaseStream;
  FileStream baseStream = myFFmpeg.StandardOutput.BaseStream as FileStream;
  myFFmpeg.BeginErrorReadLine();
  </code></pre>
  <p>Then I start a new thread to receive the stream through socket:</p>
  <pre><code>// inStream is &quot;myFFmpeg.StandardInput.BaseStream&quot; from the code block above
  var t = Task.Run(() =&gt; ReceiveStream(inStream));
  </code></pre>
  <p>Next I read the output from FFmpeg:</p>
  <pre><code>byte[] decoded = new byte[Width * Height * 3];
  int numBytesToRead = Width * Height * 3;
  int numBytesRead = 0;
  
  while (numBytesToRead &gt; 0)
  {
     int n = baseStream.Read(decoded, 0, decoded.Length);
     Console.WriteLine($&quot;Read {n} bytes&quot;);
     if (n == 0)
     {
        break;
     }              
        numBytesRead += n;
        numBytesToRead -= n;
  }
  </code></pre>
  <p>Lastly, I use ImageSharp library to save <code>decoded</code> byte array as a jpeg file.</p>
  <pre><code>image.Save(&quot;test.jpeg&quot;, encoder);
  </code></pre>
  <p>However, <code>test.jpeg</code> always comes out as a black image. What did I do wrong?
  Here's the stderr log that I got from ffmpeg:</p>
  <pre><code>
    Duration: N/A, bitrate: N/A
    Stream #0:0: Video: h264 (High), yuv420p(tv, smpte170m/bt470bg/smpte170m, progressive), 1080x2256, 25 fps, 25 tbr, 1200k tbn
  Stream mapping:
    Stream #0:0 -&gt; #0:0 (h264 (native) -&gt; h264 (libx264))
  Incompatible pixel format 'bgr24' for codec 'libx264', auto-selecting format 'yuv444p'
  [libx264 @ 0x11b9068f0] using cpu capabilities: ARMv8 NEON
  [libx264 @ 0x11b9068f0] profile High 4:4:4 Predictive, level 5.0, 4:4:4, 8-bit
  Output #0, h264, to 'pipe:':
    Metadata:
      encoder         : Lavf59.16.100
    Stream #0:0: Video: h264, yuv444p(tv, smpte170m/bt470bg/smpte170m, progressive), 1080x2256, q=2-31, 25 fps, 25 tbn
      Metadata:
        encoder         : Lavc59.18.100 libx264
      Side data:
        cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
  frame=    1 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A speed=   0x    
  frame=   56 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A speed=   0x
  frame=   87 fps= 83 q=28.0 size=     370kB time=00:00:01.16 bitrate=2610.6kbits/s speed=1.11x
  frame=  118 fps= 75 q=28.0 size=     698kB time=00:00:02.40 bitrate=2381.4kbits/s speed=1.54x
  frame=  154 fps= 75 q=28.0 size=    1083kB time=00:00:03.84 bitrate=2311.1kbits/s speed=1.86x
  ...
  </code></pre>
  <p>Thank you!</p>
  <p>Edit: as suggested by @kesh, I have changed <code>h264</code> to <code>rawvideo</code>, the arguments now are: <code>-i pipe: -f rawvideo -pix_fmt bgr24 -an -sn pipe:</code></p>
  <p>Here's the output of ffmpeg:</p>
  <pre><code>Input #0, h264, from 'pipe:':
    Duration: N/A, bitrate: N/A
    Stream #0:0: Video: h264 (High), yuv420p(tv, smpte170m/bt470bg/smpte170m, progressive), 1080x2256, 25 fps, 25 tbr, 1200k tbn
  Stream mapping:
    Stream #0:0 -&gt; #0:0 (h264 (native) -&gt; rawvideo (native))
  // About 9 of these &quot;No accelerated colorspace...&quot; message
  [swscaler @ 0x128690000] [swscaler @ 0x1286a0000] No accelerated colorspace conversion found from yuv420p to bgr24.
  Output #0, rawvideo, to 'pipe:':
    Metadata:
      encoder         : Lavf59.16.100
    Stream #0:0: Video: rawvideo (BGR[24] / 0x18524742), bgr24(pc, gbr/bt470bg/smpte170m, progressive), 1080x2256, q=2-31, 1461888 kb/s, 25 fps, 25 tbn
      Metadata:
        encoder         : Lavc59.18.100 rawvideo
  frame=    1 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A speed=   0x
  // FFmpeg outputs no more log after this
  </code></pre>