dotfiles

My collection of dotfiles
git clone git://git.noxz.tech/dotfiles
Log | Files | Refs

commit 2f9a43400b297e42901177219f81b33e6df7ede3
parent bbb65c39bfb56f7a2c416ee8d184bf275993fc3b
Author: Chris Noxz <chris@noxz.tech>
Date:   Wed, 27 May 2020 18:57:20 +0200

[bin] fix tabs, add playlist features and directory parsing

Diffstat:
Mbin/.local/bin/mpvc | 312+++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 192 insertions(+), 120 deletions(-)

diff --git a/bin/.local/bin/mpvc b/bin/.local/bin/mpvc @@ -5,77 +5,81 @@ cmd_socat="$(command -v socat) - '$mpvdsock'" cmd_jq="$(command -v jq)" 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%" + echo "Usage: mpvc ACTION [ARGS]\n" + echo "ACTIONS:" + echo " append PATH append file to playlist." + echo " goto INDEX goto track index of playlist." + echo " help print this help message." + echo " list print playlist." + echo " load PATH load file or directory, and override playlist." + echo " lower lower volume 10 units" + echo " mute toggle mute" + echo " next go to next track." + echo " plmenu MODE launch playlist menu." + echo " prev go to previous track." + echo " raise raise volume 10 units" + echo " seek (-)N seek (-)n seconds" + echo " stop stop playback" + echo " toggle toogle play and pause." + echo " " + echo "EXTRA:" + echo " ytdl-format FMT 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, " + local input="" + for var in "$@"; do + [ "$input" != "" ] && input="$input, " input="$input\"$var\"" - done - printf '{ "command": [%s] }\n' "$input" | $cmd_socat + done + printf '{ "command": [%s] }\n' "$input" | $cmd_socat } get_path() { - local path="$(readlink -f "$@")" - if [ -f "$path" ]; then - echo "$path" - else - echo "$@" - fi + local path="$(readlink -f "$@")" + if [ -f "$path" ] || [ -d "$path" ]; then + echo "$path" + else + echo "$@" + fi } notify_vol() { - case $1 in - master) - local level="$(amixer sget Master | awk -F"[][]" '/dB/ { print ($2+0) }')" + case $1 in + master) + local level="$(amixer sget Master | awk -F"[][]" '/dB/ { print ($2+0) }')" local muted="$(amixer sget Master | awk -F"[][]" '/dB/ { print ($6) }' | sed 's/off/true/')" - ;; - mpv) - local level="$(send_command "get_property" "volume" | $cmd_jq -rM '.data')" - local muted="$(send_command "get_property" "mute" | $cmd_jq -rM '.data')" - ;; - *) return;; - esac - - echo $muted > /tmp/tttt - - local marks="$((($level + 5) / 10))" - [ "$marks" -gt 10 ] && marks=10 - - local output="$1" - if [ "$muted" = "true" ]; then - local output="$output (muted)" - fi - local output="$output: [$([ "$marks" -gt 0 ] && printf "%-$((marks))s" "+" | sed 's/\ /+/g')" - local output="$output$([ "$marks" -lt 10 ] && printf "%-$((10-marks))s" " ")]" - local output="$output$(printf " %3d" "$level")%" - ztatus-notify "$output" + ;; + mpv) + local level="$(send_command "get_property" "volume" | $cmd_jq -rM '.data')" + local muted="$(send_command "get_property" "mute" | $cmd_jq -rM '.data')" + ;; + *) return;; + esac + + echo $muted > /tmp/tttt + + local marks="$((($level + 5) / 10))" + [ "$marks" -gt 10 ] && marks=10 + + local output="$1" + if [ "$muted" = "true" ]; then + local output="$output (muted)" + fi + local output="$output: [$([ "$marks" -gt 0 ] && printf "%-$((marks))s" "+" | sed 's/\ /+/g')" + local output="$output$([ "$marks" -lt 10 ] && printf "%-$((10-marks))s" " ")]" + local output="$output$(printf " %3d" "$level")%" + ztatus-notify "$output" } die() { - printf "Error: %s\n" "$@" - exit 1 + printf "Error: %s\n" "$@" + exit 1 } # check if prerequisites exist @@ -84,18 +88,18 @@ die() { # check if daemonization should apply if [ "${0##*/}" = "mpvd" ]; then - if pidof -x "$(basename -- "$0")" -o $$ >/dev/null; then - die "Another instance of mpvd is already runnig..." - fi - [ -S "$mpvdsock" ] && rm "$mpvdsock" - while true; do - mpv --idle=yes \ - --input-ipc-server="$mpvdsock" \ - --ytdl-format=best \ - --load-scripts=yes \ - 2>&1 >/tmp/mpvd.log - done - exit 0 + if pidof -x "$(basename -- "$0")" -o $$ >/dev/null; then + die "Another instance of mpvd is already runnig..." + fi + [ -S "$mpvdsock" ] && rm "$mpvdsock" + while true; do + mpv --idle=yes \ + --input-ipc-server="$mpvdsock" \ + --ytdl-format=best \ + --load-scripts=yes \ + 2>&1 >/tmp/mpvd.log + done + exit 0 fi # check if socket exists @@ -103,58 +107,126 @@ fi # print help if no argument was passed if [ $# -lt 1 ]; then - print_help - exit 0 + print_help + exit 0 fi -case "$1" in +show_playlist() { + cmd="fzf" + [ "$1" = "x" ] && cmd="dmenu_vtc -l 20 -p playlist:" + script="$(realpath -s $0)" + list="$($script list)" + [ "$list" = "" ] && exit 1 + entry="$( + echo "$list" \ + | sed -e 's/^[ \t]*/ /' -e 's/^[^\w]*current[^\t]*\t/> /' \ + | awk '{printf "%-5s %s\n", (NR ": "), $0}' \ + | $cmd \ + )" + [ "$entry" != "" ] && $script goto "$((${entry%%:*}-1))" || exit 1 +} + +exif_extract() { + echo "$1" | grep ^"$2" | cut -d':' -f 2- | sed 's/^[ \t]*//' +} - # actions - load) - shift; send_command "loadfile" "$(get_path "$@")" ;; - append) - shift; send_command "loadfile" "$(get_path "$@")" "append" ;; - stop) - send_command "stop" ;; - toggle) - send_command "cycle" "pause" ;; - prev) - send_command "playlist-prev" ;; - next) - send_command "playlist-next" ;; - list) - send_command "get_property" "playlist" \ - | $cmd_jq -rM '.data[]|( - if .current then - "\\e[1;37mcurrent\\e[0m\\t" - else - "\\t" end - )+.filename' \ - | sed -e "s/\\\\t/\\t/g" -e "s/\\\\e/`printf "\033"`/g";; - seek) - shift; send_command "seek" "$@" ;; - mute) - send_command "cycle" "mute"; notify_vol mpv;; - raise) - send_command "add" "volume" "10"; notify_vol mpv;; - lower) - send_command "add" "volume" "-10"; notify_vol mpv;; - - # extra - ytdl-format) - shift; send_command "set" "ytdl-format" "$@" ;; - - # alsa - amute) - amixer set Master toggle; notify_vol master;; - araise) - amixer -q sset Master 5%+; notify_vol master;; - alower) - amixer -q sset Master 5%-; notify_vol master;; - - # default behaviour - help) - print_help ;; - *) - echo "Unknown option: $*"; print_help ;; +exif_wrapper() { + exif_data="$(exiftool -directory -filename -artist -title -duration "$@")" + exif_directory="$(exif_extract "$exif_data" "Directory")" + exif_filename="$(exif_extract "$exif_data" "File Name")" + exif_artist="$(exif_extract "$exif_data" "Artist")" + exif_title="$(exif_extract "$exif_data" "Title")" + exif_duration="$(exif_extract "$exif_data" "Duration")" + exif_artisttitle="$exif_filename" + + if [ "$exif_artist" != "" ] && [ "$exif_title" != "" ]; then + exif_artisttitle="$exif_artist - $exif_title" + fi + + exif_directory="$(get_path "$exif_directory")" + + # make sure duration is in the format of 'hh:mm:ss' then to seconds (awk) + exif_duration="$(echo "$exif_duration" | cut -d' ' -f 1)" + exif_duration="$( \ + printf '0:0:%s' "$exif_duration" \ + | rev | cut -d':' -f -3 | rev \ + | awk -F':' '{ print ($1 * 3600) + ($2 * 60) + $3 }' \ + )" + + echo "$(printf '#EXTINF:%s,%s\n%s/%s\n' \ + "$exif_duration" \ + "$exif_artisttitle" \ + "$exif_directory" \ + "$exif_filename" \ + )" +} + +case "$1" in + # actions + load) + shift; + playlist="$(get_path "$@")" + if [ -d "$playlist" ]; then + playlist_tmp="$(mktemp /tmp/pls.mpvc.XXXXXXXXXX)" + echo '#EXTM3U' > "$playlist_tmp" + find "$playlist" -maxdepth 2 -type f | while read -r f; do + case "$f" in + *.flac | *.mp3 | *.ogg) + exif_wrapper "$f" >> "$playlist_tmp" ;; + *) continue ;; + esac + done + playlist="$playlist_tmp" + fi + send_command "loadfile" "$playlist" + [ "$playlist_tmp" != "" ] && (sleep 10 && rm "$playlist_tmp") & + ;; + append) + shift; send_command "loadfile" "$(get_path "$@")" "append" ;; + stop) + send_command "stop" ;; + toggle) + send_command "cycle" "pause" ;; + prev) + send_command "playlist-prev" ;; + next) + send_command "playlist-next" ;; + goto) + shift; send_command "set_property" "playlist-pos" "$@" ;; + list) + send_command "get_property" "playlist" \ + | $cmd_jq -rM '.data[]|( + if .current then + "\\e[1;37mcurrent\\e[0m\\t" + else + "\\t" end + )+(.title//.filename)' \ + | sed -e "s/\\\\t/\\t/g" -e "s/\\\\e/`printf "\033"`/g";; + plmenu) shift; show_playlist "$@" ;; + seek) + shift; send_command "seek" "$@" ;; + mute) + send_command "cycle" "mute"; notify_vol mpv;; + raise) + send_command "add" "volume" "10"; notify_vol mpv;; + lower) + send_command "add" "volume" "-10"; notify_vol mpv;; + + # extra + ytdl-format) + shift; send_command "set" "ytdl-format" "$@" ;; + + # alsa + amute) + amixer set Master toggle; notify_vol master;; + araise) + amixer -q sset Master 5%+; notify_vol master;; + alower) + amixer -q sset Master 5%-; notify_vol master;; + + # default behaviour + help) + print_help ;; + *) + echo "Unknown option: $*"; print_help ;; esac