Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Bash script to autoplay mp3/video files with mplayer
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
FonderiaDigitale
Veteran
Veteran


Joined: 06 Nov 2003
Posts: 1710
Location: Rome, Italy

PostPosted: Mon Jul 05, 2004 2:38 am    Post subject: Bash script to autoplay mp3/video files with mplayer Reply with quote

[EDIT 05.07.2004]: added some checks, cleaned the code.

Versione 0.2

Hi folks,
This is a hardcoded way I created to quickly listen to my collection of music.
I didn't want to install huge apps like rhythmbox and such to do the same thing, instead i've created a script to scan for files under a folder and its children.
this did the trick for me.. hope serves someone :)
Usage (all in a terminal window):
- enter: skips song
- ctrl+c: as above
- double ctrl+c: stops the script.

Code:
#!/bin/sh
# autoplay music on a directory scanning for mp3 files
# shuffling, and using mplayer to stream.
# Author: fonderiadigitale@gechi.it License: GPL2

#set this to the root folder containing your music/video
dir=/dati/samba/Musica

########################################################
ver=0.2

play () {
   #grep -e \(^ Title\|^ Album\|^ \Track\)
          [ -n "$verbose" ] && echo "Shuffling mode selected."
   [ -n "$verbose" ] && echo -e "\nFound ${#list[@]} songs.\n"
   if [ -n "$shuffle" ]; then
      while true; do
            shuffle
            if [ -n "${randfile}" ]; then
               [ -n "$verbose" ] && echo "Playing `basename ${randfile%%.mp3}`.."
               mplayer $randfile >/dev/null 2>&1
            fi
      done
   else
           for file in ${list}; do
         [ -n "$verbose" ] && echo "Playing `basename ${file%%.mp3}`.."
         mplayer ${file} >/dev/null 2>&1
      done
   fi                  
}

shuffle () {
   ll=${#list[@]}
   RANGE=${ll}
   n=${RANDOM:0:3}
   if [ ${n} -le ${ll} ] || [ ${n} -ge ${ll} ]
   then
            export randfile=${list[$n]}   
   else
      shuffle
   fi
}

IFS="
"
cd ${dir}
list=( `ls --color=never -x -R *.mp3` )

########################################################

for opt in $@; do
   case $opt in
    "-h"|"--help")
       echo "Usage: `basename $0` [-s|--shuffle] [-v|--verbose]"
      exit 0
   ;;
    "-s"|"--shuffle")
      shuffle="on"
      shift
   ;;
    "-v"|"--verbose")
       verbose="on"
       shift
   ;;
   esac
done

play

_________________
Come disse un amico, i sistemisti sono un po' come gli artigiani per l'informatica :)
Back to top
View user's profile Send private message
FonderiaDigitale
Veteran
Veteran


Joined: 06 Nov 2003
Posts: 1710
Location: Rome, Italy

PostPosted: Mon Jul 05, 2004 8:01 pm    Post subject: Reply with quote

bumped
_________________
Come disse un amico, i sistemisti sono un po' come gli artigiani per l'informatica :)
Back to top
View user's profile Send private message
aridhol
Guru
Guru


Joined: 20 Jan 2003
Posts: 509
Location: Stockholm, Sweden

PostPosted: Tue Jul 06, 2004 12:37 am    Post subject: Reply with quote

FonderiaDigitale wrote:
bumped


Please don't bump Tips & Tricks.
If people are not interested in your tip then don't keep it on the first page.

Let it slide away into the depth of Tips & Tricks until one day, a Hobbit might find it.

Cheers.
_________________
72 of Pitcairn Islands 49 inhabitants use Seti@Home
"If you buy a DVD you have a copy. If you want a backup copy you buy another one."
"Anyone who is capable of getting themselves made President should on no account be allowed to do the job."
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum