I’ve been having issues with playlists exported from iTunes, where any songs with special characters in the filename wouldn’t play in the playlist, despite playing fine when playing through my library within Sonos. Support weren’t unable to resolve this, their solution was to export as .xml instead of .m3u or .m3u8 - from what I can see it doesn’t even support .xml playlists.
Anyway I came across this manual workaround within these forums, which did solve the problem, however I have quite a few playlists, with a number of problem tracks in each, I also export regularly to pick up any updates to the playlists so manually editing every time would be a nightmare.
I’ve ended up writing a PowerShell script to fix all of my playlists with one click. I saw a request elsewhere for a script to do this so posting it here in case it is of use to anyone. Simple copy the script to a new file and save as type .ps1, editing the three variables at the top as per your own structure.
If your playlist path is a network path you can run this from any machine that has access to it, otherwise you will need to copy the script to the machine where the playlist folder resides. Then right click the file and select “Run with Powershell”.
This is probably only useful on Windows, unless you have Powershell installed on another OS. I’m not sure how that will work out of the box though, with the .Net assembly required for UrlEncoding.
#The following two variables are dependant upon your folder structure
#For example my music library physically lives in "D:\John\Music" on the server"
#And the network path to the same folder is "\\MY-SERVER\MediaServer\Music"
#So these variables should be populated with only the parts of the root path that differ
#In this case everthing before "\Music" in the paths
$localRoot = 'D:\John'
$networkRoot = '\\MY-SERVER\MediaServer'
#Path to the location of your exported playlists
$playlistPath = 'D:\John\Music\Playlists\'
#####################################################################
######## You shouldn't need to edit anything below here ########
#####################################################################
Add-Type -AssemblyName System.Web
$localRoot = $localRoot -replace '\\', '\\'
$playlistFiles = Get-ChildItem $playlistPath
foreach($playlistFile in $playlistFiles)
{
$filePathFull = $playlistPath + $playlistFile
$playlistContent = Get-Content $filePathFull
$playlistContent = $playlistContent -replace $localRoot, $networkRoot
$parsedContent = @()
foreach($line in $playlistContent)
{
if ($line -like "*" + $networkRoot + "*")
{
$parsedLine = 'x-file-cifs:' + LSystem.Web.HTTPUtility]::UrlEncode($line) -replace '%5c', '/'
$parsedContent += $parsedLine
}
else
{
$parsedContent += $line
}
}
Set-Content -Path $filePathFull -Value $parsedContent
}