Skip to content

Instantly share code, notes, and snippets.

@hekmon
Created July 25, 2024 18:03
Show Gist options
  • Save hekmon/af9fb958e33c2c787f508e4cc6382419 to your computer and use it in GitHub Desktop.
Save hekmon/af9fb958e33c2c787f508e4cc6382419 to your computer and use it in GitHub Desktop.
Count GOP (Group Of Pictures) and their frames (I, P and B frames) within a video file
#!/usr/bin/env bash
set -e
GOP=0
iframes=0
totiframes=0
pframes=0
totpframes=0
bframes=0
totbframes=0
while IFS= read -r line; do
if echo "$line" | grep -q 'frame,I'; then
if [ $GOP -ne 0 ]; then
echo "GOP #${GOP} has $(( $iframes + $pframes + $bframes )) frames: $iframes I frame(s), $pframes P frames and $bframes B frames"
fi
totiframes=$(( $totiframes + $iframes ))
iframes=1
totpframes=$(( $totpframes + $pframes ))
pframes=0
totbframes=$(( $totbframes + $bframes ))
bframes=0
GOP=$(( $GOP + 1 ))
continue
fi
if echo "$line" | grep -q 'frame,P'; then
pframes=$(( $pframes + 1 ))
continue
fi
if echo "$line" | grep -q 'frame,B'; then
bframes=$(( $bframes + 1 ))
continue
fi
echo "unexpected line: $line"
done < <(ffprobe -loglevel error -threads $(nproc) -select_streams v -show_frames -show_entries 'frame=pict_type' -of csv "$1")
echo "GOP #$GOP has $(( $iframes + $pframes + $bframes )) frames: $iframes I frame(s), $pframes P frames and $bframes B frames"
totiframes=$(( $totiframes + $iframes ))
totpframes=$(( $totpframes + $pframes ))
totbframes=$(( $totbframes + $bframes ))
echo
echo "Analysis done: $GOP Groups Of Pictures with $(( $totiframes + $totpframes + $totbframes )) total frames ($totiframes I frames, $totpframes P frames and $totbframes B frames)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment