Add -Clip parameter to remove letterboxing from 2-video comparisons by matching output height to scaled source height

This commit is contained in:
2026-06-01 02:28:59 -04:00
parent 6b5a67dddd
commit fab3020dd0
+57 -1
View File
@@ -53,6 +53,14 @@
.PARAMETER In4K .PARAMETER In4K
Generate comparison video in 4K UHD (3840x2160) resolution instead of 1K HD (1920x1080). Generate comparison video in 4K UHD (3840x2160) resolution instead of 1K HD (1920x1080).
.PARAMETER Clip
Reduces the output height to match the actual scaled height of the sources, removing
the top/bottom black letterbox padding. Only applies when exactly 2 source videos are
provided; ignored otherwise.
Example: two 1280x720 (16:9) sources rendered at 1K HD become 1920x540 instead of
1920x1080. The same pair rendered with -In4K becomes 3840x1080 instead of 3840x2160.
.EXAMPLE .EXAMPLE
.\Compare-Videos.ps1 -S1 original.mp4 -S2 enhanced.mp4 -Output comparison.mp4 .\Compare-Videos.ps1 -S1 original.mp4 -S2 enhanced.mp4 -Output comparison.mp4
@@ -77,6 +85,11 @@
.\Compare-Videos.ps1 -S1 original.mp4 -S2 enhanced.mp4 -Output comparison_4k.mp4 -In4K .\Compare-Videos.ps1 -S1 original.mp4 -S2 enhanced.mp4 -Output comparison_4k.mp4 -In4K
Compare two videos in 4K UHD (3840x2160) resolution. Compare two videos in 4K UHD (3840x2160) resolution.
.EXAMPLE
.\Compare-Videos.ps1 -S1 a_720p.mp4 -S2 b_720p.mp4 -Output side-by-side.mp4 -Clip
Compare two 720p (1280x720) videos with no top/bottom letterboxing: output is 1920x540.
#> #>
[CmdletBinding(SupportsShouldProcess)] [CmdletBinding(SupportsShouldProcess)]
param( param(
@@ -97,7 +110,9 @@ param(
[switch]$NoAudio, [switch]$NoAudio,
[switch]$In4K [switch]$In4K,
[switch]$Clip
) )
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
@@ -132,6 +147,11 @@ $videoCount = 2
if ($S4) { $videoCount = 4 } if ($S4) { $videoCount = 4 }
elseif ($S3) { $videoCount = 3 } elseif ($S3) { $videoCount = 3 }
if ($Clip -and $videoCount -ne 2) {
Write-Warning "-Clip is only supported with exactly 2 source videos; ignoring."
$Clip = $false
}
# --- Detect orientation of S1 --- # --- Detect orientation of S1 ---
Write-Host "Detecting orientation of S1..." -ForegroundColor Cyan Write-Host "Detecting orientation of S1..." -ForegroundColor Cyan
@@ -165,6 +185,42 @@ if ($isPortrait) {
Write-Host "Landscape mode detected (${width}x${height}) - Output: $resolutionLabel" -ForegroundColor Green Write-Host "Landscape mode detected (${width}x${height}) - Output: $resolutionLabel" -ForegroundColor Green
} }
# --- Clip mode: shrink output height to the actual scaled height of the sources ---
if ($Clip) {
Write-Host "Probing S2 dimensions for -Clip..." -ForegroundColor Cyan
$probeOutput2 = & ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 $S2 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to probe video dimensions for S2: $probeOutput2"
exit 1
}
$dim2 = $probeOutput2 -split ','
$w2 = [int]$dim2[0]
$h2 = [int]$dim2[1]
# Per-source cell width matches the 2-video branches below.
if ($isPortrait) {
$clipCellWidth = [int]($outputWidth * 0.6333 / 2)
} else {
$clipCellWidth = [int]($outputWidth / 2)
}
# Height each source would naturally scale to inside its cell when fitted by width
# (force_original_aspect_ratio=decrease). Take the larger of the two so neither
# source loses content; cap at the original output height.
$h1Scaled = [int][Math]::Floor($clipCellWidth * $height / $width)
$h2Scaled = [int][Math]::Floor($clipCellWidth * $h2 / $w2)
$clippedHeight = [Math]::Max($h1Scaled, $h2Scaled)
if ($clippedHeight % 2) { $clippedHeight++ } # libx264/yuv420p requires even dimensions
if ($clippedHeight -gt $outputHeight) { $clippedHeight = $outputHeight }
if ($clippedHeight -lt $outputHeight) {
Write-Host ("Clip mode: output height reduced from {0} to {1} ({2}x{1})" -f $outputHeight, $clippedHeight, $outputWidth) -ForegroundColor Yellow
$outputHeight = $clippedHeight
} else {
Write-Host "Clip mode: sources already fill the full output height; no clipping applied." -ForegroundColor Yellow
}
}
$filterComplex = "" $filterComplex = ""
$inputArgs = @("-i", $S1, "-i", $S2) $inputArgs = @("-i", $S1, "-i", $S2)