367 lines
11 KiB
PowerShell
367 lines
11 KiB
PowerShell
param(
|
|
[string]$ManifestPath = "",
|
|
[string]$OutputRoot = "\\VaultOfData\AKAMedia\음악\처리됨",
|
|
[string]$ArtistName = "SUNO",
|
|
[string]$FfmpegPath = "",
|
|
[switch]$KeepWorkFiles,
|
|
[switch]$Force
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
function Resolve-Ffmpeg {
|
|
param([string]$PreferredPath)
|
|
|
|
if ($PreferredPath -and (Test-Path -LiteralPath $PreferredPath)) {
|
|
return (Resolve-Path -LiteralPath $PreferredPath).Path
|
|
}
|
|
|
|
$repoCandidate = "E:\forfun\ffmpeg\bin\ffmpeg.exe"
|
|
if (Test-Path -LiteralPath $repoCandidate) {
|
|
return $repoCandidate
|
|
}
|
|
|
|
$cmd = Get-Command ffmpeg -ErrorAction SilentlyContinue
|
|
if ($cmd) {
|
|
return $cmd.Source
|
|
}
|
|
|
|
throw "ffmpeg.exe를 찾지 못했어. -FfmpegPath 로 경로를 지정해줘."
|
|
}
|
|
|
|
function Resolve-Manifest {
|
|
param([string]$Path)
|
|
|
|
if ($Path -and (Test-Path -LiteralPath $Path)) {
|
|
return (Resolve-Path -LiteralPath $Path).Path
|
|
}
|
|
|
|
$candidates = @()
|
|
$downloads = Join-Path $env:USERPROFILE "Downloads"
|
|
if (Test-Path -LiteralPath $downloads) {
|
|
$candidates += Get-ChildItem -LiteralPath $downloads -Filter "suno-library-*.manifest.json" -File -ErrorAction SilentlyContinue
|
|
}
|
|
$candidates += Get-ChildItem -LiteralPath (Get-Location) -Filter "suno-library-*.manifest.json" -File -ErrorAction SilentlyContinue
|
|
|
|
$latest = $candidates | Sort-Object LastWriteTime -Descending | Select-Object -First 1
|
|
if ($latest) {
|
|
return $latest.FullName
|
|
}
|
|
|
|
throw "manifest 파일을 찾지 못했어. -ManifestPath 로 suno-library-*.manifest.json 경로를 넘겨줘."
|
|
}
|
|
|
|
function ConvertTo-SafeName {
|
|
param(
|
|
[string]$Value,
|
|
[string]$Fallback = "suno-track"
|
|
)
|
|
|
|
$name = if ($Value) { $Value } else { $Fallback }
|
|
$name = $name -replace '[\\/:*?"<>|]+', ' '
|
|
$name = $name -replace '\s+', ' '
|
|
$name = $name.Trim()
|
|
if (-not $name) { $name = $Fallback }
|
|
if ($name.Length -gt 120) { $name = $name.Substring(0, 120).Trim() }
|
|
return $name
|
|
}
|
|
|
|
function Get-ShortId {
|
|
param([string]$Id)
|
|
|
|
if (-not $Id) { return "unknown-id" }
|
|
if ($Id.Length -le 12) { return $Id }
|
|
return $Id.Substring(0, 12)
|
|
}
|
|
|
|
function Get-TrackFolder {
|
|
param(
|
|
[string]$Root,
|
|
[string]$Title,
|
|
[string]$Id
|
|
)
|
|
|
|
$safeTitle = ConvertTo-SafeName $Title $Id
|
|
$shortId = Get-ShortId $Id
|
|
return Join-Path $Root ("{0} [{1}]" -f $safeTitle, $shortId)
|
|
}
|
|
|
|
function Get-AlbumTitle {
|
|
param(
|
|
[string]$Title,
|
|
[string]$Id
|
|
)
|
|
|
|
return "{0} [{1}]" -f (ConvertTo-SafeName $Title $Id), (Get-ShortId $Id)
|
|
}
|
|
|
|
function Resolve-LibraryRoot {
|
|
param(
|
|
[string]$Root,
|
|
[string]$Artist
|
|
)
|
|
|
|
$trimmed = $Root.TrimEnd("\", "/")
|
|
if ((Split-Path -Leaf $trimmed) -ieq $Artist) {
|
|
return $trimmed
|
|
}
|
|
return Join-Path $trimmed $Artist
|
|
}
|
|
|
|
function Test-ExistingTrack {
|
|
param(
|
|
[string]$Root,
|
|
[string]$Id
|
|
)
|
|
|
|
if (-not $Id -or -not (Test-Path -LiteralPath $Root)) { return $false }
|
|
|
|
$metadataHit = Get-ChildItem -LiteralPath $Root -Recurse -Filter "*metadata.json" -File -ErrorAction SilentlyContinue |
|
|
Where-Object {
|
|
try {
|
|
$json = Get-Content -LiteralPath $_.FullName -Raw | ConvertFrom-Json
|
|
"$($json.id)" -eq $Id
|
|
} catch {
|
|
$false
|
|
}
|
|
} |
|
|
Select-Object -First 1
|
|
|
|
return [bool]$metadataHit
|
|
}
|
|
|
|
function Get-ObjectValues {
|
|
param($Value)
|
|
|
|
if ($null -eq $Value) { return }
|
|
|
|
if ($Value -is [System.Collections.IEnumerable] -and -not ($Value -is [string]) -and -not ($Value -is [pscustomobject])) {
|
|
foreach ($item in $Value) { Get-ObjectValues $item }
|
|
return
|
|
}
|
|
|
|
if ($Value -is [pscustomobject]) {
|
|
$Value
|
|
foreach ($prop in $Value.PSObject.Properties) {
|
|
Get-ObjectValues $prop.Value
|
|
}
|
|
}
|
|
}
|
|
|
|
function Get-DeepValue {
|
|
param(
|
|
$Object,
|
|
[string[]]$Keys
|
|
)
|
|
|
|
foreach ($node in Get-ObjectValues $Object) {
|
|
foreach ($key in $Keys) {
|
|
$prop = $node.PSObject.Properties[$key]
|
|
if ($prop -and $null -ne $prop.Value -and "$($prop.Value)".Trim()) {
|
|
return "$($prop.Value)".Trim()
|
|
}
|
|
}
|
|
}
|
|
|
|
return ""
|
|
}
|
|
|
|
function Get-UrlExtension {
|
|
param(
|
|
[string]$Url,
|
|
[string]$Fallback
|
|
)
|
|
|
|
$clean = ($Url -split '[?#]')[0]
|
|
$ext = [IO.Path]::GetExtension($clean).TrimStart(".")
|
|
if ($ext) { return $ext.ToLowerInvariant() }
|
|
return $Fallback
|
|
}
|
|
|
|
function Resolve-SunoMediaUrl {
|
|
param([string]$Value)
|
|
|
|
if (-not $Value) { return "" }
|
|
$raw = $Value.Trim()
|
|
if ($raw -match '^https?://') { return $raw }
|
|
if ($raw.StartsWith("//")) { return "https:$raw" }
|
|
if ($raw.StartsWith("/")) { return "https://suno.com$raw" }
|
|
return $raw
|
|
}
|
|
|
|
function Invoke-Download {
|
|
param(
|
|
[string]$Url,
|
|
[string]$OutFile
|
|
)
|
|
|
|
if (-not $Url) { return $false }
|
|
|
|
$headers = @{
|
|
"User-Agent" = "Mozilla/5.0"
|
|
"Referer" = "https://suno.com/"
|
|
}
|
|
|
|
try {
|
|
Invoke-WebRequest -Uri $Url -OutFile $OutFile -Headers $headers -UseBasicParsing
|
|
return (Test-Path -LiteralPath $OutFile) -and ((Get-Item -LiteralPath $OutFile).Length -gt 0)
|
|
} catch {
|
|
Write-Warning "다운로드 실패: $Url :: $($_.Exception.Message)"
|
|
return $false
|
|
}
|
|
}
|
|
|
|
function Add-MetadataArg {
|
|
param(
|
|
[System.Collections.Generic.List[string]]$Args,
|
|
[string]$Key,
|
|
[string]$Value
|
|
)
|
|
|
|
if ($Value) {
|
|
$Args.Add("-metadata")
|
|
$Args.Add("$Key=$Value")
|
|
}
|
|
}
|
|
|
|
$ffmpeg = Resolve-Ffmpeg $FfmpegPath
|
|
$manifestFile = Resolve-Manifest $ManifestPath
|
|
$manifest = Get-Content -LiteralPath $manifestFile -Raw | ConvertFrom-Json
|
|
$tracks = @($manifest.tracks)
|
|
|
|
if (-not $tracks.Count) {
|
|
throw "manifest에 tracks 배열이 없어."
|
|
}
|
|
|
|
$libraryRoot = Resolve-LibraryRoot $OutputRoot $ArtistName
|
|
New-Item -ItemType Directory -Force -Path $libraryRoot | Out-Null
|
|
$workRoot = Join-Path $libraryRoot "_work"
|
|
New-Item -ItemType Directory -Force -Path $workRoot | Out-Null
|
|
|
|
Write-Host "Manifest: $manifestFile"
|
|
Write-Host "Output: $libraryRoot"
|
|
Write-Host "ffmpeg: $ffmpeg"
|
|
Write-Host "Tracks: $($tracks.Count)"
|
|
|
|
$index = 0
|
|
foreach ($track in $tracks) {
|
|
$index++
|
|
|
|
$id = if ($track.id) { "$($track.id)" } else { "" }
|
|
$title = if ($track.title) { "$($track.title)" } else { $id }
|
|
if (-not $Force -and (Test-ExistingTrack $libraryRoot $id)) {
|
|
Write-Host ""
|
|
Write-Host "[$index/$($tracks.Count)] 이미 있음, 건너뜀: $title [$id]"
|
|
continue
|
|
}
|
|
|
|
$albumTitle = Get-AlbumTitle $title $id
|
|
$trackFolder = Get-TrackFolder $libraryRoot $title $id
|
|
New-Item -ItemType Directory -Force -Path $trackFolder | Out-Null
|
|
$safeTitle = ConvertTo-SafeName $title $id
|
|
$shortId = Get-ShortId $id
|
|
$prefix = "{0} [{1}]" -f $safeTitle, $shortId
|
|
|
|
$lyrics = Get-DeepValue $track @("prompt", "lyrics", "lyric", "text")
|
|
$style = Get-DeepValue $track @("gpt_description_prompt", "style", "tags", "description")
|
|
$model = Get-DeepValue $track @("model_name", "model", "major_model_version")
|
|
$createdAt = Get-DeepValue $track @("created_at", "createdAt")
|
|
|
|
$wavUrl = Resolve-SunoMediaUrl $(if ($track.wavUrl) { "$($track.wavUrl)" } else { Get-DeepValue $track @("wav_url", "wavUrl", "download_url", "downloadUrl") })
|
|
$wavCandidates = @()
|
|
if ($wavUrl) { $wavCandidates += $wavUrl }
|
|
if ($id) { $wavCandidates += "https://cdn1.suno.ai/$id.wav" }
|
|
$wavCandidates = $wavCandidates | Where-Object { $_ } | Select-Object -Unique
|
|
$imageUrl = if ($track.imageUrl) { "$($track.imageUrl)" } else { Get-DeepValue $track @("image_url", "imageUrl", "image_large_url", "cover_url") }
|
|
|
|
$inputAudio = Join-Path $workRoot "$prefix.input.wav"
|
|
$rawCoverFile = Join-Path $workRoot "$prefix.cover.$(Get-UrlExtension $imageUrl 'jpg')"
|
|
$coverFile = Join-Path $trackFolder "cover.jpg"
|
|
$metadataFile = Join-Path $trackFolder "metadata.json"
|
|
$flacFile = Join-Path $trackFolder ("01 {0}.flac" -f $safeTitle)
|
|
|
|
Write-Host ""
|
|
Write-Host "[$index/$($tracks.Count)] $title"
|
|
|
|
$audioOk = $false
|
|
foreach ($candidate in $wavCandidates) {
|
|
Write-Host "WAV 후보 시도: $candidate"
|
|
$audioOk = Invoke-Download $candidate $inputAudio
|
|
if ($audioOk) { break }
|
|
}
|
|
if (-not $audioOk) {
|
|
Write-Warning "WAV를 받지 못해서 건너뜀: $title"
|
|
continue
|
|
}
|
|
|
|
$coverOk = $false
|
|
if ($imageUrl) {
|
|
$coverDownloaded = Invoke-Download $imageUrl $rawCoverFile
|
|
if ($coverDownloaded) {
|
|
& $ffmpeg -hide_banner -y -i $rawCoverFile -frames:v 1 $coverFile
|
|
$coverOk = ($LASTEXITCODE -eq 0) -and (Test-Path -LiteralPath $coverFile)
|
|
if (-not $coverOk) {
|
|
Copy-Item -LiteralPath $rawCoverFile -Destination $coverFile -Force
|
|
$coverOk = Test-Path -LiteralPath $coverFile
|
|
}
|
|
}
|
|
}
|
|
|
|
$track | ConvertTo-Json -Depth 50 | Set-Content -LiteralPath $metadataFile -Encoding UTF8
|
|
|
|
$argsList = [System.Collections.Generic.List[string]]::new()
|
|
$argsList.Add("-hide_banner")
|
|
$argsList.Add("-y")
|
|
$argsList.Add("-i")
|
|
$argsList.Add($inputAudio)
|
|
if ($coverOk) {
|
|
$argsList.Add("-i")
|
|
$argsList.Add($coverFile)
|
|
$argsList.Add("-map")
|
|
$argsList.Add("0:a")
|
|
$argsList.Add("-map")
|
|
$argsList.Add("1:v")
|
|
$argsList.Add("-disposition:v")
|
|
$argsList.Add("attached_pic")
|
|
$argsList.Add("-c:v")
|
|
$argsList.Add("copy")
|
|
$argsList.Add("-metadata:s:v")
|
|
$argsList.Add("comment=Cover (front)")
|
|
}
|
|
$argsList.Add("-c:a")
|
|
$argsList.Add("flac")
|
|
$argsList.Add("-compression_level")
|
|
$argsList.Add("8")
|
|
Add-MetadataArg $argsList "title" $title
|
|
Add-MetadataArg $argsList "artist" $ArtistName
|
|
Add-MetadataArg $argsList "album" $albumTitle
|
|
Add-MetadataArg $argsList "album_artist" $ArtistName
|
|
Add-MetadataArg $argsList "ARTISTS" $ArtistName
|
|
Add-MetadataArg $argsList "track" "1/1"
|
|
Add-MetadataArg $argsList "disc" "1/1"
|
|
Add-MetadataArg $argsList "genre" $style
|
|
Add-MetadataArg $argsList "lyrics-XXX" $lyrics
|
|
Add-MetadataArg $argsList "description" $style
|
|
Add-MetadataArg $argsList "comment" "Suno clip id: $id"
|
|
Add-MetadataArg $argsList "date" $createdAt
|
|
Add-MetadataArg $argsList "encoder" "Codex SUNO helper"
|
|
Add-MetadataArg $argsList "suno_model" $model
|
|
Add-MetadataArg $argsList "TMED" "Digital Media"
|
|
$argsList.Add($flacFile)
|
|
|
|
& $ffmpeg @argsList
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Warning "FLAC 변환 실패: $title"
|
|
continue
|
|
}
|
|
|
|
Write-Host "완료: $flacFile"
|
|
}
|
|
|
|
if (-not $KeepWorkFiles -and (Test-Path -LiteralPath $workRoot)) {
|
|
Remove-Item -LiteralPath $workRoot -Recurse -Force
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "처리 완료: $libraryRoot"
|