playrandom mp3 bash script

A easily breakable bash script for playing Random mp3 files from recursive directories using MPV player.

[sourcecode language=”shell”]

#!/bin/bash
# Play random mp3 files recursively in current directory or from a Specified Subpath in the users default Music directory
# Written in a Hurry: Brian Dunaway 2019, Feb 12. brian@studiom80.com
# Requires mpv and access to /tmp directory

playpath=~/Music/
prevpath=

usage()
{
echo "usage: playrandom [-s]"
}

while [ "$1" != "" ]; do
case $1 in
-s | –sub ) shift
prevpath=$playpath
playpath=$playpath$1/
;;
-h | –help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done

if [ "$prevpath" = "" ]; then
playpath="$(pwd)/"
fi
playpath=`shuf -n1 -e "$playpath"`

# find it / sort it / write it
find "$playpath" -type f -name "*.mp3" | sort -R > /tmp/playrandom.m3u
# play it
mpv –no-video /tmp/playrandom.m3u
# remove it
rm /tmp/playrandom.m3u
# profit
[/sourcecode]

Liked it? Take a second to support Brian Dunaway on Patreon!

Leave a Reply