Skip to content

Instantly share code, notes, and snippets.

@jessetan
Last active October 25, 2023 13:43
Show Gist options
  • Save jessetan/60842710511de3710886 to your computer and use it in GitHub Desktop.
Save jessetan/60842710511de3710886 to your computer and use it in GitHub Desktop.
Calculates average bitrate of a video file by looking at size of each frame. Uses ffprobe, sed and awk
#!/bin/bash
# Calculates average bitrate of a video file by looking at size of each frame. Uses ffprobe, sed and awk
if [ -z "$1" ]; then
echo "Error: No input video file specified"
exit 1
fi
echo "Estimating bitrate statistics for $1..." ; ffprobe -show_frames -loglevel error $1 | \
grep -A 20 media_type=video | \
grep "pkt_size\|pkt_duration_time"| \
sed 'N;s/\n/ /' | \
sed 's/pkt_duration_time=\([0-9\.]*\) pkt_size=\([0-9\.]*\)/\1 \2/' | \
awk 'BEGIN {
lines=0;
totalBits=0;
# total nr of frames for logging, not used for calculation
totalFrameTime=0
}
{
# bytes per frame ($2) * 8 = bits per frame
# bits per frame / frame length ($1) = bits per second
totalBits += $2 * 8 / $1;
totalFrameTime += $1;
lines++
}
END {
printf("Total Mbits: %8.2f, average frametime: %3.3f ms\nAverage bitrate: %4.3f Mbit/s\n", totalBits/1000000, totalFrameTime/lines, totalBits/lines/1000000)
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment