Add files via upload
This commit is contained in:
@@ -771,6 +771,106 @@ if ($renameSkipped -gt 0) {
|
||||
Write-Host ""
|
||||
}
|
||||
|
||||
# -- Rename numeric-only MP3 filenames using embedded ID3 title --------------
|
||||
# Some MP3s have filenames that are just numbers (e.g. "01.mp3", "1234.mp3",
|
||||
# "01 02 03.mp3"). These are useless in the playlist UI. When we detect such
|
||||
# a file, read the embedded ID3 title tag via Shell.Application and rename the
|
||||
# file to "<Title>.mp3" (sanitized for WoW 1.12 path compatibility).
|
||||
|
||||
function Get-Mp3EmbeddedTitle {
|
||||
param([Parameter(Mandatory=$true)][string]$FilePath)
|
||||
try {
|
||||
$shell = New-Object -ComObject Shell.Application
|
||||
$folder = $shell.Namespace((Split-Path -Parent $FilePath))
|
||||
if ($null -eq $folder) { return $null }
|
||||
$item = $folder.ParseName((Split-Path -Leaf $FilePath))
|
||||
if ($null -eq $item) { return $null }
|
||||
# Property index 21 = Title on Windows; fall back to scanning if empty.
|
||||
$title = $folder.GetDetailsOf($item, 21)
|
||||
if ([string]::IsNullOrWhiteSpace($title)) {
|
||||
for ($i = 0; $i -lt 400; $i++) {
|
||||
$header = $folder.GetDetailsOf($null, $i)
|
||||
if ($header -eq 'Title') {
|
||||
$title = $folder.GetDetailsOf($item, $i)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if ([string]::IsNullOrWhiteSpace($title)) { return $null }
|
||||
return $title.Trim()
|
||||
} catch {
|
||||
return $null
|
||||
}
|
||||
}
|
||||
|
||||
$numericRenamed = 0
|
||||
$numericSkipped = 0
|
||||
$mp3Files = @(Get-ChildItem -LiteralPath $Media -Recurse -File |
|
||||
Where-Object { $_.Extension -match '^(?i)\.mp3$' -and $_.Name -notmatch '\.sanitize\.tmp\.' })
|
||||
|
||||
foreach ($mf in $mp3Files) {
|
||||
$base = [System.IO.Path]::GetFileNameWithoutExtension($mf.Name)
|
||||
# "Only numbers": digits, optionally separated by spaces / dots / dashes / underscores.
|
||||
if ($base -notmatch '^[\d\s\.\-_]+$' -or $base -notmatch '\d') { continue }
|
||||
|
||||
$title = Get-Mp3EmbeddedTitle -FilePath $mf.FullName
|
||||
if ([string]::IsNullOrWhiteSpace($title)) {
|
||||
Write-Host " Skipped (no embedded title): $($mf.Name)"
|
||||
$numericSkipped++
|
||||
continue
|
||||
}
|
||||
|
||||
# Sanitize the title for filesystem + WoW 1.12 compatibility.
|
||||
$safeTitle = $title
|
||||
foreach ($ch in $problematicChars) { $safeTitle = $safeTitle.Replace($ch, '_') }
|
||||
foreach ($ch in [System.IO.Path]::GetInvalidFileNameChars()) {
|
||||
$safeTitle = $safeTitle.Replace([string]$ch, '_')
|
||||
}
|
||||
while ($safeTitle.Contains('__')) { $safeTitle = $safeTitle.Replace('__', '_') }
|
||||
$safeTitle = $safeTitle.Trim(' ', '_', '.')
|
||||
|
||||
if ([string]::IsNullOrWhiteSpace($safeTitle)) {
|
||||
Write-Host " Skipped (title sanitized to empty): $($mf.Name)"
|
||||
$numericSkipped++
|
||||
continue
|
||||
}
|
||||
|
||||
$newName = "$safeTitle.mp3"
|
||||
$newPath = Join-Path $mf.DirectoryName $newName
|
||||
if ($newPath -eq $mf.FullName) { continue }
|
||||
|
||||
# Avoid collisions by appending a numeric suffix.
|
||||
if (Test-Path -LiteralPath $newPath) {
|
||||
$n = 2
|
||||
do {
|
||||
$candidate = Join-Path $mf.DirectoryName ("$safeTitle ($n).mp3")
|
||||
$n++
|
||||
} while (Test-Path -LiteralPath $candidate)
|
||||
$newPath = $candidate
|
||||
$newName = Split-Path -Leaf $candidate
|
||||
}
|
||||
|
||||
try {
|
||||
Rename-Item -LiteralPath $mf.FullName -NewName $newName -Force
|
||||
Write-Host " Renamed (numeric -> title): $($mf.Name) -> $newName"
|
||||
$numericRenamed++
|
||||
} catch {
|
||||
Write-Host " WARNING: Could not rename $($mf.Name): $_"
|
||||
$numericSkipped++
|
||||
}
|
||||
}
|
||||
|
||||
if ($numericRenamed -gt 0) {
|
||||
Write-Host ""
|
||||
Write-Host "Renamed $numericRenamed numeric-only MP3 file(s) using embedded ID3 title."
|
||||
Write-Host ""
|
||||
}
|
||||
if ($numericSkipped -gt 0) {
|
||||
Write-Host ""
|
||||
Write-Host "$numericSkipped numeric-only MP3 file(s) skipped (no title / collision / error)."
|
||||
Write-Host ""
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Now that ffmpeg may be available, set up a helper to call it from the
|
||||
# local ffmpeg folder when the system PATH doesn't have it.
|
||||
|
||||
Reference in New Issue
Block a user