mpvd

[discontinued] Simple daemonization script for mpv with controller
git clone https://noxz.tech/git/mpvd.git
Log | Files | LICENSE

commit: 127d7870e57742242675d775a293a250af20e9f3
parent: 
author: Chris Noxz <chris@noxz.tech>
date:   Sun, 27 Jan 2019 14:36:28 +0100
initial commit
AMakefile17+++
Aconfig.mk1+
Ampvc113++++++++++++++++++++
3 files changed, 131 insertions(+)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,17 @@
+.POSIX:
+
+include config.mk
+
+install:
+	@echo installing executable file to ${DESTDIR}/bin
+	@mkdir -p ${DESTDIR}/bin
+	@cp -f mpvc ${DESTDIR}/bin
+	@chmod 755 ${DESTDIR}/bin/mpvc
+	@sh -c 'cd "${DESTDIR}/bin"; ln -s mpvc mpvd'
+
+uninstall:
+	@echo removing executable file from ${DESTDIR}/bin
+	@rm -f ${DESTDIR}${PREFIX}/bin/mpvd
+	@rm -f ${DESTDIR}${PREFIX}/bin/mpvc
+
+.PHONY: install uninstall
diff --git a/config.mk b/config.mk
@@ -0,0 +1 @@
+DESTDIR     = "/usr/local"
diff --git a/mpvc b/mpvc
@@ -0,0 +1,113 @@
+#!/bin/sh
+
+mpvfifo="/tmp/mpvfifo"
+
+print_help() {
+    echo "Usage: mpvc ACTION [ARGS]\n"
+    echo "ACTIONS:"
+    echo " append         append file to playlist."
+    echo " help           print this help message."
+    echo " load           load file, and override playlist."
+    echo " next           go to next track."
+    echo " prev           go to previous track."
+    echo " seek           seek (-)n seconds"
+    echo " toggle         toogle play and pause."
+    echo " lower          lower volume 10 units"
+    echo " mute           toggle mute"
+    echo " raise          raise volume 10 units"
+    echo " "
+    echo "EXTRA:"
+    echo " ytdl-format    set ytdl-format"
+    echo " "
+    echo "ALSA:"
+    echo " alower         alsa lower 5%"
+    echo " amute          alsa mute"
+    echo " araise         alsa raise 5%"
+}
+
+send_command() {
+    local input=""
+    for var in "$@"; do
+        [ "$input" != "" ] && input="$input, "
+	input="$input\"$var\""
+    done
+    printf '{ "command": [%s] }\n' "$input" > "$mpvfifo"
+}
+
+get_path() {
+    local path="$(readlink -f "$@")"
+    if [ -f "$path" ]; then
+        echo "$path"
+    else
+        echo "$@"
+    fi
+}
+
+# check if daemonization should apply
+if [ "${0##*/}" = "mpvd" ]; then
+    [ -p "$mpvfifo" ] && rm "$mpvfifo"
+
+    mkfifo "$mpvfifo"
+    chmod 600 "$mpvfifo"
+
+    while true; do
+        mpv --idle \
+            --input-file="$mpvfifo" \
+	    --ytdl-format=best \
+	    --load-scripts=yes
+    done
+    exit 0
+fi
+
+# check if fifo exists
+if [ ! -p "$mpvfifo" ]; then
+    echo "$tmpfifo is missing"
+    exit 1
+fi
+
+# print help if no argument was passed
+if [ $# -lt 1 ]; then
+    print_help
+    exit 0
+fi
+
+case "$1" in
+
+    # actions
+    load)
+        shift; send_command "loadfile" "$(get_path "$@")" ;;
+    append)
+        shift; send_command "loadfile" "$(get_path "$@")" "append" ;;
+    toggle)
+        send_command "cycle" "pause" ;;
+    prev)
+        send_command "playlist-prev" ;;
+    next)
+        send_command "playlist-next" ;;
+    seek)
+        shift; send_command "seek" "$@" ;;
+    mute)
+        send_command "cycle" "mute" ;;
+    raise)
+        send_command "add" "volume" "10" ;;
+    lower)
+        send_command "add" "volume" "-10" ;;
+
+    # extra
+    ytdl-format)
+        shift; send_command "set" "ytdl-format" "$@" ;;
+
+    # alsa
+    amute)
+        amixer set Master toggle ;;
+    araise)
+        amixer -q sset Master 5%+; killall -s 35 ztatus ;;
+    alower)
+        amixer -q sset Master 5%-; killall -s 35 ztatus ;;
+
+    # default behaviour
+    help)
+        print_help ;;
+    *)
+        echo "Unknown option: $*"; print_help ;;
+esac