title: Muxing AVPackets into mp4 file
body: <p>I am developing a tool which receives a h.264 live stream from the network (the sender is a hardware encoder), buffers the last x-minutes and creates a video file of the last x-minutes if triggered.</p>
  
  <p>My tool is able to receive the live stream and also to buffer AVPackets the  with boost::serialization. Furthermore I am able to load the buffered AVPackets.</p>
  
  <p>But when I try to create/mux a video file (mp4) with the loaded AVPackets there occurs an error in the mp4-file or mp4-header.
  I am able to create a mp4-file and also to write Packets to the file with av_interleaved_write_frame(). The mp4-file has also a plausible size. So it seams that the AVPackets were written to the file.</p>
  
  <p>But when I try to open the mp4-file with a player like vlc, the video doesn't play. The player provides an error like: the header isn't correct and doesn't play any frame. But I write the header with the writeHeader() function to the AVFormatContext.</p>
  
  <p>I guess that the reason is that the AVFormatContext isn't correctly initialized but with the example of ffmpeg/libav (muxing-examples) I can't solve the problem.</p>
  
  <p><strong>Has any one a solution (example code) or an other example how to correctly initialize the AVFormatContext?</strong></p>
  
  <p>Thanks for your help.
  Olgen</p>
  
  <pre><code>    avcodec_init();
      avcodec_register_all();
      av_register_all();
      av_log_set_level(AV_LOG_DEBUG);
  
      char *outputFilename = "/tmp/output.mp4";
  
      // define AVOutputFormat
      AVOutputFormat *outputFormat = NULL;
      outputFormat = av_guess_format("mp4", NULL, NULL);
      if (!outputFormat) {
          fprintf(stderr, "Could not find suitable output format\n");
          return 1;
      }
  
      // define AVFormatContext
      AVFormatContext *formatContext = NULL;
      // lallocate the output media context
      std::cout &lt;&lt; "alloc" &lt;&lt; std::endl;
      formatContext = avformat_alloc_context();
  //  avformat_alloc_output_context(&amp;formatContext, outputFormat, NULL, NULL);
      if (!formatContext) {
          fprintf(stderr, "Memory error\n");
          return 1;
      }
      std::cout &lt;&lt; "stream" &lt;&lt; std::endl;
      AVStream * video_st = av_new_stream(formatContext, 0);
      AVCodec * codec = NULL;
      avcodec_get_context_defaults3(video_st-&gt;codec, codec);
      video_st-&gt;codec-&gt;coder_type = AVMEDIA_TYPE_VIDEO;
  
      video_st-&gt;codec-&gt;flags = fc-&gt;streams[0]-&gt;codec-&gt;flags;
      video_st-&gt;codec-&gt;sample_aspect_ratio = fc-&gt;streams[0]-&gt;codec-&gt;sample_aspect_ratio;
      video_st-&gt;disposition = fc-&gt;streams[0]-&gt;disposition;
      video_st-&gt;codec-&gt;codec_tag = fc-&gt;streams[0]-&gt;codec-&gt;codec_tag;
      video_st-&gt;codec-&gt;bits_per_raw_sample = fc-&gt;streams[0]-&gt;codec-&gt;bits_per_raw_sample;
      video_st-&gt;codec-&gt;chroma_sample_location = fc-&gt;streams[0]-&gt;codec-&gt;chroma_sample_location;
      video_st-&gt;codec-&gt;codec_id = fc-&gt;streams[0]-&gt;codec-&gt;codec_id;
      video_st-&gt;codec-&gt;codec_tag = fc-&gt;streams[0]-&gt;codec-&gt;codec_tag;
      video_st-&gt;codec-&gt;time_base = fc-&gt;streams[0]-&gt;codec-&gt;time_base;
      video_st-&gt;codec-&gt;extradata = fc-&gt;streams[0]-&gt;codec-&gt;extradata;
      video_st-&gt;codec-&gt;extradata_size = fc-&gt;streams[0]-&gt;codec-&gt;extradata_size;
      video_st-&gt;codec-&gt;pix_fmt = fc-&gt;streams[0]-&gt;codec-&gt;pix_fmt;
      video_st-&gt;codec-&gt;width = fc-&gt;streams[0]-&gt;codec-&gt;width;
      video_st-&gt;codec-&gt;height = fc-&gt;streams[0]-&gt;codec-&gt;height;
      video_st-&gt;codec-&gt;sample_aspect_ratio = fc-&gt;streams[0]-&gt;codec-&gt;sample_aspect_ratio;
  
  
      std::cout &lt;&lt; "file" &lt;&lt; std::endl;
      avio_open(&amp;formatContext-&gt;pb, outputFilename, AVIO_FLAG_WRITE);
      std::cout &lt;&lt; "header" &lt;&lt; std::endl;
      avformat_write_header(formatContext, NULL);
  //  av_write_header(formatContext);
  
  // loop to write AVPackets with av_write_frame
  </code></pre>
  
  <p>The programm crashes after the avformat_write_header(formatContext, NULL) with a memory error. I also tried avformat_alloc_output_context() instead of avformat_alloc_output_context2() but both functions don't work. So I used avformat_alloc_context() (see above)</p>
  
  <p><strong>I think there is an alloction problem but why I can't use avformat_alloc_output_context2 or avformat_alloc_output_context. Could somebody figure out my problem here?</strong></p>