使用FFMPEG API读取特定视频帧

Mic*_* IV 6 c++ ffmpeg

我使用这个循环从FFMPEG中的视频流中读取帧:

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);
    }
Run Code Online (Sandbox Code Playgroud)

所以通过这种方式顺序读取流.我想要的是随机访问帧以便能够读取特定帧(按帧号).如何完成?

pra*_*411 6

你可能想看

int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp,
                  int flags);
Run Code Online (Sandbox Code Playgroud)

上面的api将在给出时间戳时寻找关键帧.寻求你可以阅读框架.此外,下面的教程还解释了位置和时间戳之间的转换.

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