[CmdletBinding(SupportsShouldProcess = $true)] param( [Parameter(Mandatory = $true)] [string]$Path, [string]$OutputDirectory, [ValidateSet('128k', '192k', '256k')] [string]$Bitrate = '192k', [switch]$Force ) $ErrorActionPreference = 'Stop' $ffmpeg = Get-Command ffmpeg -ErrorAction SilentlyContinue if (-not $ffmpeg) { throw 'ffmpeg не найден. Установите 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() }) if (-not $OutputDirectory) { $OutputDirectory = Join-Path $root 'phone' } } else { $item = Get-Item -LiteralPath $resolved.Path if ($extensions -notcontains $item.Extension.ToLowerInvariant()) { throw "Формат не похож на аудио: $($item.Extension)" } $files = @($item) if (-not $OutputDirectory) { $OutputDirectory = Join-Path (Split-Path -Parent $item.FullName) 'phone' } } 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 + '.m4a') if ((Test-Path -LiteralPath $destination) -and -not $Force) { Write-Host "Пропуск: $destination уже существует" $skipped++ continue } if (-not $PSCmdlet.ShouldProcess($destination, 'Конвертировать для телефона')) { continue } $overwrite = if ($Force) { '-y' } else { '-n' } & $ffmpeg.Source -hide_banner -loglevel error -nostdin $overwrite ` -i $item.FullName -map_metadata 0 -vn ` -c:a aac -b:a $Bitrate -ar 44100 -ac 2 -movflags +faststart $destination 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 }