How to calculate GOP size of a file H264
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
user3677103
Updated on January 13, 2022Comments
-
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 almost 10 yearsThank 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 almost 10 yearsFirstly, 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 almost 10 yearsSVC is Scalable Video Coding. That's right. My goal is get GOP length from H264 stream
-
Fredrik Pihl almost 10 yearsSomeone 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.