Reliably get PTS values ​​in ffmpeg?

I am trying to write a method that will provide the next frame and timestamp of the presentation when requested. The code currently looks something like this:

while( getNextFrame(image, pts) )
{
    // show current image
    drawImage(currentImage);
    sleep(pts);
    currentImage = image;
}

I have been doing Dranger tutorials until this point, but have stalled on reliably getting PTS values ​​for frames ( http://www.dranger.com/ffmpeg/tutorial05.html ). The returned PTS values ​​are always 0.

It is also get_buffer()deprecated, so now I use the method get_buffer2()to set the global pts value. However, the method is release_bufferalso deprecated, and I cannot find a replacement for it. This makes me believe that the method described in textbooks may not be the best way to solve this problem.

In short, using updated ffmpeg, is the best way to capture frame frame values ​​reliably?

+4
source share
1 answer

Well, you do not provide much information, so I'm going to make some assumptions about your code.

int err, got_frame;
AVFormatContext *avctx;
AVPacket avpkt;
AVFrame *frame;
// You open file, initialize structures here
// You read packet here using av_read_frame()
{
    AVStream *stream = avctx->streams[avpkt.stream_index];
    if ( 0 > ( err = avcodec_decode_video2 ( stream->codec, frame, &got_frame, &avpkt ) && got_frame ) )
    {
        int64_t pts = av_frame_get_best_effort_timestamp ( frame );
        // TODO test for AV_NOPTS_VALUE
        pts = av_rescale_q ( pts,  stream->time_base, AV_TIME_BASE_Q );
        // pts is now in microseconds.
    }
}
+12
source

All Articles