Search code examples
ffmpegh.264mpeg-4

GOP structure via FFmpeg


I have two questions regarding the Group-of-Pictures (GOP) in MPEG4 Video (both for MPEG4 Part2 and H.264):

  1. How can I extract the GOP structure and size of a video sequence using FFmpeg? I know that the av_get_picture_type_char function of the AVPicture struct yields picture types for each frame, but I wonder if there is a more direct method to obtain the GOP information?

  2. How can I detect whether the sequence has open GOPs or closed GOPs, i.e. whether B frames from one GOP are allowed to refer to the I and P frames in an adjacent GOP?


Solution

  • None of this is directly exported in ffmpeg, so you'll have to figure these things out yourself. So the short answer for 1 is "no". If you parse the frames (you don't need to decode them) and get the frame type, you should be able to identify location in file (offset, in bytes) in AVPacket.pos. For each I-frame in the sequence, search packets after it and note AVPacket.pos and AVPacket.pts. If B(pos) > I(pos) but B(pts) < I(pts), you have open GOP, else closed GOP. If you want to be more precise, find some way to export the POC from the h264 parser, which is directly proportional to the timestamp.

    The parsing loop would just be av_read_frame().