Skip to content

Instantly share code, notes, and snippets.

@mibli
Created January 7, 2017 14:14
Show Gist options
  • Save mibli/7505146987012fba7b30af737d6bc0f2 to your computer and use it in GitHub Desktop.
Save mibli/7505146987012fba7b30af737d6bc0f2 to your computer and use it in GitHub Desktop.
MPV persistant ipc volume control. (WIP)
#!/bin/bash
# -------------------
# MPV CONTROL RELATED
function mpv_get_config()
{
cat ${mpv_config_path} | grep $1 | cut -f2 -d'=' ||
echo "No $1 property in config"
}
function mpv_set_config()
{
sed -i "s/^\($1=\).*$/\1$2/" ${mpv_config_path} ||
echo $1=$2 >> ${mpv_config_path}
}
function ipc_set_property()
{
cmd_string='{ "command": ["set_property_string", "'$1'", "'$2'"] }'
socat - "$mpv_ipc_path" <<< $cmd_string | grep '"error":"success"'
return $?
}
function cmd_volume()
{
local volume="$(mpv_get_config volume)"
: ${volume:="50"}
local direction=${1:0:1}
local value="$(egrep -o [0-9]+ <<< "$1")"
[[ -n "value" ]] || ( echo "Couldn't parse a value"; exit 1 )
case $direction in
+) (( volume += value )) ;;
-) (( volume -= value )) ;;
*) volume="$value" ;;
esac
ipc_set_property volume $volume &&
mpv_set_config volume $volume &&
echo "Volume set to $value"
}
# --------------
# PROGRAM CONFIG
function get_config()
{
cat ${config_path} | grep $1 | cut -f2 -d'=' || echo "No $1 property in config"
}
function set_config()
{
sed -i "s/^\($1=\).*$/\1$2/" ${config_path} ||
echo $1=$2 >> ${config_path}
}
function find_config()
{
local config_paths=(
"$HOME/.mpvctlrc"
"$HOME/config/mpv/mpvctl.conf"
"/etc/mpvctlrc"
)
local broke=false
for path in ${config_paths[@]}; do
[ -f "$path" ] && echo "$path" && return
done
echo "${config_paths[1]}"
}
function main()
{
while getopts ":a" opt; do
case $opt in
c) config_path="$OPTARG" ;;
s) mpv_ipc_path="$OPTARG" ;;
?,h)
echo << EOF
Usage:
$0 [-c] [-s] command [value]
-c config file (default: ~/.config/mpv/mpv.conf)
-s socket file (default from config)
Supported commands:
volume [(+|-)]volume
EOF
;;
\?) break ;;
esac
shift
done
set -e
local cmd="cmd_$1"; shift
: ${config_path:="$(find_config)"}
: ${mpv_config_path:="$(echo "$(eval echo "$(get_config mpv-config)")")"}
: ${mpv_ipc_path:="$(get_config mpv-ipc)"}
: ${mpv_config_path:="$HOME/.config/mpv/mpv.conf"}
: ${mpv_ipc_path:="$(mpv_get_config input-ipc-server)"}
echo $config_path
$cmd "$@"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment