LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Custom playback positions for mpv media player (https://www.linuxquestions.org/questions/linux-software-2/custom-playback-positions-for-mpv-media-player-4175535081/)

nokangaroo 02-25-2015 01:25 PM

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



All times are GMT -5. The time now is 03:15 PM.