14

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
  • 195
  • 2
  • 3
  • 11

6 Answers6

16

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
...
mysteryegg
  • 491
  • 5
  • 11
11

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 :-)

Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • 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? – user3677103 Jun 03 '14 at 17:20
  • 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? – Fredrik Pihl Jun 03 '14 at 18:49
  • SVC is Scalable Video Coding. That's right. My goal is get GOP length from H264 stream – user3677103 Jun 04 '14 at 01:18
  • 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. – Fredrik Pihl Jun 05 '14 at 20:15
4
#!/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
okrunner
  • 3,083
  • 29
  • 22
0

Since every GOP starts with a keyframe you need to count those.
pict_type can be misleading as all types can occur inside GOP.

ffprobe -show_frames video_file.h264  | grep -A 3 "type=video" | grep "key_frame=1" | wc -l
0
ffprobe -i video_file.h264 -show_frames -of flat |grep I
frames.frame.1.pict_type="I"
frames.frame.308.pict_type="I"
frames.frame.805.pict_type="I"
frames.frame.1282.pict_type="I"
frames.frame.1750.pict_type="I"
frames.frame.2221.pict_type="I"
frames.frame.2620.pict_type="I"
frames.frame.3178.pict_type="I"
frames.frame.3693.pict_type="I"

moonkop
  • 29
  • 2
-1

use command like:

ffprobe -show_entries frame=pict_type  mp4_sample.mp4  -of flat | grep I

and you will see the result like:

frames.frame.0.pict_type="I"
frames.frame.384.pict_type="I"
frames.frame.764.pict_type="I"
frames.frame.1027.pict_type="I"
frames.frame.1164.pict_type="I"
frames.frame.1544.pict_type="I"
frames.frame.1944.pict_type="I"
frames.frame.2183.pict_type="I"
frames.frame.2324.pict_type="I"
bluesky
  • 145
  • 7