MPlayer resume script

MPlayer logo

I might as well share this – below are 2 scripts I’ve written for MPlayer to resume playback at a position saved in a text file.
(Note the scripts below also attempt to load the "global" profile – I use it for chaining profiles – i.e. I have other profiles that includes "global".)

It works by appending the "-ss" parameter to mplayer when a resume file, in the format "[mediaFileName.ext].txt" is detected.

An example usage (we’ll assume we’re trying to resume a file called "mediaFile.avi" at timecode 300, and the script is named "mp"):

echo 300>mediaFile.avi.txt
mp mediaFile.avi

You’ll probably want to enable the "statusline" display by adding "msglevel=statusline=9" into your mplayer config file (at "%ProgramFiles%/MPlayer/mplayer/config" | "~/.mplayer/config" | "/etc/mplayer/config"), and ensure "quiet=0".

Windows installation:
0 – Get a copy of "grep" and "head" somewhere – such as UnxUtils.
1 – Save script as mp.bat (or something simple to type)
2 – Put into a directory in your %PATH% env. variable.
3 – Play files using the "mp" command, and create resume file as specified in example above.

@echo off
setlocal ENABLEDELAYEDEXPANSION

set mplayerbin=%ProgramFiles%MPlayermplayer.exe
set cli=

FOR /F "usebackq delims==" %%c IN (`echo %*^|grep -c -i " -profile "`) DO set count=%%c
if !count! == 0 (
	set cli="!mplayerbin!" -profile global %*
) else (
	set cli="!mplayerbin!" %*
)

REM Resume stuff - this'd only work if filename is specified as first argument.
FOR /F "usebackq delims==" %%c IN (`echo %*^|grep -c -i "-ss"`) DO set count=%%c
set resumeFile=%1.txt
if exist "!resumeFile!" (
	if !count! == 0 (
		FOR /F "usebackq delims==" %%c IN (`head -n 1 "!resumeFile!"`) DO set pos=%%c
		set cli=!cli! -ss !pos!
	)
)

echo ^>^>^>^> !cli!
!cli!

endlocal

Linux installation:
1 – Save script as mp (or something simple to type)
2 – Put into a directory in your $PATH env. variable.
2 – chmod +x mp
3 – Play files using the "mp" command, and create resume file as specified in example above.

#!/bin/bash

# Script to launch mplayer and resume if [filename.ext].txt exists
# TODO - Integrate the new parser into the old windows script below

mplayerbin=mplayer

# Unfortunately, something simple like: for i in $*; do echo $i; done
# doesn't work with spaces (even when it's escaped) - it'd separate parameters out by space...
# This also assumes the first argument with a space is the filename
argc=$#
i=0
while ((i < argc)); do
	# escape spaces - can't use <code>echo $1|grep " "</code> or it'll display the params...
	if [ "$(echo $1|grep -cE "[ ()$]")" == 1 ]; then	# Any others?
		argv[$i]="$1"
	else
		argv[i]=$1
	fi
	shift; ((i++))
done


cli=${argv[*]}
# Old windows port of the script below
count=$(echo $cli|grep -c -i " -profile ")
if [ $count == 0 ]; then
	cli="$mplayerbin -profile global $cli"
else
	cli="$mplayerbin $cli"
fi

# Resume stuff - this'd only work if filename is specified as first argument.
count=$(echo $cli|grep -c -i "-ss")
resumeFile="${argv[0]}.txt"
# -f ensures the file (not directory) exist
if [ -f "$resumeFile" ]  && [ $count == 0 ]; then
	pos=$(head -n 1 "$resumeFile")
	cli="$cli -ss $pos"
fi
echo >>>> $cli
eval $cli

There is however a bug in the Windows version due to the use of delayed expansion – if you have a file with an exclamation mark, you’d need to escape it with "^^" (e.g. "^^!").

Also, it’d be a good idea to specify the filename as the first parameter – I haven’t done any extension check to figure out which parameter the filename is at.

You can find more information about the command-line parameters for MPlayer at:
http://www.mplayerhq.hu/DOCS/man/en/mplayer.1.html


Update: 2008-07-10 @ 03:54
I’ve made a new version – v2-alpha. The new post is here

Advertisements

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: