syntax highlight

Thursday, 23 July 2009

mocp rand

I'm quite sure everyone reading this must have a respectable, if not massive, music collection. In this days and age is difficult finding someone who doesn't. It's also difficult to choose one, and only one, disk to listen at any given moment. Until we're upgraded to support concurrent music listening we're better of with a random disk selector, which is exactly what this little script does:

#!/bin/bash

SEARCH_DIR="/home/nico/Música"
START_RANDOM=1
RAND_MAX=32767

while (( 1 )); do
  NUM_DISCS=$(find $SEARCH_DIR -type d | wc -l)
  RAND=$(($NUM_DISCS * $RANDOM / $RAND_MAX))
  RAND_DISC=$(find $SEARCH_DIR -type d | head -n $RAND | tail -n 1)

  # Wake up moc
  mocp -FS 2>/dev/null >/dev/null &
  mocp -pca "$RAND_DISC" &
  echo "Playing $RAND_DISC"

  # Start from a random file?
  if (( $START_RANDOM )); then
    mocp --on shuffle &
    mocp -f &
    mocp --off shuffle &
  fi

  read
done

Of course, it requires mocp, my favorite music (on console) player. And obviously, you'll have to configure SEARCH_DIR but I'm sure some bash hacking is not that hard.

Beware though, using this + cron may have the undesired effect of awakening to the pleasant music of Cannibal Corpse.

1 comment:

  1. [...] And now you can sort by random your output using sort -R. Why would this be useful? Well, I updated my mocp random script with a [...]

    ReplyDelete