[Libav-user] Grabbing individual frames from a movie

Info || Non-Lethal Applications info at non-lethal-applications.com
Wed Apr 9 23:51:44 CEST 2014


I’m using libav for my own playback engine and it works like a charm.

But I have trouble with seeking ...
I’d like to pull individual frames from a stream at a given index.
To do that I’m using the following code:

int64_t seekTarget = theFrameIndex;
int res = avformat_seek_file(pFormatCtx, self.videoStreamIndex, seekTarget, seekTarget, seekTarget, AVSEEK_FLAG_FRAME);
. . . 
while (av_read_frame(pFormatCtx, &packet)>=0)
{
    int idx = packet.stream_index;
        
    if(idx != self.videoStreamIndex)
    {
        av_free_packet(&packet);
        continue;
    }

    int len = avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
    if (len < 0)
    {
        av_free_packet(&packet);
        break;
    }     

    if(!frameFinished)
    {
        av_free_packet(&packet);
        continue;
    }

    presentationTimeStamp = av_frame_get_best_effort_timestamp(pFrame);
    
    AVRational timeBase = pFormatCtx->streams[self.videoStreamIndex]->time_base;
    float ptsInSeconds = presentationTimeStamp * av_q2d(timeBase);
    int frameIndex = (int)(ptsInSeconds * self.videoFrameRate);
. . .
}

And that’s how I am pulling the duration that is used to seek:

float mediaDurationInSecs = pFormatCtx->duration / 1000000.0f;
int durationInFrames = (int)(mediaDurationInSecs * self.videoFrameRate);

The slider used for seeking returns a value from 0-1 which is then multiplied by the duration to get the frame index.

I’m not getting any errors but the frames returned don’t belong to the frame index provided.
It looks like the AVSEEK_FLAG_FRAME doesn’t do what I’d expect.

I would convert the frame index to stream time units but I don’t know why.
I’d need a frame duration and I don’t have one at that point…

I’d appreciate any hint!!
Thanks!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20140409/e8d5c1ca/attachment.html>


More information about the Libav-user mailing list