Skip to content

Instantly share code, notes, and snippets.

@geolaw
Last active April 14, 2022 18:18
Show Gist options
  • Save geolaw/d7d7a8044afc6071f37dfc0efc8e93af to your computer and use it in GitHub Desktop.
Save geolaw/d7d7a8044afc6071f37dfc0efc8e93af to your computer and use it in GitHub Desktop.
random music player. Randomizes $MUSIC dir and plays back in mpv
#!/bin/bash
# GEL (C) 2022
# work in progress
# randomizes music playback
IFS="$(printf '\n\t')"
MUSIC="/Music/Music"
cd $MUSIC
back_to_root=0
while [ 1 ]; do
#pwd
# randomize the files/directories in the current directory
dir=$(ls | shuf -n1);
# count the files/directories inside to provide a # tracks
count=$(find $dir -ls |wc -l); # will also contain directories, -t file?
if [ $back_to_root == 1 ]; then
echo -n "play $lastdir -> $dir? ($count) y/n/q > "
else
echo -n "play $dir? ($count) y/n/q > "
fi
read answer
if [ "$answer" == "y" ]; then
# if $count > 10, e.g. artist with many albums, cd into it and then loop back to the top
echo "$dir has $count songs"
# if count > X , cd into $dir and then
if [[ $count -gt 10 && $back_to_root == 0 ]]; then
# cd into $dir and then fall back to the top to select a random album from this artist
cd $dir
lastdir="$dir"
back_to_root=1 # flag to track position
else
# play dir
mpv $dir
# if back_to_root is set, we cd back to the top and restart with another artist
if [ "$back_to_root" == "1" ]; then
cd $MUSIC
back_to_root=0
fi
lastdir=""
fi
else
if [ "$answer" == "q" ]; then
exit;
fi
# else = if they answer n - fall through and re-randomize
fi
done
@geolaw
Copy link
Author

geolaw commented Apr 14, 2022

Sample Run:
$ random_music.sh
play The Police? (30) y/n/q > y
The Police has 30 songs
play Essentials? (17) y/n/q > y
Essentials has 17 songs
[file] This is a directory - adding to playlist.

Playing: Essentials/The Police - Essentials (2018)/01 - Roxanne.mp3
(+) Video --vid=1 [P] (mjpeg 400x400 1.000fps)
(+) Audio --aid=1 (mp3 2ch 44100Hz)
File tags:
Artist: The Police
Album: Essentials
Album_Artist: The Police
Date: 2018
Genre: Rock
Title: Roxanne
Track: 1/15
Displaying cover art. Use --no-audio-display to prevent this.
VO: [gpu] 400x400 yuv444p
AO: [pulse] 44100Hz stereo 2ch float
AV: 00:00:05 / 00:03:13 (3%) Cache: 187s/10MB

Exiting... (Quit)
play Sonata Arctica? (60) y/n/q > n
play Edie Brickell and The New Bohemians? (3) y/n/q > n
play Deep Purple? (11) y/n/q > n
play Hannah Montana? (3) y/n/q > n
play Kc And The Sunshine Band? (3) y/n/q > n
play Tom Petty & The Heartbreakers? (57) y/n/q > y
Tom Petty & The Heartbreakers has 57 songs
play Full Moon Fever? (7) y/n/q > n
play Pack Up The Plantation - Live!? (16) y/n/q > y

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