ashmusic -commandline music player

Audio editors, music players, video players, burning software, etc.
Post Reply
Message
Author
User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

ashmusic -commandline music player

#1 Post by 01micko »

As seen on raffy's Puppy Help forum I made a script I call ashmusic

This is the second version with added directory support and the duration of the current track is displayed.

It may be a little heavy on resources with an older machine but the tradeoff is the number of formats it supports, it supports whatever your version of ffmpeg supports. Ffmpeg has been in all mainstream puppies for a long time. It may not be in heavily cut down puplets.

It uses the Pmusic engine by transcoding the file to .au format and piping to aplay. It works equally as well outside of X.. if anyone actually does that anymore! Should also run on any *nix.

You can create custom playlists and also a directory once played is stored as a playlist.

It handles spaces in filenames as long as you wrap the path in quotes, single or double.

Code: Select all

# ashmusic
ashmusic-0.2 GPL3
Usage:
example: ashmusic "/path/to/music file can have spaces.mp3"
Options:
	-h	[display this help]
	-a <track> [add to playlist -track plays]
	-b <track> [add to playlist -track doesn't play]
	-d <directory> [add a whole directory as playlist]
	-l <name>(optional) [load playlist <name>]
You can use "ctrl - c" to quit at any time.
<-- Supports whatever audio format your ffmpeg supports

Code: Select all

# ashmusic -l
angels
Judas_Priest
MCR
Sad_Wings_of_Destiny
The_Black_Parade
Type the name of a playlist to load from the above list
The_Black_Parade
Tracklist:
01 The End..mp3
02 Dead!.mp3
03 This Is How I Disappear.mp3
04 The Sharpest Lives.mp3
05 Welcome to the Black Parade.mp3
06 I Don't Love You.mp3
07 House of Wolves.mp3
08 Cancer.mp3
09 Mama.mp3
10 Sleep.mp3
11 Teenagers.mp3
12 Disenchanted.mp3

Now playing: 01 The End..mp3
Press "q" to skip
Duration:00:01:53
finished!
Now playing: 02 Dead!.mp3
Press "q" to skip
Duration:00:03:15
Have fun!
Attachments
ashmusic-0.2.pet
(1.92 KiB) Downloaded 573 times
Puppy Linux Blog - contact me for access

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#2 Post by technosaurus »

you can also pipe .au to /dev/audio and .wav to /dev/dsp iirc in case aspell is missing
there are a couple of other formats that work this way too
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#3 Post by 01micko »

Tried piping to /dev/audio, didn't work.

Anyway, I implemented a feature I wanted, time progress of track. You'll see the code in the 'progressfunc'. Had to put in some weird filters for some garbage. I got the idea from Paul Colby.

The echo method is ok, but there is the tiniest of lag and sometimes you can just see the back spaces. The printf method seems flawless.

Code: Select all

#!/bin/ash
#simple music player
#01micko 20111126
#GPLv3 see /usr/share/doc/legal
# (c) Mick Amadio 2011-2012
#depends <-- ffmpeg, alsa (aplay)
#v0.2 111128
#v0.3 120102

set -e #stop on all errors
#workdir
[ ! -d $HOME/.ashmusic ] && mkdir $HOME/.ashmusic
HOMEDIR="$HOME/.ashmusic"

#usage
usagefunc(){
echo "ashmusic-0.3 GPL3
Usage:
example: ashmusic \"/path/to/music file can have spaces.mp3\"
Options:
	-h	[display this help]
	-a <track> [add to playlist -track plays]
	-b <track> [add to playlist -track doesn't play]
	-d <directory> [add a whole directory as playlist]
	-l <name>(optional) [load playlist <name>]
You can use \"ctrl - c\" to quit at any time.
<-- Supports whatever audio format your ffmpeg supports" && exit 0
}

[ ! "$1" ] && usagefunc

[ $(pidof aplay) ] && echo 'ERROR: can only have 1 instance' && exit 1

#duration
durfunc(){
sleep 1
DUR=$(grep -wE "Duration" /tmp/ashmusic_ffmpeg|tr -d ' '|head -c17) 
echo
[ "$DUR" ] && echo "$DUR"
}

#progress #this is slow
progressfunc(){
sleep 1
while [ -f /tmp/ashmusic_ffmpeg ]; do
 PROGRESS=$(tail -n1 /tmp/ashmusic_ffmpeg|sed -e 's%.*time\=%%'\
 -e 's%\..*%%')
 [ "`echo $PROGRESS|grep "video"`" ] && PROGRESS="" #filters for last lines >
 [ "`echo $PROGRESS|grep "Error"`" ] && PROGRESS="" #that we don't want
 [ "`echo $PROGRESS|grep "li"`" ] && PROGRESS=""
 [ "`echo $PROGRESS|grep "Header"`" ] && PROGRESS=""
 [ "`echo $PROGRESS|grep "Press"`" ] && PROGRESS=""
 #[ "$PROGRESS" != "" ] && echo -en "\r Time:${PROGRESS} \b"
 [ "$PROGRESS" != "" ] && printf "\r%s" "Time:${PROGRESS}"
 sleep 1
 done 
}

#play
playfunc(){
SKIPORQUIT="$2"
[ ! "$SKIPORQUIT" ] && SKIPORQUIT="quit"
TRACKNAME=$(echo "$1"|tr '/' '\n'|tail -n1)
echo "Now playing: $TRACKNAME"
durfunc &
progressfunc &
echo "Press \"q\" to $SKIPORQUIT"
ffmpeg -i "$1" -ss 0  -f au - 2>/tmp/ashmusic_ffmpeg | aplay 2> /dev/null ||\
echo "$1 not found" #/dev/audio
rm -f /tmp/ashmusic*
echo -e "\nfinished!\n========================================================"
}

#play list
playlistfunc(){
	TRACKLIST="$(cat $HOMEDIR/$1|\
	while read ALINE
	 do
	 echo $ALINE|tr '/' '\n'|tail -n1
	 done)"
	echo "Tracklist:"
	echo "$TRACKLIST"
	echo ""
	 cat $HOMEDIR/$1|tr ' ' '_' > $HOMEDIR/${1}.new
	 for i in $(cat $HOMEDIR/${1}.new)
	  do TRACK=$(echo "$i"|grep "\."|tr '_' ' ')
	  #rm /tmp/ashmusic*
	  sleep 0.5
	  [ "$TRACK" ] && playfunc "$TRACK" skip|| break
	  done
	 rm -f $HOMEDIR/*.new	&& exit 0
}

#add playlist
addfunc(){
set +e #unset set -e, grep throws the error
NOTEXISTS=$(file "$1"|grep "cannot open")
[ "$NOTEXISTS" ] && \
echo ""$1" doesn't exist! Check the spelling or path" && exit 1
set -e
echo "Please type a playlist name"
read LISTNAME
[ ! -f $HOMEDIR/$LISTNAME ] && touch $HOMEDIR/$LISTNAME||\
echo "$LISTNAME exists! Appending track"	
}

#filter args
for arg in "$@"
 do
	case $arg in
	-*h*) usagefunc
	;;
	-a)[ ! "$2" ] && usagefunc
	addfunc "$2" #add to playlist and play
	echo $LISTNAME > /tmp/ashmusic_add_track
	;;
	-b)[ ! "$2" ] && usagefunc
	addfunc "$2" #add to playlist, don't play
	echo "$2" >> $HOMEDIR/$LISTNAME
	exit 0
	;;
	-d)LISTDIR="$2" #add dir as playlist, play or not
	USELISTDIR=$(basename "$LISTDIR")
	DLIST=$(ls -1 "$LISTDIR"|grep -iE "\.mp3$|\.aif$|\.ogg$|\
	\.wma$|\.ra|\.wav$|\.aac$")
	echo "Type a name for your directory playlist (no spaces) then \"Enter\""
	echo "or just \"Enter\" to use "$USELISTDIR""
	read DPLAYLIST
	if [ ! "$DPLAYLIST" ];then 
	 DPLAYLIST=$(echo "$USELISTDIR"|tr ' ' '_')
	fi
	[ -f "$HOMEDIR/$DPLAYLIST" ] && echo ""$DPLAYLIST" already exists! Exiting.."\
	&& exit 0
	echo "$DLIST"|\
	while read DLINE
	 do echo "$LISTDIR/$DLINE" >> $HOMEDIR/$DPLAYLIST
	 done
	echo "$DPLAYLIST is stored. Press \"p - Enter\""
	echo "now to play $DPLAYLIST or \"enter\" to quit"
	read PLAYNOW
	[ ! "$PLAYNOW" = "p" ] && exit 0 || echo "Playing $DPLAYLIST"
	 echo "$DPLAYLIST"
	 playlistfunc "$DPLAYLIST" 
	;;
	-l)NAME="$2" #play playlist
	ls -1 $HOMEDIR >/tmp/ashmusic_playlists
	PLAYLISTS=$(cat /tmp/ashmusic_playlists)
	if [ ! "$NAME" ];then
	  echo "$PLAYLISTS"
	  echo "Type the name of a playlist to load from the above list"
	  read NAME
	fi
	PLAYOK=$(grep "$NAME" /tmp/ashmusic_playlists)
	[ ! "$PLAYOK" ] && echo "That list doesn't exist!" && exit 1
	playlistfunc "$NAME"
	;;
	*)TRACK="$arg" #play track only
	  if [ -f /tmp/ashmusic_add_track ];then
	  TOADD=$(cat /tmp/ashmusic_add_track)
	  echo "$TRACK" >> $HOMEDIR/$TOADD
	  rm -f /tmp/ashmusic_add_track
	  fi
	playfunc "$TRACK"
	;;
	esac
 done
exit 0

Attachments
ashmusic-0.3.pet
(2.18 KiB) Downloaded 518 times
ash0.png
(27.05 KiB) Downloaded 840 times
ash1.png
(23.84 KiB) Downloaded 870 times
Puppy Linux Blog - contact me for access

User avatar
darkcity
Posts: 2534
Joined: Sun 23 May 2010, 19:16
Location: near here
Contact:

#4 Post by darkcity »


User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#5 Post by greengeek »

Thanks. Working fine on my slacko 5.6 derivative.

Post Reply