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:
The stream contains B frames.
The stream has only PTS given by the RTP/RTCP.
Here is the code I did
// ffmpeg
pkt->data = ..;
pkt->size = ..;
pkt->flags = bKeyFrame? AV_PKT_FLAG_KEY : 0;
pkt->dts = AV_NOPTS_VALUE;
pkt->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);
I received a lot of error messages like this: "Application provided invalid, non monotonically increasing dts to muxer ...".
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).
So how do I set correct DTS and PTS before sending to the container?
Update: 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.