'Compute accurately the pts and dts of a video packet

Is there a way to compute the encoded AVPacket.pts and AVPacket.dts? If the encoded packet doesn't contain a duration?

I tried computing it by starting the timestamp at 0, then increase that timestamp with the computed duration of the video frame. My computation for the duration below:

if (video_ts < 0) video_ts = 0;
else 
{
    video_ts += (int64_t)last_duration;
}
compressed_video.pts = video_ts;
compressed_video.dts = video_ts;

last_duration = ((compressed_video.size * 8) / (double)out_videocc->bit_rate) * 1000;

It worked slightly but it is not exact. The playback stutters



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source