Skip to content

Instantly share code, notes, and snippets.

@twobob
Created October 22, 2025 17:04
Show Gist options
  • Save twobob/2ff60e04a569b7b6f544b1a1645e0d46 to your computer and use it in GitHub Desktop.
Save twobob/2ff60e04a569b7b6f544b1a1645e0d46 to your computer and use it in GitHub Desktop.

Revisions

  1. twobob created this gist Oct 22, 2025.
    56 changes: 56 additions & 0 deletions normaliseVideo.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    # --- Configuration ---
    $inputFile = "C:\Users\new\Videos\someVideo.mp4"
    $outputFile = "C:\Users\new\Videos\someVideoNormalised.mp4"

    # --- Target Loudness ---
    $target_I = "-14"
    $target_LRA = "7"
    $target_TP = "-1.5"

    Write-Host "Running Pass 1 (analysis)..." -ForegroundColor Yellow

    # Build filter safely (join with ':')
    $filter1 = @(
    "loudnorm=I=$target_I"
    "LRA=$target_LRA"
    "TP=$target_TP"
    "print_format=json"
    ) -join ':'

    # Run ffmpeg pass 1 (use call operator '&' with argument tokens)
    $ffmpegOutput = & ffmpeg -hide_banner -i $inputFile -af $filter1 -f null NUL 2>&1

    # Extract JSON block
    $jsonMatch = [regex]::Match($ffmpegOutput, '(?s)\{.*\}')
    if (-not $jsonMatch.Success) {
    Write-Host $ffmpegOutput
    throw "No JSON stats found."
    }

    # Parse JSON
    $stats = $jsonMatch.Value | ConvertFrom-Json
    Write-Host ("Measured I : {0}" -f $stats.input_i)
    Write-Host ("Measured TP : {0}" -f $stats.input_tp)
    Write-Host ("Measured LRA: {0}" -f $stats.input_lra)

    Write-Host "Running Pass 2 (normalising)..." -ForegroundColor Yellow

    # Build second-pass filter safely (no concatenation-before-colon issues)
    $filter2 = @(
    "loudnorm=I=$target_I"
    "LRA=$target_LRA"
    "TP=$target_TP"
    ("measured_I={0}" -f $stats.input_i)
    ("measured_TP={0}" -f $stats.input_tp)
    ("measured_LRA={0}" -f $stats.input_lra)
    ("measured_thresh={0}" -f $stats.input_thresh)
    ("offset={0}" -f $stats.target_offset)
    ) -join ':'

    # Run ffmpeg pass 2
    & ffmpeg -hide_banner -i $inputFile -af $filter2 -c:v copy -c:a aac -y $outputFile
    if ($LASTEXITCODE -ne 0) {
    throw "FFmpeg Pass 2 failed."
    }

    Write-Host "Done. Normalised file saved to: $outputFile" -ForegroundColor Green