Last active
October 25, 2023 13:43
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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