Wikia

Music Player Daemon Community Wiki

Hack:Mpdalbum

525pages on
this wiki
Talk0

mpdalbum is a shell script able to automatically fill your playlist with full albums and with some other nice features.

Source code Edit

#!/bin/bash

# Script Name: mpdalbum v0.4
# Author: pankkake [pankkake at headfucking dot net]
# License: BSD
# Requirements: mpc and mpd  (http://musicpd.org/)

db=/var/lib/mpd/mpdalbum.db
pid=/var/lib/mpd/mpdalbum.pid

# please include the trailing slash
dir="/home/pankkake/Music/Main/"
dirl=`expr length $dir`
dirl=$(($dirl+1))

if [ ! -f `which mpc` ]
then
echo "ERROR : mpc not installed"
exit 1
fi

if [ ! -d $dir ]
then
echo "ERROR : directory"
exit 1
fi

case $1 in
   "start" )
      mpc random off > /dev/null 2>&1
      mpc repeat off > /dev/null 2>&1
      if [ "$2" != "noup" ]
      then
         $0 update
      fi
      
      echo 'Starting mpdalbum in background...'
      if [ -f $pid ]
      then
         echo 'Already started.'
      else
         nice $0 _watch &disown
         echo 'Done.'
      fi
      ;;

   "stop" )
      echo 'Stopping mpdalbum...'
      if [ -f $pid ]
      then
         pidnum=`cat $pid`
         echo "PID=$pidnum"
         rm $pid
         
         # watchpid is not required but recommended
         if [ -f `which watchpid` ]
         then
            echo 'Waiting for mpdalbum to exit...'
            echo 'C-c to kill it without waiting'
            watchpid -d1 $pidnum
         fi
         echo 'Done.'
      else
         echo 'Already stopped.'
      fi
      ;;


   "status" )
      echo -n "Status: "
      if [ -f $pid ]
      then
         echo "Running."
         ps --pid `cat $pid` -o pid=,state=,pcpu=
      else
         echo "Not running."
      fi
      ;;

   "restart" )
      $0 stop
      $0 start noup
      ;;

   "update" )
      if [ "$2" != "silent" ]
      then
	      echo 'Updating mpd db...'
	  fi
      mpc update > /dev/null 2>&1
      while [ ! -z "`mpc | grep "Updating DB"`" ]
      do
         sleep 3
      done
      
      if [ "$2" != "silent" ]
      then
         echo 'Updating mpdalbum db...'
      fi
      if [ -f $db.t ]
      then
         rm $db.t > /dev/null
      fi
      find "$dir" -type d > $db.t
      
      rm $db > /dev/null
      mv $db.t $db
      
      
      ;;

   "playlist" )
      mpc playlist --format "[%artist% - %album%]|(unknown)" \
      | cut -d" " -f2- | sort -mu
      ;;

   "file" )
   	  echo $dir`mpc --format "%file%" | head -n1`
      ;;
      
   "info" )
      mpc --format "[%artist% - %album% : %track%. %title%]|%file%" \
      | head -n1
      ;;
   
   "ss" )
      $0 smartstop $2
      ;;
   
   "smartstop" )
      i=1
      
      if [ -z $2 ]
      then
        tracks=1
      else
        tracks=$2
      fi
      
      while [ $i -le $tracks ]
      do

        file="`$0 file`"
        echo "Waiting for song $i to change..."
        while [ "`$0 file`"  = "$file" ]
        do
      	  sleep 0.2
        done

        i=$(($i+1))
      done
      
      echo "Cleaning playlist..."
      mpc pause > /dev/null 2>&1
      $0 clean
      mpc stop > /dev/null 2>&1
      echo "Smart-stopped after $tracks tracks."

      ;;
      
   "clean" )
      # deleting played songs, if playing/paused
      if [ `mpc | wc -l` -eq '3' ]
      then
          while [ `mpc | head -n2 | tail -n1 | \
           cut -d"#" -f2 | cut -d"/" -f1` -gt 1 ]
          do
             mpc del 1 > /dev/null 2>&1
          done
      fi
      ;;

   "_watch" )
      # force timer activation at start
      timer=999
      
      echo $$ > $pid
      while [ -f $pid ]
      do
         sleep 5
         timer=$(($timer+1))
         
         if [ $timer -gt 19 ]
         then
            timer=0
            
            # cleaning playlist
            $0 clean
            
            # feeding playlist
            if [ `mpc playlist | wc -l` -lt 130 ]
            then
            
               lines=`cat $db | wc -l`
               while [ `mpc playlist | wc -l` -lt 160 ]
               do
                  diradd=`head $db -n$(((RANDOM%lines)+1)) | tail -n1`
                  diradd=`expr substr "$diradd" $dirl 999`
                  # make sure there are ogg files and no directories
                  if [ \( 0 -lt `ls -f "$dir$diradd" | grep .ogg | wc -l` \) \
                  -a \( 0 -eq `find "$dir$diradd" -type d -maxdepth 1 \
                  -mindepth 1 | wc -l` \) ]
                  then
                     mpc add "$diradd" > /dev/null 2>&1
                     sleep 2
                  fi

               done
               
            fi
            
         fi
         
         
      done
      ;;

   * )
      echo "Usage: $0 command [options]"
      echo "  start [noup]"
      echo "  stop"
      echo "  status"
      echo "  update [silent]"
      echo "  info"
      echo "  file"
      echo "  playlist"
      echo "  smartstop (alias: ss)"
      # nextalbum ?
esac

Around Wikia's network

Random Wiki