Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading out specific video frame using FFMPEG API

Tags:

c++

ffmpeg

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?

like image 332
Michael IV Avatar asked Apr 23 '13 07:04

Michael IV


1 Answers

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

like image 71
praks411 Avatar answered Sep 22 '22 06:09

praks411