title: Fix 3GP file after streaming from Android Media Recorder
body: <p>I am trying to stream video from android camera through local unix socket and write file from stream to sdcard. Everything works fine, except file is not playable with any player. It's because Android not filling some gaps in the file because socket is not seekable.  As I understand I need to make some modifications after video stream is over. I read several articles <a href="https://groups.google.com/forum/#!topic/android-developers/FiZZSXEFhd0%5B1-25%5D" rel="nofollow noreferrer">here</a>, <a href="http://www.mattakis.com/blog/kisg/20090708/broadcasting-video-with-android-without-writing-to-the-file-system" rel="nofollow noreferrer">here</a> and <a href="https://stackoverflow.com/questions/11502609/fix-3gp-header-for-a-streamed-video-through-socket">here</a>, but none of them helped me. I am playing with hex editor to learn how to do it manually, so afterwards it will be trivial to do the same in the Android code.</p>
  
  <p>Here is sample file that saved from stream: <a href="https://dl.dropbox.com/u/17510473/sample_not_playable.3gp" rel="nofollow noreferrer">https://dl.dropbox.com/u/17510473/sample_not_playable.3gp</a></p>
  
  <p>Can anyone fix to make it playable and tell how he done it?</p>
  
  <p><strong>EDIT:</strong> I erase header of the 3gp file and write new one as follows:</p>
  
  <pre><code>00 00 00 18 66 74 79 70 33 67 70 34 00 00 03 00 33 67 70 34 33 67 70 36 00 00 00 00
  </code></pre>
  
  <p>Then I find starting location of mdat and moov atoms with following command:</p>
  
  <pre><code>grep -aobE "ftyp|mdat|moov" sample_not_playable.3gp
  </code></pre>
  
  <p>And it gives me following output:</p>
  
  <pre><code>4:ftyp
  28:mdat
  1414676:moov
  </code></pre>
  
  <p>Then make <code>1414676 - 28 = 1,414,648 = 0x1595F8</code></p>
  
  <p>Then I write 0x1595F8 as 25-28 bytes, just prior mdat atom. So my header now looks like this:</p>
  
  <pre><code>00 00 00 18 66 74 79 70 33 67 70 34 00 00 03 00 33 67 70 34 33 67 70 36 00 15 95 F8
  </code></pre>
  
  <p>And when I try to play it with mplayer I get some damaged video and audio output. Here's some part from mplayer output:</p>
  
  <pre><code>[amrwb @ 0x7f72ad652380]Frame too small (33 bytes). Truncated file?
  [amrwb @ 0x7f72ad652380]Encountered a bad or corrupted frame
  [amrwb @ 0x7f72ad652380]Encountered a bad or corrupted frame
  [amrwb @ 0x7f72ad652380]Frame too small (33 bytes). Truncated file?
  [amrwb @ 0x7f72ad652380]Encountered a bad or corrupted frame
  [amrwb @ 0x7f72ad652380]Encountered a bad or corrupted frame
  [amrwb @ 0x7f72ad652380]Encountered a bad or corrupted frame
  A:  11.0 V:   1.4 A-V:  9.650 ct:  0.023   0/  0 10%  1%  1.6% 0 0                                                        
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f72adeafc40]stream 1, offset 0x15e62b: partial file
  [h263 @ 0x7f72ad652380]Bad picture start code
  [h263 @ 0x7f72ad652380]header damaged
  Error while decoding frame!
  [h263 @ 0x7f72ad652380]Bad picture start code
  [h263 @ 0x7f72ad652380]header damaged
  Error while decoding frame!
  [h263 @ 0x7f72ad652380]Bad picture start code
  [h263 @ 0x7f72ad652380]header damaged
  Error while decoding frame!
  A:  11.1 V:   1.5 A-V:  9.558 ct:  0.027   0/  0  9%  1%  1.4% 0 0                                                        
  [h263 @ 0x7f72ad652380]Bad picture start code
  [h263 @ 0x7f72ad652380]header damaged
  Error while decoding frame!
  </code></pre>
  
  <p>What I am doing wrong?</p>