Fix -Frames negative range calculation to extract last N frames correctly (off-by-one error), add validation to reject invalid '-%' and '-0' formats with exit code 1, change '%' alone to extract single last frame instead of range, remove duplicate last frame from PNG sequence concat list, update Get-LastFrame.ps1 and Get-Frame.ps1 to clamp out-of-range frame indices instead of erroring and improve error messages with inline stderr output, add -y flag and -- separator to ffprobe calls, replace deprecated

This commit is contained in:
guru committed 2026-11-02 06:30:38 -05:00
1 parent cf41fb0ce5
commit 1227c6e153
13 files changed
+100 -86

No files matched your search

+12 -3
View File
@@ -11,7 +11,7 @@
- start-end: Extract frames from start to end (e.g., "100-200")
- positive_number: Extract frames from 0 to that number (e.g., "50")
- -negative_number: Extract last N frames (e.g., "-10")
- %: Represents the last frame index. Can be used alone or in ranges (e.g., "%", "10-%", "-%")
- %: Represents the last frame index. Can be used alone or in ranges (e.g., "%", "10-%")
- all: Extract all frames (same as "0-%")
.DESCRIPTION
Extracts specific range of frames of a video file to PNG images using ffmpeg.
@@ -40,6 +40,15 @@ if (-not (Test-Path -LiteralPath $VideoPath)) {
exit 1
}
if ($Frames -match '^-.*%') {
Write-Error "Invalid Frames format '$Frames': '-%' is not supported. Use '%' for the last frame, or -N for the last N frames."
exit 1
}
if ($Frames -match '^-0+$') {
Write-Error "Invalid Frames format '$Frames': a negative count must be at least 1. Use -1 for the last frame."
exit 1
}
$PSNativeCommandUseErrorActionPreference = $false
$totalFramesOutput = & ffprobe -v error -select_streams v:0 -count_frames -show_entries stream=nb_read_frames -of csv=p=0 -- $VideoPath 2>&1
@@ -65,7 +74,7 @@ $startFrame = 0
$endFrame = 0
# Replace % with actual last frame index, and handle 'all' keyword
$framesPattern = $Frames -replace '%', $maxFrameIndex
$framesPattern = if ($Frames -eq '%') { "$maxFrameIndex-$maxFrameIndex" } else { $Frames -replace '%', $maxFrameIndex }
if ($framesPattern -ieq 'all') {
$framesPattern = "0-$maxFrameIndex"
}
@@ -76,7 +85,7 @@ if ($framesPattern -match '^(\d+)-(\d+)$') {
}
elseif ($framesPattern -match '^-(\d+)$') {
$negativeCount = [int]$matches[1]
$startFrame = [Math]::Max(0, $maxFrameIndex - $negativeCount)
$startFrame = [Math]::Max(0, $maxFrameIndex - $negativeCount + 1)
$endFrame = $maxFrameIndex
}
elseif ($framesPattern -match '^\d+$') {