4
int64_t timeBase;
timeBase = (int64_t(pavStrm-> time_base.num) * AV_TIME_BASE) / int64_t(pavStrm->time_base.den);
int64_t seekTarget = int64_t(iFrameNumber) * timeBase;
av_seek_frame(fmt_ctx, -1, seekTarget, AVSEEK_FLAG_FRAME);

在这里我想在 iFrameNumebr 之后阅读下一个 5 帧

for(int iCnt = 0; iCnt <= 4; iCnt++)
{
    iRet = av_read_frame(fmt_ctx, &pkt);
        do 
        {
            ret = decode_packet(&got_frame, 0);
            if (ret < 0)
                break;
            pkt.data += ret;
            pkt.size -= ret;

        }while (pkt.size > 0);
    av_free_packet(&pkt);
}

static int decode_packet(int *got_frame, int cached)
{
int ret = 0;
int decoded = pkt.size;
*got_frame = 0;

if (pkt.stream_index == video_stream_idx)
{
    /* decode video frame */
    ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &pkt);
}

当我使用 AVSEEK_FLAG_BACKWARD 时,它返回 5 个数据包和 5 帧前两个是空白但正确。

当我使用 AVSEEK_FLAG_FRAME 时,它返回 5 个数据包和 3 帧,它们不是前 3 帧,它从视频中返回特定帧。

对于任何 iFrameNumber

所以请帮助我如何在有帧号的同时获取帧以及 av_seek_frame() 的 seektarget 3rd 参数的确切值是多少

在将帧转换为 rgb24 格式时我也有问题

4

3 回答 3

19

我认为av_seek_frame()是最常见但难以理解的功能之一,也没有得到足够好的评论。

如果设置了标志AVSEEK_FLAG_FRAME,第三个参数应该是你想要寻找的帧号,你做得很好。

让我们看一个例子来更好地理解av_seek_frame()

假设我有一个 10 帧的视频,fps=10。第一和第五帧是关键帧(I Frameintra frame)。其他是某种格式的 P 帧甚至 B 帧。

0 1 2 3 4 5 6 7 8 9(帧数)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9(时基)

av_seek_frame(fmt_ctx, -1, 2, AVSEEK_FLAG_FRAME);
av_seek_frame(fmt_ctx, -1, 0.15, 0);
// These will seek to the fifth frame. Cause `AVSEEK_FLAG_ANY` is not given. Seeking to the next key frame after third parameter.

av_seek_frame(fmt_ctx, -1, 2, AVSEEK_FLAG_FRAME | AVSEEK_FLAG_ANY);
// This will seek to exactly the third parameter specified. But probably only a frame with no actual meaning. (We can't get a meaningful image if no related I/P/B frames given.)

av_seek_frame(fmt_ctx, -1, 0.15, AVSEEK_FLAG_ANY);
// Seek to 0.2. Nothing interesting as above.

av_seek_frame(fmt_ctx, -1, 0.15, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
// Seek to 0.1. Also nothing interesting.

av_seek_frame(fmt_ctx, -1, 2, AVSEEK_FLAG_FRAME | AVSEEK_FLAG_BACKWARD);
// Got the first frame. Seeking to the nearest key frame before the third parameter.

因此,如果我想获得任意帧,通常AVSEEK_FLAG_BACKWARD先搜索,然后像往常一样解码。然后检查前几个数据包的pts和duration,看看我们是否需要丢弃它们。

于 2016-10-12T04:16:06.280 回答
7
int64_t FrameToPts(AVStream* pavStream, int frame) const
{
return (int64_t(frame) * pavStream->r_frame_rate.den *  pavStream-
>time_base.den) / 
(int64_t(pavStream->r_frame_rate.num) * 
pavStream->time_base.num);
}

iSeekTarget = FrameToPts(m_pAVVideoStream, max(0, lFrame));
iSuccess = av_seek_frame(m_pAVFmtCtx, m_iVideo_Stream_idx, 
iSeekTarget, iSeekFlag);

AVPacket avPacket;
iRet = av_read_frame(m_pAVFmtCtx, &avPacket);
于 2017-09-26T05:46:00.333 回答
1
timeBase = (int64_t(video_stream-> time_base.num) * AV_TIME_BASE) / int64_t(video_stream->time_base.den);
int64_t seekTarget = int64_t(iFrameNumber) * timeBase * (video_stream->time_base.den / video_stream->avg_frame_rate.num);


int iiiret = av_seek_frame(fmt_ctx, -1, seekTarget, AVSEEK_FLAG_FRAME);
于 2016-10-29T06:35:57.520 回答