[CmdletBinding(SupportsShouldProcess = $true)] param( [Parameter(Mandatory = $true)] [string]$Path, [ValidateSet('128k', '192k', '256k', '320k')] [string]$Bitrate = '192k', [string]$OutputDirectory, [switch]$Force ) $ErrorActionPreference = 'Stop' $ffmpeg = Get-Command ffmpeg -ErrorAction SilentlyContinue if (-not $ffmpeg) { throw 'ffmpeg не найден. Установите FFmpeg через winget.' } $resolved = Resolve-Path -LiteralPath $Path -ErrorAction Stop $audioExtensions = @('.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 { $audioExtensions -contains $_.Extension.ToLowerInvariant() }) if (-not $OutputDirectory) { $OutputDirectory = Join-Path $root 'bitrate' } } else { $file = Get-Item -LiteralPath $resolved.Path if ($audioExtensions -notcontains $file.Extension.ToLowerInvariant()) { throw "Файл не похож на аудио: $($file.Extension)" } $files = @($file) if (-not $OutputDirectory) { $OutputDirectory = Join-Path (Split-Path -Parent $file.FullName) 'bitrate' } } if ($files.Count -eq 0) { Write-Host 'Аудиофайлы не найдены.' exit 0 } $outRoot = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutputDirectory) New-Item -ItemType Directory -Path $outRoot -Force | Out-Null $done = 0 $skipped = 0 $failed = 0 foreach ($item in $files) { $destination = Join-Path $outRoot ($item.BaseName + '.mp3') if ((Test-Path -LiteralPath $destination) -and -not $Force) { Write-Host "Пропуск: $destination уже существует" $skipped++ continue } if (-not $PSCmdlet.ShouldProcess($destination, "Перекодировать в MP3 $Bitrate")) { continue } $overwrite = if ($Force) { '-y' } else { '-n' } $arguments = @( '-hide_banner', '-loglevel', 'error', '-nostdin', $overwrite, '-i', $item.FullName, '-map_metadata', '0', '-codec:a', 'libmp3lame', '-b:a', $Bitrate, $destination ) & $ffmpeg.Source @arguments if ($LASTEXITCODE -eq 0) { Write-Host "Готово: $($item.Name) -> $destination" $done++ } else { Write-Host "Ошибка: $($item.FullName)" -ForegroundColor Red $failed++ } } Write-Host "Итог: готово $done, пропущено $skipped, ошибок $failed" if ($failed -gt 0) { exit 2 }