title: How to write a video stream containing B-frame and no DTS to a MP4 container?
body: <p>I want to save a h264 video stream received from a RTSP source to a MP4 container.
  Not like other questions asked on SO, here the challenges I face are: </p>
  
  <ul>
  <li><p>The stream contains B frames. </p></li>
  <li><p>The stream has only PTS given by the RTP/RTCP.</p></li>
  </ul>
  
  <p>Here is the code I did</p>
  
  <pre><code>//  ffmpeg
      pkt-&gt;data = ..;
      pkt-&gt;size = ..;
      pkt-&gt;flags = bKeyFrame? AV_PKT_FLAG_KEY : 0;    
      pkt-&gt;dts = AV_NOPTS_VALUE;
      pkt-&gt;pts = PTS;
  
      // PTS is based on epoch microseconds so I ignored re-scaling.
      //av_packet_rescale_ts(pkt, { 1, AV_TIME_BASE }, muxTimebase);
  
      auto ret = av_interleaved_write_frame(m_pAVFormatCtx, pkt);
  </code></pre>
  
  <p>I received a lot of error messages like this: 
      "Application provided invalid, non monotonically increasing dts to muxer ...".</p>
  
  <p>Result: the mp4 file is playable via VLC but the FPS is just a half of the original FPS and the video duration is incorrect (VLC shows a weird number).</p>
  
  <p>So how do I set correct DTS and PTS before sending to the container?</p>
  
  <p><strong>Update:</strong>
  I have tried some changes, though not successfully yet, I found that the reason of the frame rate drop is due to the muxer discards frames having incorrect DTS.
  Additionally, if I set start of PTS and DTS value too big, some players like VLC has to delay some time before showing video.</p>