[CmdletBinding(SupportsShouldProcess = $true)] param( [Parameter(Mandatory = $true)] [string]$Path, [string]$Title, [string]$Artist, [string]$Album, [string]$OutputDirectory, [switch]$Force ) $ErrorActionPreference = 'Stop' if (-not $Title -and -not $Artist -and -not $Album) { throw 'Укажите хотя бы один тег: -Title, -Artist или -Album.' } $ffmpeg = Get-Command ffmpeg -ErrorAction SilentlyContinue $ffprobe = Get-Command ffprobe -ErrorAction SilentlyContinue if (-not $ffmpeg -or -not $ffprobe) { throw 'ffmpeg и ffprobe должны быть установлены.' } $resolved = Resolve-Path -LiteralPath $Path -ErrorAction Stop $extensions = @('.mp3', '.flac', '.m4a', '.mp4', '.ogg', '.wav') 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 'tagged' } } else { $file = Get-Item -LiteralPath $resolved.Path if ($extensions -notcontains $file.Extension.ToLowerInvariant()) { throw "Формат не поддержан: $($file.Extension)" } $files = @($file) if (-not $OutputDirectory) { $OutputDirectory = Join-Path (Split-Path -Parent $file.FullName) 'tagged' } } 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 + $item.Extension) if ((Test-Path -LiteralPath $destination) -and -not $Force) { Write-Host "Пропуск: $destination уже существует" $skipped++ continue } if (-not $PSCmdlet.ShouldProcess($destination, 'Записать теги')) { continue } $arguments = @('-hide_banner', '-loglevel', 'error', '-nostdin') if ($Force) { $arguments += '-y' } else { $arguments += '-n' } $arguments += @('-i', $item.FullName, '-map', '0', '-map_metadata', '0', '-c', 'copy') if ($Title) { $arguments += @('-metadata', "title=$Title") } if ($Artist) { $arguments += @('-metadata', "artist=$Artist") } if ($Album) { $arguments += @('-metadata', "album=$Album") } $arguments += $destination & $ffmpeg.Source @arguments if ($LASTEXITCODE -ne 0) { Write-Host "Ошибка записи: $($item.FullName)" -ForegroundColor Red $failed++ continue } $raw = & $ffprobe.Source -v quiet -print_format json -show_format $destination if ($LASTEXITCODE -eq 0) { $info = $raw | ConvertFrom-Json $tags = $info.format.tags if ($tags) { Write-Host ("Теги: title={0}; artist={1}; album={2}" -f $tags.title, $tags.artist, $tags.album) } } Write-Host "Готово: $destination" $done++ } Write-Host "Итог: готово $done, пропущено $skipped, ошибок $failed" if ($failed -gt 0) { exit 2 }