Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ###############################################################################################
- # HISTORY
- # V1.0 Complete, Stuart Graham 24/12/09.
- #
- #
- # BRIEF DESCRIPTION - resumes mocp to last track and position in track
- #
- #
- # WHY I WROTE IT
- # I am a big fan of moc but, the only missing feature I found was it's inability to remember
- # the track and position of track since the last time I ran it. I tend to listen to a lot
- # of podcasts & audio books on moc that I stream to my cordless headphones from a server.
- # I control my server remotely using my Nokia N810 internet tablet, the screen and keyboard
- # of the N810 is a little small, it makes things a lot easier now that my script allows moc
- # to resume from from where it left off.
- #
- # The script tries to match each track in the playlist with the one stored in a settings file.
- # It takes moc a second or so to check a track in the playlist, this means the script works best
- # if the track you are trying to find is near the top of your playlist. I've also limited it to
- # only search the first 30 tracks on the playlist, although this can be easily changed by
- # changing the value of $count in function RESUME.
- #
- #
- # HOW TO USE IT
- # You must first have a fairly new version of moc installed on your system.
- # I am currently running Moc 2.5.0-alpha2 Build: Oct 28 2007 11:11:40, which was available in the
- # Ubuntu Hardy repo. eg run "sudo apt-get install moc" to install moc
- # or use Synaptic Package Manager and search for "moc"
- # Copy the script into somewhere the system can find it, maybe /usr/bin, I have a scripts dir in
- # my home dir which is included in my enviroment variable.
- # Create the settings file somewhere handy, use command "touch settings"
- # modify the values of the # GLOBAL CONSTANTS to suit your needs
- # eg modfy the values of "fpath" and "fname" then you should be ready to go.
- # good luck, I hope you find this useful :)
- #
- #
- # OPERATION OF SCRIPT
- # The script checks to see if the settings file "$fname" exists, if it doesn't the script exits.
- #
- # It checks to see if the moc server is playing or paused, if it is then it runs the moc GUI as
- # normal. If the moc server is stopped it loads the previous played track name and position info
- # from the settings file "$fname".
- #
- # If the title stored in the settings file "fname" is blank then the moc GUI is run as normal.
- #
- # If the title stored in the settings file "fname" is NOT blank then the script diplays a
- # "PLAY OPTIONS" menu.
- #
- # The "PLAY OPTIONS" are (R)esume, (C)ontinue or (Q)uit.
- #
- # If the (Q)uit option is chosen the script exits normally.
- # If the (C)ontinue option is chosen then the script runs the moc GUI as normal.
- # If the (R)esume option is chosen then the script will attempt to resumes moc to it's
- # previous run track within the playlist.
- # It will also attempt to resume to the previous track position.
- #
- #
- # SHORTCOMMINGS
- # if the track has a blank title in the ID tag info then resume will not work as this is how the
- # script resumes to the correct track.
- #
- # Script will try 30 times to find the track to resume from. If the track is further down than 30
- # tracks then the script gives up.
- #
- # The script will exit if the settings file doesn't exist, it will not create a blank settings
- # file for you.
- #
- #
- # POSSIBLE IMROVEMENTS
- # Script could recognise when the end of the playlist is reached, this could be done by moving
- # down one track then comparing the current track name with the previous track name, if the
- # track names is unchanged then it can be assumed that we reached the end of the playlist.
- # For me, this is a low priority improvment since I wrote this script to listen to audio books
- # & podcasts, which I delete from the playlist once listend to.
- # The script is not intended to be used to search through large playlists as it would take
- # prohibitively long to locate the track to resume.
- #
- # Another improvement would be to create an empty settings file rather than just exit if it
- # doesn't exist.
- ###############################################################################################
- # GLOBAL CONSTANTS
- readonly fpath="~/" # path to settings file
- readonly fname="resume-mocp-settings" # name of file that holds settings
- readonly mocp="mocp $@" # allows you to run mocp with options if required
- readonly recap="30" # amount of seconds to recap when moc resumes
- # GLOBAL VAIABLES
- # Values gathered from current running mocp
- mocState="" # current moc state, eg PLAY, PAUSE, STOP
- mocCurrentSec="" # current seconds into track
- mocCurentTime="" # current time into track eg "hours:minutes"
- mocTitle="" # Title of current song
- # Values from mocp settings file gathered from previous run of mocp
- resumeState="" # saved moc state, eg PLAY, PAUSE, STOP
- resumeCurrentSec="" # saved position in track in seconds
- resumeCurentTime="" # saved position in track in "hours:minutes"
- resumeTitle="" # saved Title of song
- function GET_MOC_STATE ()
- # Gets the current running moc state
- {
- mocState="$($mocp --info | grep "^State:" | awk '{print $2}')"
- mocCurrentSec="$($mocp --info | grep "^CurrentSec:" | awk '{print $2}')"
- mocCurrentTime="$($mocp --info | grep "^CurrentTime:" | awk '{print $2}')"
- mocTitle="$($mocp --info | grep "^Title:" | cut -c8-)"
- }
- function GET_RESUME_STATE ()
- # Get old moc resume state from disk and pass the values to the global
- # resume variables
- {
- # LOCAL VARIABLES
- settings="" # stores the settings read from file "$fname"
- # read settings from disk from $fname and store in variable $settings
- # use awk to chop up the text string held in $settings into fields
- # then assign these values to the global resume variables
- while read settings
- do
- # eval used here so that all aurguments are concatenated together into a single
- # command before being evaluated by the shell this allows the value of $1, $2,
- # $3 and $4 to be assinged to the appropriate resume variables.
- eval $(echo $settings | \
- awk -F\| '{print "resumeState=\""$1"\"; resumeCurrentSec=\""$2"\"; resumeCurrentTime=\""$3"\"; resumeTitle=\""$4"\""}')
- done < "${fpath}${fname}" # path and file name of settings file
- }
- function RUN_MOC ()
- # Runs mocp then when mocp GUI exits gets current mocp state and writes it to disk
- {
- $mocp # runs mocp
- GET_MOC_STATE
- # write current seek position, time and track name to disk
- echo "$mocState|$mocCurrentSec|$mocCurrentTime|$mocTitle" > "${fpath}${fname}"
- }
- function RESUME ()
- # Resume Mocp to it's previous state from values stored in $fname file
- # Will try $count times (30) to match title stored on disk with one of the titles
- # within the laylist
- {
- # LOCAL VARIABLES
- count="30" # Number of tracks to move through while trying to find resumed track.
- eval "$mocp --play" # start playing from 1st track in playlist
- echo "Waiting for mocp to settle..."
- sleep "2" # give moc time to settle before checking first track
- echo "Trying $count times to find track, please wait..."
- while [ "$count" -ge 1 ] ; do
- echo " ${count}..."
- GET_MOC_STATE
- if [[ "$mocTitle" = "$resumeTitle" ]] ; then # Check to see if moc is playing the correct resume track
- if [[ "$resumeCurrentSec" -gt "$recap" ]] ; then # Recap if far enough into track
- eval "$mocp --seek $(($resumeCurrentSec - $recap))" # Seek to resume part of track minus recap value
- else
- eval "$mocp --seek $resumeCurrentSec" # Seek to resume part of track
- fi
- sleep "1" # delay introduced so there is time for pause to work
- eval "$mocp --pause" #pause mocp playing
- RUN_MOC
- exit 0
- else
- eval "$mocp --next" # move to next track to try again to match track
- fi
- count="$(($count - 1))"
- done
- # If track not found then pause mocp
- eval "$mocp --previous" # Go back one track to last track title checked
- sleep "1" # delay introduced so there is time for pause to work
- eval "$mocp --pause" #pause mocp playing
- # Error mesages sent to standard err
- clear
- echo
- echo "***************************** ERROR ***********************"
- echo "Could not find track!" >&2
- echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
- echo "STOPPED AT.......$mocTitle" >&2
- echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
- echo "TRYING TO FIND...$resumeTitle" >&2
- echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
- echo "Run $mocp to see where you are"
- echo "***********************************************************"
- echo
- exit 1
- }
- function MAIN_PROGRAM ()
- # Exit if settings file $fname does not exist
- # Get current running moc state. If moc is playing or paused then run moc gui.
- # If moc is in stopped state then load moc resume settings from disk. If the resume
- # title is blank run moc gui. If resume title is NOT blank then display "PLAY OPTION"
- # menu. If user picks (R)esume then resume moc to previous track and postion. If user
- # picks (C)ontinue then run moc gui. If user picks (Q)uit then exit script.
- {
- # LOCAL VARIABLES
- answer="" # answer from user input in PLAY OPTIONS menu
- # Exit if settings file not found
- if [[ ! -f "${fpath}${fname}" ]] ; then
- echo
- echo " **** Resume settings file ${fpath}${fname} NOT FOUND! ****" >&2
- echo
- exit 2
- fi
- GET_MOC_STATE
- # for some reason wont work with [[ if using -o, ie tesing for one thing OR another
- if [ "$mocState" = "PLAY" -o "$mocState" = "PAUSE" ] ; then
- RUN_MOC # run moc
- exit 0
- else
- # mocpState must be STOPPED
- GET_RESUME_STATE # loads resume settings from disk
- # If resumeTitle is blank then run mocp
- if [[ "$resumeTitle" = "" ]] ; then
- RUN_MOC
- exit 0
- fi
- clear # clear screen
- echo "~~~~~~~~~~~~~~~~~~~~~~~ PLAY OPTIONS ~~~~~~~~~~~~~~~~~~~~~~~~"
- echo "(R)esume"
- # Could use colours here to improve readablility
- echo " ${resumeCurrentTime}|${resumeTitle}"
- echo
- echo
- echo "(C)ontinue without resume"
- echo
- echo
- echo "(Q)uit"
- echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
- echo -n "CHOOSE OPTION: "
- #echo "============================================================"
- read "answer"
- case "$answer" in
- r|R)
- # move to resume point and run mocp
- RESUME
- ;;
- c|C)
- # Continue and run moc as normal
- RUN_MOC
- exit 0
- ;;
- q|Q)
- # Quit script with exit code 0
- exit 0
- ;;
- *)
- echo
- echo "INVALID OPTION...!"
- sleep "1"
- MAIN_PROGRAM
- ;;
- esac
- fi
- }
- # Runs MAIN_PROGRAM
- MAIN_PROGRAM
Advertisement
Add Comment
Please, Sign In to add comment