Published on [Permalink]
Reading time: 4 minutes
Posted in:

Randomized Albums in Track Order on Sonos (and Jellyfin)

Most music platforms focus on individual songs and not albums. I’m a completionist and love to  listen to full albums. But most times I end up paralyzed above what album to listen to. So whenever possible, I listen to a random sort order of albums that are in track order. Sonos software and equipment do not make this easy. So I’ve resorted to a semi-manual system to create M3U files with shuffled albums in track order.

Sonos music libraries, sourced from a local network share, can import M3U files. M3U files are list of files. If you store M3U files at the root of your Sonos music library, you can use relative paths and Sonos will usually be able to find everything. My music library is structured as /Artist Name/Album/track#_song_title.flac. I’ve cleaned out special characters from file names such as diacritics using the fantastic macOS app Transnomino. Diacritics can really mess up some scripts. Turns out this folder structure is also a good choice for the JellyFin media server.

Once you have a good folder structure, then my manual process is ready to fire off. It requires a *nix operating system, the Python mkpl script for making M3U playlists, and your choice of way to randomize text lines. Which you can definitely do in the shell but I prefer the macOS app bbedit.

First I generate a comprehensive list of album folders using this cobbled together shell script. I probably got most of the code from searching Stack Overflow, since I often find bash scripts incomprehensible. This script makes an assumption that the list of folders will be used for a final command line tool so paths are quoted and have a \ for script line continuation. I assume you understand how to use bash scripts. Careful though, different versions of sed can do different things so maybe you’ll have fun with that.

#!/bin/bash

if [[ -f albums.txt ]]; then
    rm albums.txt
fi

find . -maxdepth 2 -type d | while read folder; do
    if [[ $folder == ./*/* ]]; then
        echo "$folder\" \\" >> albums.txt
    fi
done

# Remove the first two characters of each line, should be "./"
sed -i'.bak' -e 's/..//' albums.txt

# Insert a " at the beginning of each line where \1 is the matched group 
sed -i'.bak' -e "s/\(.*\)/\"\1/" albums.txt

I then review the output to fix any obvious errors.

Then, I make a quick bash script that makes use of the Python script mkpl. Here’s an example:

#!/bin/bash
today=`date '+%Y%m%d'`;
filename="Shuff_Albums_$today.m3u";

mkpl -d \
"Wings/Band On The Run" \
"The Jesus and Mary Chain/Hate Rock 'n' Roll" \
"Cat Power/You Are Free" \
"The Wrens/The Meadowlands" \
"The Housemartins/London 0 Hull 4" \
"Stereolab/Mars Audiac Quintet" \
"The Jesus and Mary Chain/Stoned And Dethroned" \
"Catherine Wheel/Chrome" \
"Blind Pilot/3 Rounds And A Sound" \
-g "UNICODE" -o $filename;

mkpl -o means use track order. It probably helps that I use track numbers at the front of each song filename, as it’s likely not all song metadata includes track data.

Then, I use bbedit’s Text->Sort Lines->Randomize Order (Sorted Lines replace selection) upon the selected folder list. Once the albums are randomize, I then decide if I have any new albums I want at the top of the playlist. Then I run the final random album M3U script, with the final M3U file at the root of the library, and have Sonos update the music library. Done.

My operational script also makes M3Us for recently bought albums and a time-limited shuffled track playlist for driving and the office. The mkpl script is very handy with all sorts of options for playlists. Those additional M3Us are generally meant for my JellyFin media server, which happily imports these M3Us too! Manet is my favored iOS app for using my self-hosted Jellyfin server. Jellyfin uses the same media library as Sonos.

A problem for me is, the Sonos app on iOS still cannot go to the currently playing song in the playlist view. Scrolling through long playlists in the iOS Sonos App is awful. But the macOS Sonos app does go to the current playlist position and it is only slightly less awful to scroll through.

Hope this helps somebody.

Mastodon.

All photos on this website, unless stated otherwise, are Ryan Mikulovsky's and are Copyright 2025. All Rights Reserved.