Skip to content

Instantly share code, notes, and snippets.

@ddennedy
Last active March 29, 2018 22:21
Show Gist options
  • Save ddennedy/dbdf530660627adb036a to your computer and use it in GitHub Desktop.
Save ddennedy/dbdf530660627adb036a to your computer and use it in GitHub Desktop.
display the GOP structure of a video file (requires ffprobe)
#!/bin/sh
startswith() { case $1 in $2*) true;; *) false;; esac; }
tmpfile=$(mktemp)
ffprobe -show_frames "$1" 2> /dev/null > "$tmpfile"
GOP=1
maxGOP=1
Bframes=0
M=0
maxM=0
is_video=false
is_started=false
while read p; do
if startswith "$p" "media_type="; then
if startswith "$p" "media_type=video"; then
is_video=true
else
is_video=false
fi
fi
if $is_video; then
if startswith "$p" "key_frame=0"; then
GOP=$(( GOP + 1 ))
elif startswith "$p" "key_frame=1"; then
is_max_thus_far=
if [ $GOP -gt $maxGOP ]; then
maxGOP=$GOP
is_max_thus_far="*"
fi
if $is_started; then
printf "GOP: M=$maxM, N=$GOP $is_max_thus_far\n"
else
is_started=true
fi
GOP=1
Bframes=0
maxM=0
elif startswith "$p" "pict_type=P"; then
if [ $Bframes -gt $maxM ]; then
maxM=$(( Bframes + 1 ))
fi
Bframes=0
elif startswith "$p" "pict_type=B"; then
Bframes=$(( Bframes + 1 ))
fi
fi
done < "$tmpfile"
rm "$tmpfile"
echo Max. GOP = $maxGOP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment