Skip to content

Commit

Permalink
bitrateCalc: New script
Browse files Browse the repository at this point in the history
Give it target filesize and audio bitrate, and it will give you a
bitrate based on the duration of the input file.
  • Loading branch information
flaeri committed Dec 31, 2023
1 parent 6d24331 commit 4c4c6b1
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions bitrateCalc.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# User configurable with prompt
$maxSizeInput = Read-Host -Prompt "Enter max file size in kilobytes (e.g., 8000 for 8 MB)"
$audioBrInput = Read-Host -Prompt "Enter audio bitrate in kbit (e.g., 128 for 128 kbps)"

# Remove spaces and convert to integer
$maxSize = [int]($maxSizeInput -replace '\s', '')
$audioBr = [int]($audioBrInput -replace '\s', '')

# Rest of your script
$video = Read-Host -Prompt "`nPlease drag&drop a video" # Drag video in
$video = Get-ChildItem -Path ($video -replace '"', "") # Input is a string, fix it
Clear-Host

# Calc
# 5% overhead safety
$safeSize = $maxSize * 0.95

# Get duration of file
$durationSec = ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$video"
$durationSecClamp = [math]::Round($durationSec)

$vidBr = $safeSize * 8 / $durationSec # size in MB * 8 = bits, divided by duration. x 1000 for kbps
$vidBr = [math]::Round($vidBr) # int please
$vidBr = $vidBr - $audioBr # account for audio bitrate

# Format video bitrate with space as thousand delimiter
$culture = [System.Globalization.CultureInfo]::InvariantCulture.Clone()
$culture.NumberFormat.NumberGroupSeparator = " "
$vidBrFormatted = "{0:N0}" -f $vidBr
$vidBrFormatted = $vidBrFormatted -replace ",", " " # Replace comma with space if necessary

Write-Host "Duration: $durationSecClamp sec" -ForegroundColor Yellow
Write-Host "Video Bitrate (kbps): $vidBrFormatted" -ForegroundColor Yellow
Write-Host "Audio Bitrate (kbps): $audioBr" -ForegroundColor Yellow
pause

0 comments on commit 4c4c6b1

Please sign in to comment.