LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-25-2015, 01:25 PM   #1
nokangaroo
Member
 
Registered: Nov 2009
Posts: 141

Rep: Reputation: 25
Custom playback positions for mpv media player


The "official" way to do this would probably be Lua scripts, but there are other
ways with a less steep learning curve that work perfectly well.

There is a "custom playback positions" thread on github, where they discuss
using ffmetadata files for creating chapters, but I am not sure that will work.
ffmetadata files are for video editing, not playback, and you would have to
create all bookmarks in one go, in sequence (A script could be written for this,
using input created with avidemux, which will also tell you where the I-frames
are, but that is a different task).

I have some doubts about the MEDIA_ID function in the following script. I don't
know if it is unique enough, and it probably won't work with URLs. Suggestions
are welcome.

The player script to start mpv with an environment:
Code:
#!/bin/bash
# Copyright (C) 2015 nokangaroo nokangaroo@NOSPAM.aon.at

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.



# Player script
# Work in progress

# Not supposed to run as root:
[[ $UID -eq 0 ]] && exit

# Media folder:
MEDIADIR="$HOME/mpv-data"
[[ -d $MEDIADIR ]] || mkdir -p $MEDIADIR

#Input fifo:
FIFO=mpv.input
[[ -p $FIFO ]] || mkfifo $FIFO

#This script:
PLAYER="$0"

# Identifying the media file (this is probably better than using a path,
# but I don't know yet if it is unique enough; head -c 2048 is too short)
MEDIA_ID="$MEDIADIR/`head -c 4096 "$1" | md5sum | cut -d \  -f 1`"
#MEDIA_ID="$MEDIADIR/`basename $1`"

# Data for the bookmarking script to source. This will handle spaces in file
# names:
rm -f $MEDIADIR/data
for data in MEDIA_ID FIFO PLAYER; do
    eval echo $data=\$"$data" | sed 's/ /\\ /g' >> $MEDIADIR/data
done

mpv --fs --input-file=mpv.input "$1"
The bookmarking script, to be started while mpv, started with the player
script, is running:
Code:
#!/bin/bash
# Copyright (C) 2015 nokangaroo nokangaroo@NOSPAM.aon.at

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.



# Bookmarking script to be used with the player script
# Work in progress

# Media folder:
MEDIADIR="$HOME/mpv-data"

# The file to source for variables:
DATAFILE="$MEDIADIR/data"

# Get variables:
[[ -f $DATAFILE ]] && . $DATAFILE

# Needs to be started after the player script, except for help:
# TODO: write help option (and lots of other options)!
# This is surprisingly difficult to get right:
#[[ `ps u | grep $PLAYER | grep -v grep` || $1 == '-h' || $1 == '--help' ]] || exit
#[[ `pidof -x $PLAYER` ]] || exit
[[ `ps u | grep $PLAYER | grep -v grep` ]] || exit



# Functions go here:
mpcmd() {
    echo "$*" > $FIFO
}

set_bookmark() {
    # This function can set arbitrary commands, not just seek times.
    # The bookmarks are numbered so they can be selected with the keypad:
    echo -e "$(expr `grep -s -o '^[0-9]\+' $MEDIA_ID | sort -n | tail -n 1` + 1)"'\t'"$BMK" >> $MEDIA_ID
    while [[ $# -gt 0 ]]; do
        echo -n 'command@'"$1 " >> $MEDIA_ID
        shift
        echo 'run "/bin/sh" "-c" "/usr/bin/echo \"${'"$1"'}\" >> '$MEDIA_ID\" > $FIFO
        sleep 0.3 # May or may not be needed
        shift
    done
}
# End functions

case $1 in
-c|--create)
BMK=$(zenity --entry --title="New Bookmark" --text="A new bookmark will be created at the present position. Choose a name for it:")
if [[ $BMK ]]; then
    set_bookmark "set time-pos" "=time-pos"
fi
;;
-k|--command)
# The distinction between bookmarks and commands is for convenience; they work
# in the same way
# An even number of arguments after the bookmark name is required. Arguments are
# evaluated in pairs with each command in a new line, see set_bookmark function
# The command can of course be combined with a seek
# Example: Set volume, audio and subtitle track as desired and do:
# bookmarkscript -k 'Name' 'set aid' '=aid' 'set sid' '=sid' 'set volume' '=volume'
# Repeat this for all tracks you want to switch between
BMK="$2"
if [[ "$3" && "$4" ]]; then
    shift 2
    set_bookmark "$@"
fi
;;
-l|--list)
# This can execute an arbitrary number of command@ lines after the bookmark.
# Note the command@ prefix is not really needed (yet); but it makes the file
# more human-readable, and also makes it possible to add comments or distinguish
# between different types of commands (sed won't give any error if $LINES is
# empty, and it works past the end of the file)
if [[ -f $MEDIA_ID ]]; then
    SELECTION=`sed -n '/^[0-9]\+\t/ P' $MEDIA_ID | zenity --list --title="Bookmarks" --column="" --hide-header | cut -f 1`
    if [[ $SELECTION ]]; then
    LINES=`sed -n "/^\<${SELECTION}\>/,/^\<[0-9]\+\t.*\>/ {/^\<${SELECTION}\>/n
/^\<[0-9]\+\t.*\>/! s/^command@// p}" $MEDIA_ID`
        for i in "$LINES"; do
            mpcmd "$i"
        done
    fi
fi
;;
*)
#quit mpv
echo 'quit' > $FIFO
;;
esac
A sample bookmark file (the bookmarks should of course be given meaningful
names):
Code:
1   *** Track 1 ***
command@set aid 1
command@set sid no
command@set volume 34.000000
2   bmk 1
command@set time-pos 33.424156
This is a comment
3   *** Track 2 ***
command@set aid 2
command@set sid 1
command@set volume 47.000000
4   bmk 2
command@set time-pos 12.144156
5   bmk 3
command@set time-pos 3.144156

Last edited by nokangaroo; 02-26-2015 at 03:07 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Custom portable media player with xmbc amsweitzer Linux - General 3 04-11-2011 10:55 PM
Looking for a media player for Hollywood movie playback ronb0803 Linux - Newbie 5 02-07-2009 02:52 PM
Media player with gapless playback of FLAC/SHN? MheAd Linux - Software 3 06-15-2007 09:01 PM
DVD and windows media player playback on Mepis 3.4 (codecs needed?) protonluke MEPIS 15 04-08-2007 09:05 AM
m4a/aac playback on Beep-Media-Player GT_Onizuka Linux - Software 0 01-21-2006 02:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration