简体   繁体   English

如何计算文件 H264 的 GOP 大小

[英]How to calculate GOP size of a file H264

I have a h264 file that extract from YUV format using SVC software.我有一个使用 SVC 软件从 YUV 格式中提取的 h264 文件。 Now, I want to caculate size of each GOP in the h264 file.现在,我想计算 h264 文件中每个 GOP 的大小。 We know that size of GOP is the distance between two nearest I frame.我们知道 GOP 的大小是两个最近的 I 帧之间的距离。 here .在这里 Could you suggest to me how to cacluate the GOP size of a given h264 file.您能否向我建议如何计算给定 h264 文件的 GOP 大小。 It is better when we implement it by C/C++.Thank you最好用C/C++实现。谢谢

I personally prefer filtering by pict_type:我个人更喜欢按 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
...

Well, just parsing the bitstream to find the each I-frame is a bit tricky;好吧,仅仅解析比特流以找到每个 I 帧有点棘手; 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.一种解决方案是使用 ffmpeg-suite 中的http://www.ffmpeg.org/ffprobe.html

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从输出中,您可以轻松计算 GOP 长度

Another solution is to patch the reference implementation found at http://iphome.hhi.de/suehring/tml/另一种解决方案是修补http://iphome.hhi.de/suehring/tml/ 上的参考实现

Let me know if you need help with this part :-)如果您需要这部分的帮助,请告诉我:-)

#!/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

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"

Since every GOP starts with a keyframe you need to count those.由于每个 GOP 都以关键帧开始,因此您需要计算这些关键帧。
pict_type can be misleading as all types can occur inside GOP. pict_type可能会产生误导,因为所有类型都可能出现在 GOP 内部。

ffprobe -show_frames video_file.h264  | grep -A 3 "type=video" | grep "key_frame=1" | wc -l
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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM