#!/bin/bash # # script to play mp3's in a random order # April 2004. Pete Nesbitt # updated Dec 2006 (find player) # PROG="mp3_player.sh" ############### # player and string get_player() { # if realplayer is available, use it: which realplay &> /dev/null RET_RPLAY=$? if [ $RET_RPLAY -eq 0 ];then # realplayer PLAY_TYPE="real" PLAYER="realplay -q" else which mplayer &> /dev/null RET_MPLAYER=$? if [ $RET_MPLAYER -eq 0 ];then # mplayer PLAY_TYPE="mplay" PLAYER="mplayer -playlist" else echo echo "$PROG FAILED" echo " could not find either mplayer or realplay" exit 1 fi fi } ############### clear rm -f /tmp/playlist.tmp /tmp/playlist.clean /tmp/playlist.random /tmp/play.txt /tmp/playlist.txt &> /dev/null touch /tmp/playlist.tmp /tmp/playlist.clean /tmp/playlist.random /tmp/play.txt /tmp/playlist.txt PLAYLIST="/tmp/playlist.tmp" CLEANLIST="/tmp/playlist.clean" RAN_LIST="/tmp/playlist.random" PLAY="/tmp/play.txt" FINAL_LIST="/tmp/playlist.txt" # use dir as arg else use current if [ "$#" -eq "0" ] ; then # use current dir echo "`ls -1`" > $PLAYLIST ; else for i in $1 $2 $3 do echo "`ls -1 $i`" >> $PLAY while read SHORTNAME do LONG_NAME=${i}${SHORTNAME} echo $LONG_NAME >> $PLAYLIST done < $PLAY done fi grep -v ^$0 $PLAYLIST >> $CLEANLIST #get dir listing and randomize contents while read SONG do # validate all file names & check type if [ -n "`file $SONG|cut -d: -f2|egrep -i 'mp3|mpeg'`" ] ; then # if mp3, add to list echo "${RANDOM}.${SONG}" >> $RAN_LIST fi done < $CLEANLIST sort -n $RAN_LIST |cut -d. -f2,3,4,5 >> $FINAL_LIST #### PLAYERS ################ get_player if [ "$PLAY_TYPE" = "real" ];then # for RealPlayer (plays one song only): echo echo " Playing:" while read EACH_SONG; do echo " `echo $EACH_SONG|sed -e s/.mp3$//|sed -e s/-/:__/g|sed -e s/_/\ /g`" $PLAYER $EACH_SONG done < $FINAL_LIST else # for mplayer (reads a list) $PLAYER $FINAL_LIST fi exit # eof