Fandom

Music Player Daemon Community Wiki

Hack:addrandom

525pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Use this bit of code to randomly add songs to what is currently playing. Anyone know how to adjust for weighted playlists?



  • Code
!/bin/bash  
#evildick:04:24:2006  
mpddb=/var/lib/mpd/database  
addrandom() {  
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/temp$$  
trap "rm -f $tempfile" 0 1 2 5 15  
tempfile1=`tempfile 2>/dev/null` || tempfile1=/tmp/temp1$$  
trap "rm -f $tempfile1" 0 1 2 5 15  
sed 's/ /^/g' $mpddb > $tempfile  
getdbfile="$(cat $tempfile | grep file | cut -c 7-)"  
dbfile=($getdbfile)  
num_dbfile=${#dbfile[*]}  
echo "${dbfile[$((RANDOM%num_dbfile))]}" > $tempfile  
sed 's/\^/ /g' $tempfile > $tempfile1  
mpc add "$(cat $tempfile1)"  
}  
addrandom




I made a similar script, the main difference is in how we select the songs (array vs. text file + tools), but I made mine so you can pass an integer argument to it and have it add that number of songs. But I have nothing for weighted playlists.



  • Code
#!/bin/bash  
# gunnihinn @ 19.11.2007  
hjalp ()  
{  
    echo "Usage: addRandom [OPTION]... [INTEGER]..."  
    echo "Adds INTEGER numer of randomly selected songs"  
    echo "to the current mpd playlist."  
    echo " "  
    echo "  -h,  --help            Display this help"  
}  
# Make sure the input argument is an integer.   
# Set number $num of songs to be added.  
if [ $# -eq 0 ]; then  
    hjalp  
    exit  
elif [ $1 = "--help" -o $1 = "-h" ]; then  
    hjalp  
    exit  
else  
    if [ `echo $1 | grep [^0-9]` ]; then  
	echo "Error: Argument not an integer. Exiting."  
	exit  
    else  
	num=$1  
    fi  
fi  
# Get all song file names in database.   
# We borrow a file in /tmp  
mpc listall > /tmp/songs.tmp  
numSongs=`wc -l /tmp/songs.tmp | sed "s/ \/tmp\/songs.tmp//"`   
if [ $numSongs -eq 0 ]; then  
    echo "Error: No songs in database."   
    rm /tmp/songs.tmp  
    exit  
fi  
# Add all songs if $num is too big  
if [ $num -gt $numSongs ]; then  
    num=$numsongs  
fi  
for (( i = 1; i <= $num; i++ ))  
do  
    songnr=`expr 1 + $RANDOM % $numSongs`  
    song=`cat /tmp/songs.tmp | head -n $songnr | tail -n 1`  
    # Don't add the same song twice, fix number of songs  
    mpc add "$song" && sed -i "\^$song^d" /tmp/songs.tmp && numSongs=`expr $numSongs - 1`  
done  
# Make it squeaky clean  
rm /tmp/songs.tmp




If you simply want 10 random songs from you library and care about efficiency too:
  • Code
mpc listall | shuf | head -n 10 | mpc add

I have a couple of oneliners.


The first one outputs single random song from mpd database:


  • Code
!/bin/bash  
mpc listall | sed -n $[RANDOM % $(mpc stats | grep Songs | awk '{print $2}')+1]p


The second one adds single random song from mpd database to playlist:


  • Code
mpc_add_random: aliased to ~/bin/mpc_random | xargs -I{} mpc add {}


Then you can add multiple random songs with something like this:


  • Code
for i in `seq 100` ; do mpc_add_random ; done


I took the one-liner above and made this shell script, to add 10 tracks to my playlist. I call it from cron.

  • Code
#!/bin/bash
#Add 10 random tracks to mpd playlist. Adjust LIMIT to increase/decrease.
var0=0
LIMIT=10
while [ "$var0" -lt "$LIMIT" ]
do
 # List the whole library, and randomly select one item, then do some string manipulation with grep and awk) and finally pipe result to mpc add.
 mpc listall | sed -n $[RANDOM % $(mpc stats | grep Songs | awk '{print $2}')+1]p | mpc add
 # Increment
  var0=`expr $var0 + 1` 
done
echo
exit 0

--90.214.240.41 20:13, January 22, 2010 (UTC) Toby Newman


A small script that adds $LIMIT songs to the playlist if it contains less then $LIMIT songs. This can be added to the crontab of your mpd server.

  • Code
#!/bin/bash
LIMIT=10

MPC='/usr/bin/mpc -h password@localhost'
if [ `$MPC playlist | wc -l` -lt $LIMIT ]; then
  $MPC listall | shuf -n $LIMIT | while read LINE; do $MPC add "$LINE"; done
fi;

--82.197.206.170 15:49, February 18, 2013 (UTC)

Added the functionality to select from all songs or just from a subfolder by passing the subfolder, also the script waits for a song to end and then goes to add a new one if the playlist is shorter then LIMIT (this only works if consume is on, otherwise just remove the while [ "$var0" -lt "$LIMIT" ] loop.

To use this you would have to edit the directory where your music is in. Remember that the base folder for any mpc command is not your home folder but the music library of mpd.

  • Code
#!/bin/bash
var0=$1
folder="${var0// /\\ }"
directory="MusicCollection/Music/"${folder}
var0=`mpc playlist | wc -l`
LIMIT=10
while true; do
       var0=`mpc playlist | wc -l`
       while [ "$var0" -lt "$LIMIT" ]
       do
               if [ -z "$1" ]
               then
                       mpc listall | sed -n $[RANDOM % $(mpc stats | grep Songs | awk '{print $2}')+1]p | mpc add
               else
                       eval mpc listall ${directory} | sed -n $[RANDOM % $(eval mpc listall ${directory} | wc -l)+1]p | mpc add
               fi
               var0=`mpc playlist | wc -l`
       done
       mpc current --wait
done

Also on Fandom

Random Wiki