Friday, December 4, 2009

Using sockets to run VLC in the background or across computers Part 1

I wrote a series of scripts to work with VLC.
They work with VLC in the background and let you manipulate the music you listen to via the command line.
With some small changes (change $optdir + chmod $optdir/sockets) you could even get this to work across computers or accounts.

You could download all the scripts I talk about here at The SVN repo viewer or just check them out at:
svn checkout vlc-scripts

Lets get this code to work on any system (if we don't know where sh is)

#!/usr/bin/env sh

Here is a simple function which creates the relevant directory and the working files.
This function is only run if it can't find $optdir

mkdir "$whomedir";
touch "$whomedir/socket_name";
mkdir "$whomedir/sockets";

Here we initialize configuration values.
Afterwords we use the getopts interface to read the command line options.
-c = if you did not properly exit VLC last time vlc_create_sock will refuse to run. This option cleans up
-n = Allows you to use an ncurses interface as well
-o = Allows you to modify $optdir. Changing this to a directory on another user account or on another computer will let you modify remote instances

#we default to a directory in your home.
while getopts co:n opt
case "$opt" in
'c') cleanup="YES";;
'n') ncurse="YES";;
'o') optdir="$OPTARG";;
'?') exit 1;;

If we don't exist let us install ourself

if [ ! -d "$optdir" ]
install_me "$optdir";

We now work with the options you gave us....
Print "Yeah!" if we will ignoring current instances

[ -n "$cleanup" ] && echo "Yeah!";
base="$(basename $0)";

Here we look to see if VLC is currently running. This prevents us from stopping a current instance. Using the option "-c" will forcefully end current instances. It does not skip the check

current_sock="$(cat $optdir/socket_name)";
if [ -n "$cleanup" ]
# exit program if currently running -c quit;
# remove the socket file (not deleted before if program not running)
rm $current_sock;
# empty the current socket file...

Here we tell you if vlc is current running. It requires Xdialog (and therefore X). I did these because of how I use the script. It isn't to difficult to change to detect X/no X or even just make it an option. This is on my list of things to do (issue 9).

if [ -e "$current_sock" ]
Xdialog --icon ~/bin/icons/warning.xpm --infobox "VLC already running...." 0 0 2000;
return 1;

We now know that we are all set to start a new session.
We first create a file in $optdir/sockets. This is because we will be using the "rc-unix" interface of vlc. This allows you to run vlc in the background and then close the terminal.

TMPFILE=$(mktemp $optdir/sockets/XXXXXX);
#we can't operate if the file exists....
rm "$TMPFILE";
echo $TMPFILE;
echo "$TMPFILE">$optdir/socket_name;

Choose the default options to run VLC with. If we specified "-n" also include an ncurses control to the same session.

Lets explain each of these options one by one:
--rc-unix This option tells VLC where to put the unix socket that we will be communicating with.
--rc-fake-tty This option is required by certain Linux distros or when run by some Terminal emulators
-L Loop through the music once you reach the last one - maybe I should make this into an option
--no-media-library Not actually required
--volume I let vlc run at max volume and have the operating system or speaker control what comes out.
-d Run as a daemon.

# -L == loop; -d == deamon;
vlc_opts="--rc-unix "$TMPFILE" --rc-fake-tty -L --no-media-library -d --volume 1024";
if [ -n "$ncurse" ]
vlc --intf rc --extraintf ncurses $vlc_opts;
vlc --intf rc $vlc_opts;

It worked! Lets return 0!

return 0;

In my next post I'll explain, vlc.done, and the other control scripts.

No comments:

Post a Comment

Have something you want to say? You think I'm wrong? Found something I said useful?
Leave a comment!