title: C++ FFmpeg create mp4 file
body: <p>I'm trying to create mp4 video file with FFmpeg and C++, but in result I receive broken file (windows player shows "Can't play ... 0xc00d36c4"). If I create .h264 file, it can be played with 'ffplay' and successfully converted to mp4 via CL.</p>
  
  <p>My code:</p>
  
  <pre><code>int main() {
      char *filename = "tmp.mp4";
      AVOutputFormat *fmt;
      AVFormatContext *fctx;
      AVCodecContext *cctx;
      AVStream *st;
  
      av_register_all();
      avcodec_register_all();
  
      //auto detect the output format from the name
      fmt = av_guess_format(NULL, filename, NULL);
      if (!fmt) {
          cout &lt;&lt; "Error av_guess_format()" &lt;&lt; endl; system("pause"); exit(1);
      }
  
      if (avformat_alloc_output_context2(&amp;fctx, fmt, NULL, filename) &lt; 0) {
          cout &lt;&lt; "Error avformat_alloc_output_context2()" &lt;&lt; endl; system("pause"); exit(1);
      }
  
  
      //stream creation + parameters
      st = avformat_new_stream(fctx, 0);
      if (!st) {
          cout &lt;&lt; "Error avformat_new_stream()" &lt;&lt; endl; system("pause"); exit(1);
      }
  
      st-&gt;codecpar-&gt;codec_id = fmt-&gt;video_codec;
      st-&gt;codecpar-&gt;codec_type = AVMEDIA_TYPE_VIDEO;
      st-&gt;codecpar-&gt;width = 352;
      st-&gt;codecpar-&gt;height = 288;
      st-&gt;time_base.num = 1;
      st-&gt;time_base.den = 25;
  
      AVCodec *pCodec = avcodec_find_encoder(st-&gt;codecpar-&gt;codec_id);
      if (!pCodec) {
          cout &lt;&lt; "Error avcodec_find_encoder()" &lt;&lt; endl; system("pause"); exit(1);
      }
  
      cctx = avcodec_alloc_context3(pCodec);
      if (!cctx) {
          cout &lt;&lt; "Error avcodec_alloc_context3()" &lt;&lt; endl; system("pause"); exit(1);
      }
  
      avcodec_parameters_to_context(cctx, st-&gt;codecpar);
      cctx-&gt;bit_rate = 400000;
      cctx-&gt;width = 352;
      cctx-&gt;height = 288;
      cctx-&gt;time_base.num = 1;
      cctx-&gt;time_base.den = 25;
      cctx-&gt;gop_size = 12;
      cctx-&gt;pix_fmt = AV_PIX_FMT_YUV420P;
      if (st-&gt;codecpar-&gt;codec_id == AV_CODEC_ID_H264) {
          av_opt_set(cctx-&gt;priv_data, "preset", "ultrafast", 0);
      }
      if (fctx-&gt;oformat-&gt;flags &amp; AVFMT_GLOBALHEADER) {
          cctx-&gt;flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
      }
      avcodec_parameters_from_context(st-&gt;codecpar, cctx);
  
      av_dump_format(fctx, 0, filename, 1);
  
      //OPEN FILE + WRITE HEADER
      if (avcodec_open2(cctx, pCodec, NULL) &lt; 0) {
          cout &lt;&lt; "Error avcodec_open2()" &lt;&lt; endl; system("pause"); exit(1);
      }
      if (!(fmt-&gt;flags &amp; AVFMT_NOFILE)) {
          if (avio_open(&amp;fctx-&gt;pb, filename, AVIO_FLAG_WRITE) &lt; 0) {
              cout &lt;&lt; "Error avio_open()" &lt;&lt; endl; system("pause"); exit(1);
          }
      }
      if (avformat_write_header(fctx, NULL) &lt; 0) {
          cout &lt;&lt; "Error avformat_write_header()" &lt;&lt; endl; system("pause"); exit(1);
      }
  
  
      //CREATE DUMMY VIDEO
      AVFrame *frame = av_frame_alloc();
      frame-&gt;format = cctx-&gt;pix_fmt;
      frame-&gt;width = cctx-&gt;width;
      frame-&gt;height = cctx-&gt;height;
      av_image_alloc(frame-&gt;data, frame-&gt;linesize, cctx-&gt;width, cctx-&gt;height, cctx-&gt;pix_fmt, 32);
  
      AVPacket pkt;
      double video_pts = 0;
      for (int i = 0; i &lt; 50; i++) {
          video_pts = (double)cctx-&gt;time_base.num / cctx-&gt;time_base.den * 90 * i;
  
          for (int y = 0; y &lt; cctx-&gt;height; y++) {
              for (int x = 0; x &lt; cctx-&gt;width; x++) {
                  frame-&gt;data[0][y * frame-&gt;linesize[0] + x] = x + y + i * 3;
                  if (y &lt; cctx-&gt;height / 2 &amp;&amp; x &lt; cctx-&gt;width / 2) {
                      /* Cb and Cr */
                      frame-&gt;data[1][y * frame-&gt;linesize[1] + x] = 128 + y + i * 2;
                      frame-&gt;data[2][y * frame-&gt;linesize[2] + x] = 64 + x + i * 5;
                  }
              }
          }
  
          av_init_packet(&amp;pkt);
          pkt.flags |= AV_PKT_FLAG_KEY;
          pkt.pts = frame-&gt;pts = video_pts;
          pkt.data = NULL;
          pkt.size = 0;
          pkt.stream_index = st-&gt;index;
  
          if (avcodec_send_frame(cctx, frame) &lt; 0) {
              cout &lt;&lt; "Error avcodec_send_frame()" &lt;&lt; endl; system("pause"); exit(1);
          }
          if (avcodec_receive_packet(cctx, &amp;pkt) == 0) {
              //cout &lt;&lt; "Write frame " &lt;&lt; to_string((int) pkt.pts) &lt;&lt; endl;
              av_interleaved_write_frame(fctx, &amp;pkt);
              av_packet_unref(&amp;pkt);
          }
      }
  
      //DELAYED FRAMES
      for (;;) {
          avcodec_send_frame(cctx, NULL);
          if (avcodec_receive_packet(cctx, &amp;pkt) == 0) {
              //cout &lt;&lt; "-Write frame " &lt;&lt; to_string((int)pkt.pts) &lt;&lt; endl;
              av_interleaved_write_frame(fctx, &amp;pkt);
              av_packet_unref(&amp;pkt);
          }
          else {
              break;
          }
      }
  
      //FINISH
      av_write_trailer(fctx);
      if (!(fmt-&gt;flags &amp; AVFMT_NOFILE)) {
          if (avio_close(fctx-&gt;pb) &lt; 0) {
              cout &lt;&lt; "Error avio_close()" &lt;&lt; endl; system("pause"); exit(1);
          }
      }
      av_frame_free(&amp;frame);
      avcodec_free_context(&amp;cctx);
      avformat_free_context(fctx);
  
      system("pause");
      return 0;
  }
  </code></pre>
  
  <p>Output of program:</p>
  
  <pre><code>Output #0, mp4, to 'tmp.mp4':
      Stream #0:0: Video: h264, yuv420p, 352x288, q=2-31, 400 kb/s, 25 tbn
  [libx264 @ 0000021c4a995ba0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
  [libx264 @ 0000021c4a995ba0] profile Constrained Baseline, level 2.0
  [libx264 @ 0000021c4a995ba0] 264 - core 152 r2851 ba24899 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=12 keyint_min=1 scenecut=0 intra_refresh=0 rc=abr mbtree=0 bitrate=400 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
  [libx264 @ 0000021c4a995ba0] frame I:5     Avg QP: 7.03  size:  9318
  [libx264 @ 0000021c4a995ba0] frame P:45    Avg QP: 4.53  size:  4258
  [libx264 @ 0000021c4a995ba0] mb I  I16..4: 100.0%  0.0%  0.0%
  [libx264 @ 0000021c4a995ba0] mb P  I16..4:  0.0%  0.0%  0.0%  P16..4: 100.0%  0.0%  0.0%  0.0%  0.0%    skip: 0.0%
  [libx264 @ 0000021c4a995ba0] final ratefactor: 9.11
  [libx264 @ 0000021c4a995ba0] coded y,uvDC,uvAC intra: 18.9% 21.8% 14.5% inter: 7.8% 100.0% 15.5%
  [libx264 @ 0000021c4a995ba0] i16 v,h,dc,p:  4%  5%  5% 86%
  [libx264 @ 0000021c4a995ba0] i8c dc,h,v,p:  2%  9%  6% 82%
  [libx264 @ 0000021c4a995ba0] kb/s:264.68
  </code></pre>
  
  <p>If I will try to play mp4 file with 'ffplay' it prints:</p>
  
  <pre><code>[mov,mp4,m4a,3gp,3g2,mj2 @ 00000000026bf900] Could not find codec parameters for stream 0 (Video: h264 (avc1 / 0x31637661), none, 352x288, 138953 kb/s): unspecified pixel format
  [h264 @ 00000000006c6ae0] non-existing PPS 0 referenced
  [h264 @ 00000000006c6ae0] decode_slice_header error
  [h264 @ 00000000006c6ae0] no frame!
  </code></pre>
  
  <p>I've spent a lot of time without success of finding issue, what could be the reason of it?</p>
  
  <p>Thank for help!</p>