The world is my backyard.
Random header image... Refresh for more!

Jukebox in a single line

If you are like me, who starts starts iTunes with default library and realize you have heard the same song 8 times in the last week, here are two one-line-Jukebox scripts for you. The scripts randomly choose and play songs right from the command line.

Before using the script make sure you have an mp3 player installed. I have installed lightweight mpg123, however you can use any player. I would recommend mplayer for Linux.

While running the scripts, I pass ~/Music folder argument. Change the path to point to folder where you store your mp3s.

First one is in Ruby, and shamelessly inspired by Kevin Baird’s book.

Open a text editor and save this script. I named it shuffle_play.rb. (You need to have ruby installed on your system. Installed on Leopard and most Linux distributions by default.)

#!/usr/bin/env ruby -w
ARGV.sort_by{ rand }.each {|file| system("mpg123 \"#{file}\"")}

To run the jukebox

ruby shuffle_play.rb ~/Music/*.mp3

For the second, I ported above script to bash, which is even more simple and can run on any Linux or OS X. Lets name the script shuffle_play_shell.sh

#!/usr/bin/env bash
n=0;for songs in "$@" ; do song_array[n]=$songs ;((n++)) ; done; for ((i=0;i<$n;i+=1));do number=$RANDOM; let "number %= $n"; `mpg123 "${song_array[number]}"`; done

To run the jukebox

source shuffle_play_shell.sh ~/Music/*.mp3

Update: The second one is not completely random. It will choose n random songs out of n songs. However it does not checks if each song is played at-least once, thus chances of songs getting repeated. Any suggestions?

June 28, 2008   No Comments

Installing command line MP3 player

Mpg 123 Player

mpg123 is a real time MPEG 1.0/2.0/2.5 audio player for layers 1,2 and 3.

I was playing with Ruby scripts and needed a command line mp3 player which was not a resource hog.

Steps I took for my OS X (Should work on Linux/Unix box):

Download the latest version from the website.

Untar the archive

tar -xvf mpg123-1.4.3.tar

Compile and install the player

./configure --prefix=/usr/local/mpg123
make
make install

Add the path to .bash_profile

echo 'export PATH=/usr/local/mpg123/bin:$PATH' >> ~/.bash_profile

Load the new path

source ~/.bash_profile

Enjoy playing music from command-line!

 mpg123 ~/Music/Bagho_Bagh_Gippy_Grewal.mp3

June 28, 2008   2 Comments