Yet, another one. Made a few changes to the previous one.
- #!/bin/bash
- ###################################################
- # Inspired by the idea of Apple Ipod Shuffle
- # This script makes playing music easier than ever before
- ###################################################
- #you can use another music player like: xmms, amarok...
- player=beep-media-player
- #play how many songs once
- num=5
- #the playlist generated dynamicly each time
- playlist=$HOME/.ipod_playlist.m3u
- tmplist=$HOME/.ipod_playlist.m3u.tmp
- oldlist=$HOME/.ipod_playlist.m3u.recent
- #your music library path
- #lib=$HOME/music
- lib=$HOME
- # This function generate a random m3u playlist
- Genlist()
- {
- # fill $num songs in the playlist in random
- fill=1
- while [ $fill -le $num ]
- do
- #generate random number to choose songs
- choose=$(($RANDOM % $all + 1))
- #check if the song has been added already, compare should return nothing if not added
- current=`head -$choose $tmplist | tail -1`
- compare=`grep "$current" $playlist`
- #check if the song has been played recently, compare should be empty if not
- compare2=`grep "$current" $oldlist`
- # put the selected songs in the playlist and mark it as being played recently
- if [ "$compare" = "" ] && [ "$compare2" = "" ]
- then
- echo "#EXTM3U $fill" >>$playlist
- echo $current >> $playlist
- echo $current >> $oldlist
- fill=$(($fill + 1))
- fi
- done
- }
- #search mp3 in user's music library and put them all in the tmp playlist
- #if you find it is unneccessary to search them everytime, just search at some time you like
- find "$lib" -name *[Mm][pP]3 >$tmplist
- #you can add other music types here like ogg, flac
- #find "$lib" -name *[Oo][Gg][Gg] >>$tmplist
- #calculate how many songs found in all
- all=`wc -l $tmplist`
- all=${all%% *}
- # if no songs found, nothing to do but exit
- if [ $all -eq 0 ]
- then
- exit 1
- fi
- #check the old playlist
- if [ ! -e $oldlist ]
- then
- touch $oldlist
- fi
- #calculate how many songs have been plalyed recently
- old=`wc -l $oldlist`
- old=${old%% *}
- #refresh the playlist first
- if [ -e $playlist ]
- then
- rm -f $playlist
- fi
- touch $playlist
- #make a new playlist
- if [ $all -le $num ]
- # the number of songs are not more than $num, just play them all
- then
- num=$all
- Genlist
- elif [ $(($all - $old)) -lt $num ]
- then
- # a circle will be complete this time, play the left ones first
- total=$num
- num=$(($all - $old))
- Genlist
- #rewrite the recently played list
- echo "`tail -$num $oldlist`" >$oldlist
- num=$(($total - $num))
- Genlist
- else
- Genlist
- fi
- #call your player to play it at the background
- $player $playlist &
复制代码 |