9

I read frames from video stream in FFMPEG using this loop:

while(av_read_frame(pFormatCtx, &packet)>=0) {
        // Is this a packet from the video stream?
        if(packet.stream_index==videoStream) {
            // Decode video frame
            avcodec_decode_video2(pCodecCtx,pFrame,&frameFinished,&packet);

            // Did we get a video frame?
            if(frameFinished) {


                sws_scale(img_convert_context ,pFrame->data,pFrame->linesize,0,
                     pCodecCtx->height, pFrameRGBA->data, pFrameRGBA->linesize);
                printf("%s\n","Frame read finished ");

                                       ExportFrame(pFrameRGBA->data[0]);
                    break;
                }
            }
            // Save the frame to disk

        }
            printf("%s\n","Read next frame ");

        // Free the packet that was allocated by av_read_frame
        av_free_packet(&packet);
    }

So in this way the stream is read sequentially.What I want is to have a random access to the frame to be able reading a specific frame (by frame number).How is it done?

1

2 Answers 2

9

You may want to look

int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp,
                  int flags);

The above api will seek to the keyframe at give timestamp. After seeking you can read the frame. Also the tutorial below explain conversion between position and timestamp.

http://dranger.com/ffmpeg/tutorial07.html

Sign up to request clarification or add additional context in comments.

3 Comments

The time stamp is measured in frames?
Timestamps depend on the time_base of the stream
Yes av_seek_frame seem to be the right thing.I will mark your answer as the correct if you add the whole setup for frame based seek.I found how to do it here stackoverflow.com/q/504792/2065121 (last answer) but just for other people to see how it is done. :)
3

Since most frames in a video depend on previous and next frames, in general, accessing random frames in a video is not straightforward. However, some frames, are encoded independently of any other frames, and occur regularly throughout the video. These frames are known as I-frames. Accessing these frames is straightforward through seeking.

If you want to "randomly" access any frame in the video, then you must:

  1. Seek to the previous I-frame
  2. Read the frames one by one until you get to the frame number that you want

You've already got the code for the second point, so all you need to do is take care of the first point and you're done. Here's an updated version of the Dranger tutorials that people often refer to -- it may be of help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.