2.27.2010

Songbird killed my BASH script

I used to play music in the terminal. Why is this? iTunes didn't "really" support flac, and it makes an utter mess of my otherwise neatly organized collection. And, until very recently, Songbird failed to play certain mp3s for seemingly no good reason (silly encoding issues). Besides, I enjoy being able to rock right off of the prompt. But no more.

Just as I finished neatly merging a few scripts I had been using all this time, I found the aforementioned encoding issue, unsucking Songbird in what appears to be a fairly permanent way. *sigh* I'm just not elitist or bullheaded enough (most of the time) to cling to the terminal when better options exist.

Nevertheless, if you wish to play a mixture of formats over SSH, this may just be what you're looking for.

#!/bin/bash
#
# FILENAME: playdir.sh
# LAST UPDATE: jds - 02/27/10
# DESCRIPTION: Plays mp3s, flacs, and m4as using mpg321, flac123, and afplay,
# respectively. This was written in OS X, hence afplay. Most
# everything should work under Linux, though.
#

FIND='/usr/bin/find'
shuffle=0
playlist=""
player=""
if [ "$1" == "-s" ]; then
shuffle=1
shift
fi

function chplayer
{
# OCD tidiness where possible
local MPG321='/opt/local/bin/mpg321'
local FLAC123='/usr/local/bin/flac123'
local AFPLAY='/usr/bin/afplay'
local filename="$1"

case "$filename" in
*.mp3)
player="$MPG321";;
*.flac)
player="$FLAC123";;
*.m4a)
player="$AFPLAY";;
esac
}

function shufflelist
{
playlist=$(while read i; do
echo "$RANDOM $i"
done < <(echo "$playlist") | sort | sed -E 's/^[0-9]+ //')
}

function playdir
{
# Choose player and play file.
while read file; do
echo "$file"
chplayer "$file"
${player} "$file" 2>/dev/null 1>/dev/null
done < <(echo "$playlist")
}

# Create playlist
while (( "$#" )); do
dir="$1"
pattern='.*\.mp3|.*\.flac|.*\.m4a'
playlist="`echo \"$playlist\"
${FIND} -Es \"$dir\" -iregex $pattern`"
shift
done
playlist="`echo \"$playlist\" | tail -n +2`" #Chop empty line off top

if [ "$shuffle" -eq "1" ]; then
shufflelist
fi
playdir

3 comments: