MPlayer playback / resume position script This PHP script will save the playback position of a file you watch with MPlayer. Once you start playing the same file again, it will resume from last playback position. The *only* way this script will work is if you pass the "get_time_pos" command to MPlayer through an input event, mapped by either LIRC or the keyboard. For v1.5, you can also add "get_filename" to the exit command as well, though this is optional for resuming playback of a file you started in a playlist (see Playlist section below). The script captures the output and saves it to a file in the playback directory for each media file. See http://www.mplayerhq.hu/DOCS/tech/slave.txt for more commands that work on both backends. LIRC: Setup your ~/.lircrc file to do three things when you hit the stop button: 1) Get the time position 2) Get the filename 3) Stop playing begin prog = mplayer button = stop config = get_time_pos end begin prog = mplayer button = stop config = get_filename end begin prog = mplayer button = stop config = quit end You can of course map any button on your remote that you'd like to use this function for. Keyboard: Map a key with ~/.mplayer/input.conf to run 'get_time_pos' Sample entry: g get_time_pos When you want to save the position, hit 'g', and then 'q' to quit playback. You can also do it manually using mplayer's slave mode, which is useful for debugging: $ mplayer -slave -quiet movie.avi get_time_pos quit Configuration: This script will save a text file for each media file you watch using this script. The text files are saved in the config variable $save_files_to defined in the script, which must be a directory that this script can read / write to. The script will try to automatically create a directory, and uses /home/username/.mplayer/playback/ by default. Usage: To execute this program, either call it using the PHP binary, or make it executable. Whichever you like best. :) $ php mplayer-resume movie.avi # cp mplayer-resume /usr/local/bin/ # chmod +x /usr/local/bin/mplayer-resume $ mplayer-resume movie.avi The script should parse any arguments that you want to send mplayer at the same time, regardless of whether you start it as a standalone program or with php. Standalone: $ mplayer-resume movie.avi -vo xv With php: $ php mplayer-resume movie.avi -vo xv The *only* extra arguments that this script will add to your mplayer command is -ss to seek to resume a file, and -quiet so it can correctly grab the output. The script will also strip out any commands that are known to break the output. Currently the only one being stripped is "-really-quiet". As of version 1.5, mplayer-resume can handle files with spaces in them. Either escape the filename correctly, or add quotes around it. $ mplayer-resume dvds/Peter\ Pan.mkv $ mplayer-resume "dvds/Peter Pan.mkv" Playlists: v1.5 also adds a feature where if you start playing a file from a playlist (.m3u and .pls formats recognized by the script) and then resume the same file outside of the playlist, it will resume playback for that same file. For instance, let's say you have a playlist of movies with this as the contents: $ cat movies.pls Peter Pan.mkv TRON.mkv Star Trek 9.mkv If you start playing the playlist with mplayer-resume, when you resume playing back the same file, it will seek to the resume position. $ mplayer-resume -playlist movies.pls 1. Peter Pan.mkv starts playing 2. Exit $ mplayer-resume "Peter Pan.mkv" 3. Peter Pan.mkv resumes playback This works because of adding the "get_filename" option to LIRC as mentioned at the top of this document, which is optional if you want to use this feature. It's handy if you want to randomly play a file from a playlist, but resume playback by choosing it specifically the second time. $ mplayer-resume -playlist movies.pls -shuffle $ mplayer-resume TRON.mkv MythTV: If you are using MythVideo (http://www.mythtv.org/) and want to use mplayer-resume as your playback script instead of mplayer, it's easy to setup. From the main menu, go to: Utilities/Setup Video Settings Player Settings Change the entry for Default Player to: mplayer-resume %s Please note that you *do not* need to put quotes around the filename (as of v0.20.x), as MythTV will do it by itself. You can of course add any other extra MPlayer arguments as you normally may here. mplayer-resume -fs -vo xv -aspect 16/9 %s File formats: mplayer-resume has a strict list of file extensions that it recognizes as playable file formats. The reason for this is that there is no way to know what the filename is of the movie you are playing until you actually exit, and the script needs to know what it is at the beginning to resume playback. Media filename is determined by examining the arguments passed to the script and finding the one that ends with a hardcoded extension. The list of formats the script supports is extensive, and should cover most everything I can think of, but you may want it to add more. To do so, just hack the script manually and look for the variable $movie. You'll see a long regular expression with pipes between each format. To add a new extension, just add "|foo" somewhere in there. If you edit the script, it should be simple to figure out. :) Notes: If your CLI version of PHP is dumping version information each time it runs, add -q to the top line to supress the output. #!/usr/bin/php -q Bugs: Doesn't save cwd with the file, so you can only have one entry per filename Help: If you get stuck, don't hesitate to contact me. http://wonkabar.org/contact-me