Skip to content

Instantly share code, notes, and snippets.

@kidsil
Created November 16, 2023 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kidsil/9ef21af57d3dfc754728b77634b0bb87 to your computer and use it in GitHub Desktop.
Save kidsil/9ef21af57d3dfc754728b77634b0bb87 to your computer and use it in GitHub Desktop.
Cmus create shuffled albums playlist from your library
#!/bin/bash
# Album limit
ALBUM_LIMIT=10
# Get latest library from cmus
cmus-remote -C "save -l $(pwd)/cmus-library.pl"
# Use awk to group songs by album
awk -F'/' '{
album = $(NF-1);
songs[album] = songs[album] $0 "\n"
}
END {
for (a in songs) {
print "BEGIN_ALBUM:" a "\n" songs[a] "END_ALBUM"
}
}' "./cmus-library.pl" > "./grouped-albums.pl"
# Shuffle the albums and limit to $ALBUM_LIMIT
awk -v album_limit="$ALBUM_LIMIT" 'BEGIN {
srand();
RS="END_ALBUM";
ORS=""
}
{
albums[NR] = $0 "END_ALBUM"
}
END {
for (i=1; i<=album_limit; i++) {
rand_idx = int(1 + rand() * NR);
temp = albums[i];
albums[i] = albums[rand_idx];
albums[rand_idx] = temp;
}
for (i=1; i<=album_limit; i++) {
gsub(/BEGIN_ALBUM:[^\n]*\n/, "", albums[i]);
gsub(/END_ALBUM/, "", albums[i]);
gsub(/\n+/, "\n", albums[i]);
print albums[i]
}
}' "./grouped-albums.pl" > "./shuffled-albums.pl"
# Remove empty lines from the shuffled-albums.pl file
sed -i '/^$/d' "./shuffled-albums.pl"
# Add playlist to cmus
cmus-remote -C "pl-import $(pwd)/shuffled-albums.pl"
# Gentle clean up
gio trash shuffled-albums.pl grouped-albums.pl cmus-library.pl
@kidsil
Copy link
Author

kidsil commented Nov 16, 2023

This took some tinkering, but essentially it successfully takes the existing CMUS library, and creates a shuffled album playlist.
Those of us who remember Rockbox - that's a feature that was available there and here I'm trying to recreate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment