Thursday, May 29, 2008

BASH Playlist handler and mpg123 interface

My First "somewhat" USeful BASH script
This small script scans your / directory and generates a playlist of mp3 files, and you can play your file by ./juke . You will get a list of songs matching your criteria.

Installation
Copy the source code,given below, to a new file named juke


# apt-get install mpg123
$ chmod 755 juke
$ ./juke west


Sample output and Controls

arun@XiO3:~/bash$ ./juke happy
1 happy birthday
2 Avril Lavigne - My Happy Ending
3 07_-_Happy_Days_-_Happy_Days
4 manasinu marayilla
Enter song number [MAX 4]: 2
./juke: line 13: [: -g: binary operator expected
Playing /media/disk/Songs/English/avril lavigne/Avril Lavigne - My Happy Ending.mp3
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
version 0.67; written and copyright by Michael Hipp and others
free software (LGPL/GPL) without any warranty but with best wishes

Directory: /media/disk/Songs/English/avril lavigne/
Playing MPEG stream 1 of 1: Avril Lavigne - My Happy Ending.mp3 ...
Title: My Happy Ending Artist: Avril Lavigne
Comment: Album: Under My Skin
Year: 2004 Genre: Rock
MPEG 1.0, Layer: III, Freq: 44100, mode: Stereo, modext: 0, BPF : 626
Channels: 2, copyright: No, original: Yes, CRC: No, emphasis: 0.
Bitrate: 192 kbits/s Extension value: 0
Audio: 1:1 conversion, rate: 44100, encoding: signed 16 bit, channels: 2
Frame# 520 [ 8762], Time: 00:13.58 [03:48.88], RVA: off, Vol: 100(100)

-= terminal control keys =-
[s] or space bar interrupt/restart playback (i.e. 'pause')
[f] next track
[b] back to beginning of track
[p] pause while looping current sound chunk
[.] forward
[,] rewind
[:] fast forward
[;] fast rewind
[>] fine forward
[<] fine rewind
[+] volume up
[-] volume down
[r] RVA switch
[v] verbose switch
[h] this help
[q] quit



v1.2
Usage
./juke <keywords separated by , (comma)>
input indexes separated by space

Source

#!/bin/bash
if [ ! -e mp3list.txt ]; then
echo "Listing all mp3 on your / "
find / -name *.mp3 > mp3list.txt
fi

echo -n > tpl.tmp
i=1
while true; do
l=`echo $1, | cut -d"," -f$i`
let i=i+1
if [[ -z "$l" ]]; then
break
fi
`cat mp3list.txt | grep -i $l >> tpl.tmp`
done



sed = tpl.tmp | sed 'N;s/\n/\t/;s/\/.*\///g;s/.mp3//' | more # number lines and strip of directories and extensions.
line=1;
max=`wc -l tpl.tmp | cut -s -d" " -f1`
if [ ! "$max" -eq 0 ] && [ ! "$max" -eq 1 ]
then
echo -n "Enter song number [MAX $max]: ";
read line
elif [ "$max" -eq 0 ]; then
exit 0
fi

for i in `seq 1 10`;
do
l=`echo "$line 0" | cut -s -d" " -f$i`
echo "Playin : $l";
if [ -z "$l" ] || [ "$l" -le 0 ]; then
exit 0 #Exit here
fi

filename=`more +$l tpl.tmp | head -n 1`
#echo "$filename"
if [ "$l" -gt "$max" ]; then
echo "Out of bounds $max "
else echo "Playing $filename"
mpg123 -Cv "$filename"
fi

done

echo "OveR"; #doesnt reach


Updates
+ Added multiple search and concatenation
+ Queue more than one mp3

v1.1
Source


#!/bin/bash
# v 1.1
if [ ! -e mp3list.txt ]; then
echo "Listing all mp3 on your / "
find / -name *.mp3 > mp3list.txt
fi
`cat mp3list.txt | grep -i $1 > tpl.tmp`
sed = tpl.tmp | sed 'N;s/\n/\t/;s/\/.*\///g;s/.mp3//' | more # number lines and strip of directories and extensions.
max=`wc -l tpl.tmp | cut -d" " -f1`
if [ ! "$max" -eq 0 ] && [ ! "$max" -eq 1]
then
echo -n "Enter song number [MAX $max]: ";
read line
elif [ "$max" -eq 0 ]; then
exit 0
fi

filename=`more +$line tpl.tmp | head -n 1`

if [ $line -gt $max ]; then echo "Out of bounds $max "
else echo "Playing $filename"
mpg123 -Cv "$filename" -E like.eq
fi

echo "OveR";


Equalizer Sample like.eq

2.5 2.5
2.0 2.0
2.0 2.0
2.0 2.0
2.0 2.0
2.0 2.0
2.0 2.0
1.9 1.9
1.9 1.9
1.9 1.9
1.9 1.9
1.9 1.9
1.0 1.0
1.0 1.0
1.0 1.0
1.0 1.0
1.0 1.0
1.0 1.0
2.4 2.4
2.5 2.5
2.5 2.5
2.5 2.5
2.5 2.5
2.6 2.6
2.7 2.7
2.7 2.7
2.8 2.8
2.9 2.9
3.0 3.0
3.0 3.0


Updates


  • 1.1 :
    + Added equalizer file "like.eq"
    + Removed input if only 1 selection
    + Exits automatically if no matches

  • 1.0 : Everything new

0 comments: