diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2023-05-17 00:09:51 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2023-05-17 00:09:51 +0200 |
| commit | 5d3c0b24405381a41bd13a3135fa2969c2b47a48 (patch) | |
| tree | c59a2521159b90daac9e139e5ebaeaf24dd3fa0f /scripts/musiccmd | |
| parent | 436159f3542d4e11c81ff927ab6b22f5ad2619e0 (diff) | |
| parent | ca1fba1ef518b2f55bc168e9782b500dfec67c16 (diff) | |
Merge branch 'main' of https://github.com/Laitram31/config
Diffstat (limited to 'scripts/musiccmd')
| -rwxr-xr-x | scripts/musiccmd | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/scripts/musiccmd b/scripts/musiccmd new file mode 100755 index 0000000..ecaf437 --- /dev/null +++ b/scripts/musiccmd @@ -0,0 +1,116 @@ +#!/bin/sh +# helper for the music script +# dependencies: music, mpv, socat, yt-dlp, ffmpeg, imagemagick +# optional dependencies: dwmblocks, libnotify, herbe + +FAVDIR="${XDG_MUSIC_DIR:-$HOME/music}/favorite" +DLDIR="${XDG_MUSIC_DIR:-$HOME/music}/download" +CACHE="${XDG_CACHE_HOME:-$HOME/.cache}" +IMG="nsxiv" + +getpath() { + tmp="$(printf '{ "command": ["get_property", "path"] }\n'\ + | socat - /tmp/mpvsocket)" + tmp="${tmp#{\"data\":\"}" + path="${tmp%\",\"request_id\":0,\"error\":\"success\"\}}" +} + +getvol() { + tmp="$(printf '{ "command": ["get_property", "volume"] }\n'\ + | socat - /tmp/mpvsocket)" + tmp="${tmp#{\"data\":}" + printf '%s\n' "${tmp%.000000,\"request_id\":0,\"error\":\"success\"\}}" +} + +download() { + mkdir -p "$1" + notify-send "Downloading '$path' to '$1'" + if yt-dlp -f "ba" -x --embed-thumbnail --audio-format mp3\ + -o"%(title)s [%(id)s].%(ext)s" -P "$1" "$path"; then + notify-send "'$path' successfuly downloaded to '$2'" + return 0 + else + notify-send "Error: '$path' couldn't be downloaded" + return 1 + fi +} + +addtofav() { + case $path in + *youtube.com*) + download "$FAVDIR" ;; + *) + mkdir -p "$FAVDIR" + if cp "$path" "$FAVDIR"; then + notify-send "'$path' was copied to '$FAVDIR'" + return 0 + else + notify-send "'$path' couldn't be copied to '$FAVDIR'" + return 1 + fi ;; + esac +} + +getthumbnail() { + mkdir -p "$CACHE" + case $path in + *youtube.com*) + yt-dlp --skip-download --force-overwrites --write-thumbnail\ + -o "$CACHE/thumbnail" "$path" > /dev/null 2>&1 &&\ + magick "$CACHE/thumbnail.webp" "$CACHE/thumbnail.jpg" ;; + *) + ffmpeg -y -i "$path" "$CACHE/thumbnail.jpg" 2> /dev/null ;; + esac +} + + +main() { + if ! pgrep -x music >/dev/null; then + kill -35 "$(pidof dwmblocks)" + herbe "Error: There is no music playing"\ + "Do you want to play some ?" && + music + return 1 + fi + + cmd="${1:-$(printf '⏯️pause/play\n⏭️next\n⏮️prev\n📢volume\n🔳stop +⭐favorite\n⬇️download\n🖼️thumbnail' | dmenu -i -p "musiccmd")}" + + case "$cmd" in + cycle|*pause|*play) + printf 'cycle pause\n' | socat - /tmp/mpvsocket + kill -35 "$(pidof dwmblocks)" ;; + *next) + printf 'playlist-next\n' | socat - /tmp/mpvsocket ;; + *prev) + printf 'playlist-prev\n' | socat - /tmp/mpvsocket ;; + *volume) + printf '{ "command": ["set_property", "volume", %s] }\n'\ + "${2:-$(true | dmenu -p "Current volume: $(getvol)")}"\ + | socat - /tmp/mpvsocket 1> /dev/null ;; + *stop) + printf 'stop\n' | socat - /tmp/mpvsocket + kill -35 "$(pidof dwmblocks)" ;; + *favorite|fav) + getpath + addtofav ;; + *download) + getpath + download "$DLDIR" ;; + *thumbnail) + getpath + if getthumbnail; then + $IMG "$CACHE/thumbnail.jpg" + else + notify-send "Error: can't get thumbnail" + return 1 + fi ;; + path) + getpath + printf '%s\n' "$path" ;; + vol) + getvol ;; + esac +} + +main "$@" |
