[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$InputPath, [string]$Time = '00:00:05', [ValidateRange(64, 3840)] [int]$Width = 640, [string]$OutputPath, [switch]$Force ) $ErrorActionPreference = 'Stop' if (-not (Get-Command ffmpeg -ErrorAction SilentlyContinue)) { throw 'ffmpeg не найден в PATH. Установите FFmpeg.' } if (-not (Test-Path -LiteralPath $InputPath -PathType Leaf)) { throw "Входной файл не найден: $InputPath" } if (-not $Time.Trim()) { throw 'Укажите время кадра, например 00:00:05.' } $source = (Resolve-Path -LiteralPath $InputPath).Path if (-not $OutputPath) { $directory = Split-Path -Parent $source $name = [IO.Path]::GetFileNameWithoutExtension($source) $OutputPath = Join-Path (Join-Path $directory 'thumbnails') "${name}_thumb.jpg" } $target = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutputPath) if ((Test-Path -LiteralPath $target) -and (-not $Force)) { throw "Файл уже существует: $target. Добавьте -Force." } $targetDir = Split-Path -Parent $target if (-not (Test-Path -LiteralPath $targetDir)) { New-Item -ItemType Directory -Path $targetDir -Force | Out-Null } $arguments = @( '-hide_banner', '-nostdin', '-ss', $Time, '-i', $source, '-frames:v', '1', '-vf', "scale=${Width}:-2", '-q:v', '3', '-update', '1' ) if ($Force) { $arguments += '-y' } else { $arguments += '-n' } $arguments += $target Write-Host "Кадр $Time, ширина $Width" & ffmpeg @arguments if ($LASTEXITCODE -ne 0) { throw "ffmpeg завершился с кодом $LASTEXITCODE" } if (-not (Test-Path -LiteralPath $target)) { throw 'ffmpeg не создал файл.' } Write-Host "Готово: $target"