title: Different between fragmented mp4 files generated by ffmpeg and by code
body: <p>Currently I have a problem when generating fragmented MP4 file from code using libavformat. My file can be played using VLC, but can't be streamed (via WebSocket) and played (via MediaSource) in (Chrome) browser. (I used <a href="https://github.com/codeaholicguy/mediasource-over-websocket" rel="noreferrer">this</a> to test streaming fragmented MP4 file to browser via WebSocket).</p>
  
  <p>Note: The files below is encoded by Baseline profile, level 4. So you should change the MIME type (in index.html) to const mimeCodec = 'video/mp4; codecs="avc1.42C028"'; to be able to play them.</p>
  
  <p>I checked and found that my generated Mp4 file is a bit different with the file generated by using <code>ffmpeg</code> tool. </p>
  
  <p>Here's what I've done:</p>
  
  <p>I have a .h264 <a href="https://drive.google.com/open?id=0B34Y_mnzP99cMVlPMGpPQ2xfdmM" rel="noreferrer">file</a></p>
  
  <ol>
  <li><p>The first approach, I use ffmpeg to generate the <a href="https://drive.google.com/open?id=0B34Y_mnzP99cT21lWlV2VlpjOVU" rel="noreferrer">fragmented MP4 file</a>.</p>
  
  <pre><code>ffmpeg -i temp.h264 -vcodec copy -f mp4 -movflags empty_moov+default_base_moof+frag_keyframe ffmpeg.mp4
  </code></pre>
  
  <p>The generated file can be played by both Quicktime player and VLC player</p></li>
  <li><p>The second approach, I programmaticaly generate the <a href="https://drive.google.com/open?id=0B34Y_mnzP99cZHVRZjRmbmNoU0U" rel="noreferrer">fragmented Mp4 file by using libavformat</a></p>
  
  <p>First I initialize the context, the <code>codec</code> in code is the <code>AVCodecContext*</code> of the input stream</p>
  
  <pre><code> av_register_all();
   avcodec_register_all();
   int ret;
   AVOutputFormat* fmt = av_guess_format("mp4", 0, 0);
   if(!fmt) {
     return;
   }
  
   AVFormatContext* ctx = avformat_alloc_context();
   // Create AVIO context to capture generated Mp4 contain
   uint8_t *avio_ctx_buffer = NULL;
   size_t avio_ctx_buffer_size = 50000;
   IOOutput buffer = {};
   const size_t bd_buf_size = 50000;
   avio_ctx_buffer = (uint8_t*)av_malloc(avio_ctx_buffer_size);
   buffer.buff = (uint8_t*)av_malloc(bd_buf_size);
  
   buffer.size = bd_buf_size;
   AVIOContext* ioCtx = avio_alloc_context(avio_ctx_buffer, (int)avio_ctx_buffer_size, 1, &amp;buffer, NULL, MP4Formatter::onWritePacket, NULL);
  
   ctx-&gt;oformat = fmt;
   if (ctx-&gt;oformat-&gt;flags &amp; AVFMT_GLOBALHEADER)
     ctx-&gt;flags |= CODEC_FLAG_GLOBAL_HEADER;
   ctx-&gt;pb = ioCtx;
   av_opt_set(ctx, "movflags", "frag_keyframe+empty_moov+default_base_moof", 0);
  
   AVStream* st = avformat_new_stream(ctx, codec-&gt;codec);
   if (!st) {
     return;
   }
  
   st-&gt;id = (ctx-&gt;nb_streams - 1);
   avcodec_parameters_from_context(st-&gt;codecpar, codec);
   st-&gt;time_base = codec-&gt;time_base;
   ioCtx-&gt;seekable = false;
  </code></pre>
  
  <p>Second I implement the onWritePacket callback</p>
  
  <pre><code> int MP4Formatter::onWritePacket(void *opaque, uint8_t* buf, int buf_size) {
     file.write((char*)buf, buf_size);
   }
  </code></pre>
  
  <p>Third, on every packet from h264 file, I write it using <code>av_interleaved_write_frame</code></p>
  
  <pre><code> if (firstFrame) {
     AVDictionary *opts = NULL;
     av_dict_set(&amp;opts, "movflags", "frag_keyframe+empty_moov+default_base_moof", 0);
     if(!parseSPSPPS(data, length)) {
       return;
     }
     cout &lt;&lt; "spslen " &lt;&lt; spslen &lt;&lt; " ppslen " &lt;&lt; ppslen &lt;&lt; endl;
     auto c = st-&gt;codecpar;
     // Extradata contains PPS &amp; SPS for AVCC format
     int extradata_len = 8 + spslen + 1 + 2 + ppslen;
     c-&gt;extradata = (uint8_t*)av_mallocz(extradata_len);
     c-&gt;extradata_size = extradata_len;
     c-&gt;extradata[0] = 0x01;
     c-&gt;extradata[1] = sps[1];
     c-&gt;extradata[2] = sps[2];
     c-&gt;extradata[3] = sps[3];
     c-&gt;extradata[4] = 0xFC | 3;
     c-&gt;extradata[5] = 0xE0 | 1;
     int tmp = spslen;
     c-&gt;extradata[6] = (tmp &gt;&gt; 8) &amp; 0x00ff;
     c-&gt;extradata[7] = tmp &amp; 0x00ff;
     int i = 0;
     for (i=0; i&lt;tmp; i++) {
       c-&gt;extradata[8 + i] = sps[i];
     }
     c-&gt;extradata[8 + tmp] = 0x01;
     int tmp2 = ppslen;
     c-&gt;extradata[8 + tmp + 1] = (tmp2 &gt;&gt; 8) &amp; 0x00ff;
     c-&gt;extradata[8 + tmp + 2] = tmp2 &amp; 0x00ff;
     for (i=0; i&lt;tmp2; i++) {
       c-&gt;extradata[8 + tmp + 3 + i] = pps[i];
     }
     int ret = avformat_write_header(ctx, &amp;opts);
     if(ret &lt; 0) {
       return;
     }
     firstFrame = false;
   }
   AVPacket pkt;
   av_init_packet(&amp;pkt);
   pkt.buf = av_buffer_alloc(length);
   memcpy(pkt.buf-&gt;data, data, length);
   pkt.buf-&gt;size = length;
   pkt.data = pkt.buf-&gt;data;
   pkt.size = pkt.buf-&gt;size;
   pkt.pts = ts;
   pkt.dts = ts;
  
   if (keyFrame) {
     pkt.flags |= AV_PKT_FLAG_KEY;
   }
   else {
     pkt.flags = 0;
   }
  
   pkt.stream_index = st-&gt;id;
  
   av_interleaved_write_frame(ctx, &amp;pkt);
   av_buffer_unref(&amp;pkt.buf);
   av_packet_unref(&amp;pkt);
  </code></pre></li>
  </ol>
  
  <p>Can you guys take a look at my file to see what's wrong?</p>