You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pandas按I帧分组统计帧数量并绘制图表的实现方法

基于FFProbe输出帧数据统计相邻I帧间隔帧数及可视化

前置处理流程

首先通过FFProbe获取全量帧信息,调用参数如下:

args = [ffprobe, '-hide_banner', '-loglevel', 'quiet', '-show_frames', '-count_frames', '-of', 'json', '-i', fp]

从返回结果中筛选视频类型帧,构建初始DataFrame:

video_frames = [i for i in input_frames_info["frames"] if i["media_type"].lower() == "video"]
filtered_frames_video = pd.DataFrame.from_dict(video_frames)

I帧分组计数实现

核心逻辑:以每次出现的I帧(兼容pict_type列的I/i两种取值)作为新分组起点,通过累计求和标记分组ID,再按分组统计帧总数。
实现代码:

import pandas as pd
import matplotlib.pyplot as plt

# 标记所有I帧位置,统一做小写转换兼容大小写取值
filtered_frames_video["is_i_frame"] = filtered_frames_video["pict_type"].str.lower() == "i"
# 每遇到一个I帧分组ID加1,为所有帧分配所属GOP分组编号
filtered_frames_video["gop_group_id"] = filtered_frames_video["is_i_frame"].cumsum()
# 按分组统计总帧数,同时保留分组起始I帧的位置、时间戳信息
gop_stat_df = filtered_frames_video.groupby("gop_group_id", as_index=False).agg(
    group_total_frames=("pict_type", "count"),
    start_frame_index=("coded_picture_number", "first"),
    start_pts=("pts", "first")
)

最终生成的gop_stat_df即为目标DataFrame,每一行对应一个以I帧为起点的分组,group_total_frames列存储两个相邻I帧之间的所有帧总数。

提示:上述逻辑会自动将视频末尾最后一个I帧到视频结尾的所有帧计入最后一个分组,如果需要排除末尾不完整GOP,直接删除gop_stat_df最后一行即可。

统计结果可视化

通过柱状图展示每个I帧分组的帧数分布:

plt.figure(figsize=(12, 5))
plt.bar(
    x=gop_stat_df.index,
    height=gop_stat_df["group_total_frames"],
    width=0.7,
    color="#2568a7"
)
plt.xlabel("GOP分组序号(按I帧出现顺序排列)")
plt.ylabel("分组内包含的总帧数")
plt.title("视频相邻I帧间隔帧数分布")
plt.grid(axis="y", linestyle="--", alpha=0.6)
plt.tight_layout()
plt.show()

# 可选:输出GOP帧数的统计特征(均值、最大/最小值、分位数等)
print(gop_stat_df["group_total_frames"].describe())

内容的提问来源于stack exchange,提问作者user9540477

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 23:06:07