PDA

View Full Version : Shuffling, MP3 playing bash (or python) script



rogier.de.groot
August 12th, 2008, 12:10 PM
Since I'm not much of a bash-programmer, I need some advise for a pet project. I've got a server machine which runs both samba and apache. It's samba share has a folder full of MP3's in it (ordered with several levels of subdirectories). I know I can use mpg123 to play mp3's from the CLI, so I figure it should be possible to write a bash script that picks an mp3 file from my samba share at random, plays it using mpg123 and then repeats. But I don't know how to pick a random file using bash.
Ideally this little script would also set itself and child processes to run at low privilege so it won't interfere with apache on occassional peaks. Don't know how to do that either though.
I guess doing it in python might be a good alternative too, although I do believe the bash version would use less resources.

Anybody got some good suggestions?

aloshbennett
August 12th, 2008, 01:47 PM
You could roughly do it like this
1. mount the samba share
2. get the list of files into an array
3. use $RANDOM (a function under bash) to generate a random number. It returns from 0 to 32767.
4. normalize it against the array indexes
5 happy listening with mpg123 :)

ghostdog74
August 12th, 2008, 04:24 PM
check the mpg123 help documentation and see if there are any shuffle options.
I believe something like this: mpg123 --shuffle --list
no need to create your own shuffle routine