How to calculate GOP size of a file H264
Asked Answered
K

6

15

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

Kvass answered 3/6, 2014 at 8:51 Comment(0)
D
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 :-)

Disfranchise answered 3/6, 2014 at 15:6 Comment(4)
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?Kvass
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?Disfranchise
SVC is Scalable Video Coding. That's right. My goal is get GOP length from H264 streamKvass
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.Disfranchise
D
17

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
...
Digestive answered 17/5, 2016 at 20:0 Comment(0)
D
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 :-)

Disfranchise answered 3/6, 2014 at 15:6 Comment(4)
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?Kvass
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?Disfranchise
SVC is Scalable Video Coding. That's right. My goal is get GOP length from H264 streamKvass
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.Disfranchise
V
5
#!/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
Vite answered 25/6, 2015 at 22:14 Comment(0)
C
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
Cancer answered 12/1, 2022 at 16:57 Comment(0)
C
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"

Carlyle answered 20/7, 2022 at 10:20 Comment(0)
S
-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"
Safari answered 20/11, 2019 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.