playshuf
Endlessly play music files from a directory in random order. Recursive directories are supported; files inside subdirectories will also be played.
Last modified | |
Lines | 56 |
Parent directory Download CGIread sitemap Main page
Quick links: (none)
#!/bin/sh
default_music_dir="/media/wrk/music"
player_wrap ()
{
# .mkv -> Matroska data
# .ogg -> audio
# .mp3 -> Audio
# .webm -> Webm
# .mp4 -> MP4
# .m4a -> MPEG v4
if [ -n "$(file "$1" | grep -E '(WebM|[Aa]udio)|Matroska data|MP(EG v)?4')" ]; then
cvlc --play-and-exit --no-video "$1" 2>/dev/null || cvlc --play-and-exit "$1"
else
file "$1" >&2
fi
}
mk_playlist ()
{
# Use du(1) to allow a "structured" playlist.
du -aL "$music_dir" | cut -f 2 | shuf \
| while read -r filename; do
test -d "$filename" || echo "$filename"
done
}
usage="playshuf [-q] [music-directory]"
quiet=false
if [ "$1" = "-q" ]; then
quiet=true
shift
fi
if [ -n "$1" ]; then
music_dir="$1"
else
music_dir="$default_music_dir"
fi
# Print the usage line anyway.
$quiet || echo "Usage: $usage" >&2
# Main loop
while true; do
i=0
n="$(mk_playlist | wc -l)"
mk_playlist | while read -r song; do
i=$(($i + 1))
$quiet || echo "$i/$n\t$song"
sleep 2
player_wrap "$song"
done
done