简体   繁体   English

使用FFMPEG API读取特定视频帧

[英]Reading out specific video frame using FFMPEG API

I read frames from video stream in FFMPEG using this loop: 我使用这个循环从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);
    }

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? 所以通过这种方式顺序读取流。我想要的是随机访问帧以便能够读取特定帧(按帧号)。如何完成?

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. 上面的api将在给出时间戳时寻找关键帧。 After seeking you can read the frame. 寻求你可以阅读框架。 Also the tutorial below explain conversion between position and timestamp. 此外,下面的教程还解释了位置和时间戳之间的转换。

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

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. 这些帧称为I帧。 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 寻找前一个I帧
  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. 这是人们经常提到的Dranger教程的更新版本 - 它可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM