Some mplayer -slave foo

I am an extremely lazy person. That means I am always looking for ways
to do things quicker, easier and with the least bit of effort. This
incessant obsessive search for alternate solutions often leads to some
quite cool discoveries.

This particular post is to share with others as lazy as myself who
hate to get off their bed to change the movie or volume of the mplayer
instance playing on the desktop when you have your laptop lying next
to you. Of course this is easily solved with some money if you buy an
LIRC remote (mplayer supports it). But the challenge there is to
actually find a decent remote that works with PCs (damn you
consoles!). Hope this keeps you on your bed longer, so here it goes:

#!/bin/bash


# Copyright (C) 2011 by Suvayu Ali
#
# This is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.


function usage_help()
{
    echo -e "
\e[1mUsage:\e[0m $(basename $0) -s <file> [<options>] | -c <mplayer commands> | -q

For a list of supported commands for mplayer -slave, try:
       mplayer -input cmdlist

       \e[1m-s\e[0m <file> [<options>]

           Play <file> and pass the optional extra <options> to
           mplayer instance.

       \e[1m-c\e[0m <quoted mplayer commands>

           Send the commands to the running mplayer instance. Note
           that if the commands are multi-word, they need to be
           quoted.

       \e[1m-q\e[0m

           Cleanup any stale mplayer fifo used to send commands to
           \e[4mmplayer -slave\e[0m.

"
}


function cleanup_slave()
{
    [ -e /tmp/mplayer-control ] && rm -f /tmp/mplayer-control \
	&& echo "Deleting /tmp/mplayer-control"
}


function test_slave()
{
    echo "(test) Playing file ${mediafile} with options" ${cmdopts}
}


function start_slave()
{
    # make fifo file
    [ ! -e /tmp/mplayer-control ] \
	&& mkfifo -m 644 /tmp/mplayer-control \
        || {
	echo "fifo file /tmp/mplayer-control already exists."
	ps -fu ${USER} | egrep '[m]player-slave' -q
	[ $? -eq 0 ] && echo "mplayer-slave already running." \
	    && exit -1
    }

    mplayer -quiet -slave ${cmdopts} -input file=/tmp/mplayer-control "${mediafile}" \
	&& cleanup_slave
}


function command_slave()
{
    echo "Commands issued to mplayer: " ${slavecmd}
    [ -e /tmp/mplayer-control ] \
    	&& echo "${slavecmd}" > /tmp/mplayer-control
}


getopts "s:c:qt:" mode
case $mode in
    s)
	mediafile=$OPTARG
	shift $((OPTIND - 1))
	cmdopts=$@
	start_slave ${mediafile} ${cmdopts}
	;;
    c)
	slavecmd=$OPTARG
	command_slave
	;;
    q)
	cleanup_slave
	;;
    t)
	mediafile=$OPTARG
	shift $((OPTIND - 1))
	cmdopts=$@
	test_slave ${mediafile} ${cmdopts}
	;;
    *)
	usage_help
	;;
esac
Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: