From 5da221830d023589b1f7ce10145b241ec1694d48 Mon Sep 17 00:00:00 2001 From: Donovan Glover Date: Thu, 1 Jun 2023 15:02:10 -0400 Subject: [PATCH] ncmpcpp: Add on-song-change script Now the cover image and artist + title shows on song change. --- terminal/ncmpcpp/default.nix | 4 +++- terminal/ncmpcpp/on-song-change.sh | 32 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100755 terminal/ncmpcpp/on-song-change.sh diff --git a/terminal/ncmpcpp/default.nix b/terminal/ncmpcpp/default.nix index 9a716a0..17d9fc6 100644 --- a/terminal/ncmpcpp/default.nix +++ b/terminal/ncmpcpp/default.nix @@ -10,6 +10,8 @@ ''; }; + xdg.configFile."ncmpcpp/on-song-change.sh".source = ./on-song-change.sh; + programs.ncmpcpp = { enable = true; @@ -138,7 +140,7 @@ autocenter_mode = "yes"; allow_for_physical_item_deletion = "no"; mouse_support = "no"; - execute_on_song_change = "~/.config/mpd/mpdnotify"; + execute_on_song_change = "~/.config/ncmpcpp/on-song-change.sh"; mpd_crossfade_time = 3; }; }; diff --git a/terminal/ncmpcpp/on-song-change.sh b/terminal/ncmpcpp/on-song-change.sh new file mode 100755 index 0000000..881fa5e --- /dev/null +++ b/terminal/ncmpcpp/on-song-change.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +music_dir="$HOME/Music" +fallback_image="$HOME/a.jpg" + +main () { + find_cover 2>/dev/null + send 2>/dev/null +} + +find_cover () { + ext="$(mpc --format %file% current | sed 's/^.*\.//')" + if [ "$ext" != "flac" ]; then + ffmpeg -y -i "$(mpc --format "$music_dir"/%file% | head -n 1)" \ + /tmp/cover.jpg && cover_path="/tmp/cover.jpg" && return + else + # ffmpeg doesn't work for flacs + metaflac --export-picture-to=/tmp/cover.jpg \ + "$(mpc --format "$music_dir"/%file% current)" && cover_path="/tmp/cover.jpg" && return + fi + #without embedded images this is used as a fallback + file="$music_dir/$(mpc --format %file% current)" + album="${file%/*}" + #search for cover image + cover_path=$(find "$album" -maxdepth 1 | grep -m 1 ".*\.\(jpg\|png\|gif\|bmp\)") +} + +send () { + notify-send -i "${cover_path:-$fallback_image}" "Now Playing" "$(mpc current)" +} + +main