[CmdletBinding(SupportsShouldProcess = $true)] param( [Parameter(Mandatory = $true)] [string]$Path, [string]$OutputDirectory, [ValidateRange(10, 3600)] [int]$ShortSeconds = 180, [ValidateRange(60, 7200)] [int]$LongSeconds = 600, [switch]$Move, [switch]$Force ) $ErrorActionPreference = 'Stop' if ($ShortSeconds -ge $LongSeconds) { throw 'ShortSeconds должен быть меньше LongSeconds.' } $ffprobe = Get-Command ffprobe -ErrorAction SilentlyContinue if (-not $ffprobe) { throw 'ffprobe не найден. Установите FFmpeg через winget.' } $resolved = Resolve-Path -LiteralPath $Path -ErrorAction Stop $extensions = @('.mp3', '.wav', '.flac', '.ogg', '.m4a', '.aac', '.wma') if (Test-Path -LiteralPath $resolved.Path -PathType Container) { $root = $resolved.Path $files = @(Get-ChildItem -LiteralPath $root -File -Recurse -ErrorAction SilentlyContinue | Where-Object { $extensions -contains $_.Extension.ToLowerInvariant() }) } else { $item = Get-Item -LiteralPath $resolved.Path if ($extensions -notcontains $item.Extension.ToLowerInvariant()) { throw "Формат не похож на аудио: $($item.Extension)" } $root = Split-Path -Parent $item.FullName $files = @($item) } if ($files.Count -eq 0) { Write-Host 'Аудиофайлы не найдены.' exit 0 } if (-not $OutputDirectory) { $OutputDirectory = Join-Path $root 'sorted' } $outRoot = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutputDirectory) $done = 0 $skipped = 0 $failed = 0 foreach ($file in $files) { $raw = & $ffprobe.Source -v error -show_entries format=duration -of default=nw=1:nk=1 $file.FullName $duration = 0.0 if (-not [double]::TryParse([string]$raw, [ref]$duration)) { Write-Host "Не удалось прочитать длительность: $($file.FullName)" -ForegroundColor Red $failed++ continue } if ($duration -le $ShortSeconds) { $category = 'short' } elseif ($duration -ge $LongSeconds) { $category = 'long' } else { $category = 'medium' } $directory = Join-Path $outRoot $category New-Item -ItemType Directory -Path $directory -Force | Out-Null $destination = Join-Path $directory $file.Name if ((Test-Path -LiteralPath $destination) -and -not $Force) { Write-Host "Пропуск: $destination уже существует" $skipped++ continue } if (-not $PSCmdlet.ShouldProcess($file.FullName, "Переместить в $category")) { continue } try { if ($Move) { Move-Item -LiteralPath $file.FullName -Destination $destination -Force:$Force } else { Copy-Item -LiteralPath $file.FullName -Destination $destination -Force:$Force } Write-Host ("{0}: {1} ({2:N1} с)" -f $category, $file.Name, $duration) $done++ } catch { Write-Host "Ошибка: $($file.FullName): $($_.Exception.Message)" -ForegroundColor Red $failed++ } } Write-Host "Итог: обработано $done, пропущено $skipped, ошибок $failed" Write-Host "Папка результата: $outRoot" if ($failed -gt 0) { exit 2 }