可靠地获得ffmpeg中的PTS值?

kar*_*rl_ 4 video ffmpeg pts libavcodec libavformat

我正在尝试编写一个方法,在查询时提供下一帧和显示时间戳.代码目前看起来像这样:

while( getNextFrame(image, pts) )
{
    // show current image
    drawImage(currentImage);
    sleep(pts);
    currentImage = image;
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我一直在关注Dranger教程,但是为了可靠地获取帧的PTS值而停滞不前(http://www.dranger.com/ffmpeg/tutorial05.html).返回的PTS值始终为0.

此外,get_buffer()已被弃用,所以我现在使用该get_buffer2()方法设置全局pts值.但是,该release_buffer方法也已被弃用,我似乎无法找到它的替代品.这让我相信教程中列出的方法可能不再是完成此任务的最佳方法.

简而言之,使用最新的ffmpeg,可靠地获取帧pts值的最佳方法是什么?

sza*_*ary 12

好的,你没有提供太多信息,所以我将对你的代码做一些假设.

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.
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 替代方法是直接访问 AVFrame->best_effort_timestamp 字段。 (3认同)