Skip to content

Instantly share code, notes, and snippets.

@afarah1
Last active September 17, 2018 19:29
Show Gist options
  • Save afarah1/e8cbafaf1d9d8029c6ca to your computer and use it in GitHub Desktop.
Save afarah1/e8cbafaf1d9d8029c6ca to your computer and use it in GitHub Desktop.
mplayerp - Use mplayer+bash as a full-featured media player
#!/bin/bash
#
# mplayerp - Use mplayer+bash as a full-featured music player
#
# Starts mplayer in slave mode listening to commands from a fifo and reading
# media from a playlist file. There are some extras to make it pleasent to use.
#
# Syntax: mplayerp [OPTIONS] [ACTION | DIR]
#
# Options:
#
# --depth DEPTH
# All operations use this depth instead of the default (see depth var)
#
# Actions:
#
# All actions are done for files under ./ and child dirs, according to depth
# options, unless otherwise specifiend. They can be combined.
#
# find PATTERN
# Play all songs which contain PATTERN in the filename (an empty pattern plays
# all songs)
#
# findd PATTERN
# Same as find, but for directories
#
# random [DIR]
# Play a random song under DIR or ./
#
# playlist FILE
# Play songs from a playlist
#
# count [DIR]
# Display some info about all songs under DIR or ./ and exit (mp3 only)
#
# If no action is provided, DIR may be specified. All songs under it (or ./
# in case it's not specified) will be played.
#
# It's useful to configure keyboard shortcuts in one's window manager to write
# commands to the fifo and thus be able to control mplayerp from any workspace
# or even from a different user acc/computer. E.g., on dwm:
#
# static const char *nextsong[] = { "sh", "-c", "echo 'pt_step 1'>/home/afh/.mplayer/fifo", NULL };
# static const char *prevsong[] = { "sh", "-c", "echo 'pt_step -1'>/home/afh/.mplayer/fifo", NULL };
# static const char *togglesong[] = { "sh", "-c", "echo 'pause'>/home/afh/.mplayer/fifo", NULL };
# ... could also be done with SHCMD ...
# { MODKEY, XK_F8, spawn, {.v = nextsong } },
# { MODKEY, XK_F7, spawn, {.v = prevsong } },
# { MODKEY, XK_F6, spawn, {.v = togglesong } },
#
EXTENSIONS="(asf|asx|avi|flac|flv|m1v|m2p|m2v|m4a|m4v|mjpg|mka|mkv|mov|mp3|mp4|mpe|mpeg|mpg|ogg|ogm|ogv|qt|rm|ts|vob|wav|webm|wma|wmv)"
PLAYLIST="${HOME}/.mplayer/playlist"
FIFO="${HOME}/.mplayer/fifo"
depth=999
acted=0
dir=""
rm -f "$PLAYLIST"
touch "$PLAYLIST"
while [ $# -gt 0 ]
do
key="${1:-0}"
case $key in
--depth)
re='^[0-9]+$'
[ -z $2 ] || ! [[ $2 =~ $re ]] && {
echo "Must provide a depth (positive integer)"
exit
}
depth=$2
shift
;;
find | findd | random | count)
acted=1
pattern=$2
case $key in
find)
find "`pwd`" -maxdepth $depth -type f | grep -Pe "$EXTENSIONS" | grep -iPe "/[^/]*$pattern[^/]*\.\w+$" | sort >>"$PLAYLIST"
;;
findd)
find "`pwd`" -maxdepth $depth | grep -Pe "$EXTENSIONS" | grep -iPe "$pattern.*/" | sort >>"$PLAYLIST"
;;
random)
find "`pwd`/$pattern" -maxdepth $depth -type f | grep -Pe "$EXTENSIONS" | shuf -n 1000 >>"$PLAYLIST"
;;
count)
echo "Music files:"
echo `find "`pwd`/$pattern" -maxdepth $depth -type f | grep -c -Pie "\.${EXTENSIONS}$"`
echo "Hours of music (mp3 files only):"
s=`find "`pwd`/$pattern" -name "*.mp3" -type f -print0 | xargs -0 -I '{}' mp3info -p "%S\n" {} | paste -sd+ - | bc`
echo "$s / 3600.0" | bc -l
exit
;;
esac
if [ ! -z $pattern ]; then shift; fi
;;
playlist)
acted=1
[ -z $2 ] && {
echo "Must provide a playlist"
exit
}
cat "$2">>"$PLAYLIST"
shift
;;
esac
dir="$1"
shift
done
if [[ $acted -ne 1 ]]
then
find "`pwd`/$dir" -maxdepth $depth -type f | grep -Pe "$EXTENSIONS" | grep -iPe "/[^/]*$*[^/]*\.\w+$" | sort >"$PLAYLIST"
fi
rm -f "$FIFO"
mkfifo "$FIFO"
echo "loadlist ${HOME}/.mplayer/playlist 1" >${HOME}/.mplayer/fifo &
mplayer -slave -idle -input file=${HOME}/.mplayer/fifo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment