[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [string]$Csv ) $ErrorActionPreference = 'Stop' $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', '.opus') if (Test-Path -LiteralPath $resolved.Path -PathType Container) { $files = @(Get-ChildItem -LiteralPath $resolved.Path -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)" } $files = @($item) } if ($files.Count -eq 0) { Write-Host 'Аудиофайлы не найдены.' exit 0 } $records = @() foreach ($file in $files) { $raw = & $ffprobe.Source -v quiet -print_format json -show_format -show_streams $file.FullName if ($LASTEXITCODE -ne 0) { continue } try { $info = $raw | ConvertFrom-Json } catch { continue } $audio = @($info.streams | Where-Object { $_.codec_type -eq 'audio' }) | Select-Object -First 1 if (-not $audio) { continue } $duration = 0.0 if ($info.format.duration) { [void][double]::TryParse([string]$info.format.duration, [ref]$duration) } $bitrate = [int64]0 if ($audio.bit_rate) { [void][int64]::TryParse([string]$audio.bit_rate, [ref]$bitrate) } if ($bitrate -le 0 -and $info.format.bit_rate) { [void][int64]::TryParse([string]$info.format.bit_rate, [ref]$bitrate) } $tags = $info.format.tags $title = if ($tags -and $tags.title) { [string]$tags.title } else { '' } $artist = if ($tags -and $tags.artist) { [string]$tags.artist } else { '' } $album = if ($tags -and $tags.album) { [string]$tags.album } else { '' } $records += [pscustomobject]@{ File = $file.FullName Codec = [string]$audio.codec_name DurationSec = [math]::Round($duration, 2) BitrateKbps = if ($bitrate -gt 0) { [math]::Round($bitrate / 1000, 0) } else { 0 } SizeMB = [math]::Round($file.Length / 1MB, 2) Artist = $artist Album = $album Title = $title MissingTags = (($title -eq '') -or ($artist -eq '') -or ($album -eq '')) } } if ($records.Count -eq 0) { Write-Host 'Не удалось прочитать ни одного файла.' exit 1 } $totalSeconds = ($records | Measure-Object -Property DurationSec -Sum).Sum $hours = [int][math]::Floor($totalSeconds / 3600) $minutes = [int][math]::Floor(($totalSeconds % 3600) / 60) $seconds = [int][math]::Floor($totalSeconds % 60) $totalSize = ($records | Measure-Object -Property SizeMB -Sum).Sum $weighted = 0.0 $weight = 0.0 foreach ($record in $records) { if ($record.BitrateKbps -gt 0 -and $record.DurationSec -gt 0) { $weighted += $record.BitrateKbps * $record.DurationSec $weight += $record.DurationSec } } $averageKbps = if ($weight -gt 0) { [math]::Round($weighted / $weight, 0) } else { 0 } $missing = @($records | Where-Object { $_.MissingTags }).Count $formats = ($records | Group-Object Codec | Sort-Object Count -Descending | ForEach-Object { "$($_.Name): $($_.Count)" }) -join ', ' Write-Host 'Сводка по библиотеке' Write-Host "Файлов: $($records.Count)" Write-Host ("Общее время: {0:00}:{1:00}:{2:00}" -f $hours, $minutes, $seconds) Write-Host ("Размер: {0:N2} ГБ" -f ($totalSize / 1024)) Write-Host "Средний битрейт: $averageKbps kbps" Write-Host "Форматы: $formats" Write-Host "Без полных тегов: $missing" $top = $records | Where-Object { $_.Artist } | Group-Object Artist | Sort-Object Count -Descending | Select-Object -First 5 if ($top) { Write-Host '' Write-Host 'Топ исполнителей:' $top | ForEach-Object { Write-Host (" {0}: {1}" -f $_.Name, $_.Count) } } if ($Csv) { $csvPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Csv) $records | Export-Csv -LiteralPath $csvPath -NoTypeInformation -Encoding UTF8 Write-Host "CSV сохранён: $csvPath" }