-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_extensions.ps1
More file actions
78 lines (63 loc) · 2.69 KB
/
Copy pathfix_extensions.ps1
File metadata and controls
78 lines (63 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# fix_extensions.ps1 - Detect and fix incorrect file extensions in the mp3files folder.
#
# Some audio files (like 'Simple Plan - Jet Lag' and 'Projekt F - Blue Neon') are actually MPEG-4 (M4A) files
# but have been incorrectly named with the .mp3 extension. This causes playback issues on VLC and other players
# because the server serves them with an incorrect MIME type (audio/mpeg instead of audio/mp4).
#
# This script reads the file header (magic bytes) to detect the true format and renames the file if necessary.
$MusicDir = Join-Path $PSScriptRoot "mp3files"
if (-not (Test-Path $MusicDir)) {
Write-Error "Music directory not found at $MusicDir"
exit 1
}
$Files = Get-ChildItem -Path $MusicDir -File
$RenamedCount = 0
foreach ($File in $Files) {
$FilePath = $File.FullName
# Open file stream and read the first 12 bytes
try {
$Stream = [System.IO.File]::OpenRead($FilePath)
$Buffer = New-Object byte[] 12
$BytesRead = $Stream.Read($Buffer, 0, 12)
$Stream.Close()
}
catch {
Write-Warning "Could not read file header for: $($File.Name)"
continue
}
if ($BytesRead -lt 8) {
continue
}
# Check for 'ftyp' at offset 4 (MPEG-4 container: M4A, MP4)
# Hex for 'ftyp' is 0x66 0x74 0x79 0x70
$IsM4A = $Buffer[4] -eq 0x66 -and $Buffer[5] -eq 0x74 -and $Buffer[6] -eq 0x79 -and $Buffer[7] -eq 0x70
# Check for 'ID3' at offset 0 (MP3 with ID3v2 tag)
$IsID3 = $Buffer[0] -eq 0x49 -and $Buffer[1] -eq 0x44 -and $Buffer[2] -eq 0x33
$CurrentExt = $File.Extension.ToLower()
$NewExt = $null
if ($IsM4A -and $CurrentExt -ne ".m4a") {
$NewExt = ".m4a"
} elseif ($IsID3 -and $CurrentExt -ne ".mp3") {
$NewExt = ".mp3"
}
if ($NewExt) {
$NewName = [System.IO.Path]::ChangeExtension($File.Name, $NewExt)
$NewPath = Join-Path $MusicDir $NewName
Write-Host "Fixing: '$($File.Name)' -> '$NewName' (detected true format as $(if ($NewExt -eq '.m4a') {'M4A'} else {'MP3'}))" -ForegroundColor Yellow
try {
Rename-Item -LiteralPath $FilePath -NewName $NewName -Force
$RenamedCount++
}
catch {
Write-Error "Failed to rename $($File.Name): $_"
}
}
}
if ($RenamedCount -gt 0) {
Write-Host "`nSuccessfully renamed $RenamedCount file(s)." -ForegroundColor Green
Write-Host "Regenerating playlist.m3u..." -ForegroundColor Cyan
python generate_playlist.py
Write-Host "Done! Please commit and push the renamed files to GitHub to update your cloud stream." -ForegroundColor Green
} else {
Write-Host "`nAll files have correct extensions. No renaming needed." -ForegroundColor Green
}