Skip to content

Instantly share code, notes, and snippets.

@khaosx
Created January 14, 2020 14:24
Show Gist options
  • Save khaosx/822f642bc5230bb2a58dd2e46d45e985 to your computer and use it in GitHub Desktop.
Save khaosx/822f642bc5230bb2a58dd2e46d45e985 to your computer and use it in GitHub Desktop.
#! /bin/bash
#
# Calculate bitrate statistics for movies
# Script courtesy of https://www.reddit.com/user/pcpcy
#
# Requires:
# ffmpeg_bitrate_stats (https://github.com/slhck/ffmpeg-bitrate-stats)
# printTable from util.bash (https://github.com/gdbtek/linux-cookbooks/blob/master/libraries/util.bash)
# jq (https://stedolan.github.io/jq/)
#
# Sample results: https://pastebin.com/qNJhj61x
#
set -e
# Find all 4K movies on hard drives
find /mnt/B /mnt/G /mnt/S /mnt/Z -type f -regextype egrep -iregex ".*/Downloads/Movies-4k/.*\.(mkv|mp4|ts)" -exec du -hs "{}" \; | sort -hr > 4k_files
# Calculate bitrate stats for all 4K files
# Bitrate calculation takes ~ 5 minutes per 60 GB 4K file
for i in $(< 4k_files); do echo "$i"; fname="$(echo "$i" | cut -f2)"; out_fname="rate_output/$(basename $fname).rate"; [[ -f "$out_fname" ]] && echo "Rate file already exists. Skipping." || ffmpeg_bitrate_stats -of json "$fname" > "$out_fname"; done
# Write table of bitrate stats to file
cd rate_output
echo "Name,Size,Average,Minimum,Maximum,Seconds > 100" > rate_table; for i in $(< ../4k_files); do fsize=$(echo "$i" | cut -f1); fname=$(basename $(echo "$i" | cut -f2)); avg=$(cat "$fname.rate" | jq -r '.avg_bitrate/1000'); max=$(cat "$fname.rate" | jq -r '.max_bitrate/1000'); min=$(cat "$fname.rate" | jq -r '.min_bitrate/1000'); count=$(cat "$fname.rate" | jq -r '.bitrate_per_chunk | .[] | "\(.) \(. > 100000)"' | grep true | wc -l); printf "%s,%s,%.2f,%.3f,%.2f,%d\n" "$fname" "$fsize" "$avg" "$min" "$max" "$count"; done >> rate_table
# Print table to output
printTable ',' "$(< rate_table)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment