Search code examples
mplayerslaveash

Get current playing file in MPlayer slave mode


Problem: I can't find any way to reliably get the current playing file in an MPlayer playlist.

Here is how far I have gotten. This working ash script monitors a text file with the path to the current playlist. When I update the file, the script closes the old instance of MPlayer and opens a new one with the new playlist:

# POLL PLAYLIST FILE FOR CHANGES
CURRENTPLAYLISTPATH=/home/tc/currentplaylist
INFIFO=/tmp/mplayer-in

CURRENTPLAYLIST="NEVERMATCHAPLAYLIST"
FIRSTRUN=1

while [ 1 ];
do
    # CHECK FOR NEW PLAYLIST
    NEWPLAYLIST=$(head -n 1 $CURRENTPLAYLISTPATH)
    if [[ "$NEWPLAYLIST" != "$CURRENTPLAYLIST" ]]; then
        if [ "$FIRSTRUN" == 0 ]; then
            echo "quit" > "$INFIFO"
        fi

        # CREATE NAMED PIPE, IF NEEDED
        trap "rm -f $INFIFO" EXIT
        if [ ! -p $INFIFO ]; then
            mkfifo $INFIFO
        fi

        # START MPLAYER
        mplayer -fixed-vo -nolirc -vc ffmpeg12vdpau,ffh264vdpau, -playlist $NEWPLAYLIST -loop 0 -geometry 1696x954 -slave -idle -input file=$INFIFO -quiet -msglevel all=0 -identify | tee -a /home/tc/mplayer.log &

        CURRENTPLAYLIST=$NEWPLAYLIST
        FIRSTRUN=0
    fi

    sleep 5;
done

My original plan was just to use the "-identify" flag and parse the log file. This actually works really well up until I need to truncate the log file to keep it from getting too large. As soon as my truncating script is run, MPlayer stops writing to the log file:

FILENAME=/home/tc/mplayer.log
MAXCOUNT=100
if [ -f "$FILENAME" ]; then
    LINECOUNT=`wc -l "$FILENAME" | awk '{print $1}'`

    if [ "$LINECOUNT" -gt "$MAXCOUNT" ]; then
        REMOVECOUNT=`expr $LINECOUNT - $MAXCOUNT`
        sed -i 1,"$REMOVECOUNT"d "$FILENAME"
    fi
fi

I have searched and searched but have been unable to find any other way of getting the current playing file that works.

I have tried piping the output to another named pipe and then monitoring it, but only works for a few seconds, then MPlayer completely freezes.

I have also tried using bash (instead of ash) and piping the output to a function like the following, but get the same freezing problem:

function parseOutput()
{
    while read LINE
    do
        echo "get_file_name" > /tmp/mplayer-in
        if [[ "$LINE" == *ANS_FILENAME* ]]
        then
          echo ${LINE##ANS_FILENAME=} > "$CURRENTFILEPATH"
        fi
        sleep 1
    done

}

# START MPLAYER
mplayer -fixed-vo -nolirc -vc ffmpeg12vdpau,ffh264vdpau, -playlist $NEWPLAYLIST -loop 0 -geometry 1696x954 -slave -idle -input file=/tmp/mplayer-in -quiet | parseOutput &

I suspect I am missing something very obvious here, so any help, ideas, points in the right direction would be greatly appreciated.

fodder


Solution

  • Well, I gave up on getting the track from MPlayer itself.

    My 'solution' is probably too hackish, but works for my needs since I know my machine will only ever have one instance of MPlayer running:

    lsof -p $(pidof mplayer) | grep -o "/path/to/my/assets/.*"
    

    If anyone has a better option I'm certainly still interested in doing this the right way, I just couldn't make any of the methods work.

    fodder