How to calculate GOP size of a file H264

28,981

Solution 1

Well, just parsing the bitstream to find the each I-frame is a bit tricky; among other things the encode order might be (or not) different from the display-order. One solution is to use http://www.ffmpeg.org/ffprobe.html from the ffmpeg-suite.

Example:

ffprobe -show_frames input.bin | grep key_frame
key_frame=1
key_frame=0
key_frame=0
key_frame=0
key_frame=0
key_frame=0
...

from the output you can easily calculate the GOP-length

Another solution is to patch the reference implementation found at http://iphome.hhi.de/suehring/tml/

Let me know if you need help with this part :-)

Solution 2

I personally prefer filtering by pict_type:

ffprobe -show_frames input.h264 | grep pict_type

This will show you the frame structure:

pict_type=I
pict_type=P
pict_type=P
pict_type=P
pict_type=P
pict_type=P
...

Solution 3

#!/bin/sh

ffprobe -show_frames $1 > output.txt

GOP=0;

while read p; do
  if [ "$p" = "key_frame=0" ]
  then
    GOP=$((GOP+1))
  fi

if [ "$p" = "key_frame=1" ]
then
  echo $GOP
  GOP=0;
fi

done < output.txt
Share:
28,981
user3677103
Author by

user3677103

Updated on January 13, 2022

Comments

  • user3677103
    user3677103 over 2 years

    I have a h264 file that extract from YUV format using SVC software. Now, I want to caculate size of each GOP in the h264 file. We know that size of GOP is the distance between two nearest I frame. here. Could you suggest to me how to cacluate the GOP size of a given h264 file. It is better when we implement it by C/C++.Thank you

  • user3677103
    user3677103 almost 10 years
    Thank you so much. But in my case, I used SVC to extract YUV to h264 bit stream. It is very difficult to detect which is key frame. Do you have other solution?
  • Fredrik Pihl
    Fredrik Pihl almost 10 years
    Firstly, what is SVC? You encode an YCbCr file into a H.264 bistream and you would like to get the GOP-length from that file or any other H.264 for that matter?
  • user3677103
    user3677103 almost 10 years
    SVC is Scalable Video Coding. That's right. My goal is get GOP length from H264 stream
  • Fredrik Pihl
    Fredrik Pihl almost 10 years
    Someone is actually using SVC? :-) If you don't like the ffmpeg-solution, have a look att the reference sw that I linked to in my answer. The binary ldecod.exe outputs the frametype to stdout. It would also be quite simple to add a counter in the src-code to count the frame-number difference between IDR (I)-frames.