title: Android FFmpeg - Mux encoded h264 frames from MediaCodec
body: <p>I'm trying to capture h264 frames from Android camera (encoded by MediaCodec) and pass them to an FFmpeg process running on the same device.</p>
  
  <p>I currently do it by writing the encoded byte arrays I receive from the MediaCodec to a file called out.h264. </p>
  
  <p>Like so :</p>
  
  <pre><code>    FileOutputStream fosVideo = new ...
  
      ...
  
      // encoder callback
      @Override
      public void onVideoData(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) {
          fosVideo.write(h264Buffer);
      }
  </code></pre>
  
  <p>While the h264 file is being written to is I start the FFmpeg process and provide the h264 file as input.</p>
  
  <pre><code>ffmpeg -re -i out.h264 -c:v copy -r 30 -loglevel 48 a.mp4
  </code></pre>
  
  <p>I also tried </p>
  
  <pre><code>ffmpeg -re -framerate 25 -i out.h264 -c:v copy -r 30 -loglevel 48 a.mp4
  </code></pre>
  
  <p>The FFmpeg process runs anywhere between 10 seconds to a few minutes and then stops abruptly with:</p>
  
  <pre><code>frame=  330 fps= 29 q=31.0 size=     512kB time=00:00:10.98 bitrate= 381.8kbits/s dup=55 drop=0 speed=0.972x    
  [h264 @ 0xf1863800] nal_unit_type: 1(Coded slice of a non-IDR picture), nal_ref_idc: 1
  [h264 @ 0xf1863b80] nal_unit_type: 1(Coded slice of a non-IDR picture), nal_ref_idc: 1
  [h264 @ 0xf1863f00] nal_unit_type: 1(Coded slice of a non-IDR picture), nal_ref_idc: 1
  *** 1 dup!
  [h264 @ 0xf1864280] nal_unit_type: 1(Coded slice of a non-IDR picture), nal_ref_idc: 1
  Clipping frame in rate conversion by 0.199989
  [h264 @ 0xf1864600] nal_unit_type: 1(Coded slice of a non-IDR picture), nal_ref_idc: 1
  [h264 @ 0xf1862a00] nal_unit_type: 1(Coded slice of a non-IDR picture), nal_ref_idc: 1
  [h264 @ 0xf1862d80] nal_unit_type: 1(Coded slice of a non-IDR picture), nal_ref_idc: 1
  [h264 @ 0xf1863100] nal_unit_type: 1(Coded slice of a non-IDR picture), nal_ref_idc: 1
  *** 1 dup!
  Clipping frame in rate conversion by 0.199989
  *** 1 dup!
  frame=  347 fps= 29 q=31.0 size=     768kB time=00:00:11.53 bitrate= 545.5kbits/s dup=58 drop=0 speed=0.974x    
  Clipping frame in rate conversion by 0.199989
  [out_0_0 @ 0xf182e1e0] EOF on sink link out_0_0:default.
  No more output streams to write to, finishing.
  frame=  349 fps= 29 q=24.8 Lsize=     920kB time=00:00:17.68 bitrate= 426.1kbits/s dup=58 drop=0 speed=1.48x    
  video:631kB audio:282kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.732886%
  Input file #0 (/storage/emulated/0/MOVIES/out.h264):
  Input stream #0:0 (video): 291 packets read (6065016 bytes); 291 frames decoded; 
  Total: 291 packets (6065016 bytes) demuxed
  Input file #1 (/storage/emulated/0/MOVIES/out.aac):
  Input stream #1:0 (audio): 830 packets read (289119 bytes); 
  Total: 830 packets (289119 bytes) demuxed
  Output file #0 (/storage/emulated/0/hls/a.mp4):
  Output stream #0:0 (video): 349 frames encoded; 349 packets muxed (645952 bytes); 
  Output stream #0:1 (audio): 830 packets muxed (289119 bytes); 
  Total: 1179 packets (935071 bytes) muxed
  291 frames successfully decoded, 0 decoding errors
  </code></pre>
  
  <p>Even though the out.h264 file is still being recorded into.
  It is as if the ffmpeg process thinks that the file has ended.</p>
  
  <p>Any idea what could it be?</p>