ffmpegfluent-ffmpeg

How to read any frame while having frame number using ffmpeg av_seek_frame()


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);

here I want to read next 5 frame after iFrameNumebr

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);
}

when i am using AVSEEK_FLAG_BACKWARD its return 5 packet and 5 frame first two is blank but correct.

when i am using AVSEEK_FLAG_FRAME its return 5 packet and 3 frame which are not first 3 frame its return specific frame from video.

for any iFrameNumber

so please help me how to get frame while having frame number and what is exact value of seektarget 3rd param of av_seek_frame()

also I have problem while converting frame to rgb24 format


Solution

  • 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);