2019-02-14 11:17:16 +00:00
|
|
|
#!/usr/bin/env bash
|
2019-02-14 08:11:30 +00:00
|
|
|
# ┬┌─┬ ┬┌┐┌┌─┐┌┬┐
|
2019-02-17 12:05:53 +00:00
|
|
|
# ├┴┐│ ││││└─┐ │
|
|
|
|
# ┴ ┴└─┘┘└┘└─┘ ┴
|
2019-02-14 08:11:30 +00:00
|
|
|
# Created by Siddharth Dushantha
|
2019-02-27 08:57:49 +00:00
|
|
|
#
|
2020-03-27 19:05:03 +00:00
|
|
|
# Dependencies: sxiv, imagemagick, bash, ffmpeg, mpc, jq, mpd
|
2019-02-27 08:57:49 +00:00
|
|
|
|
2020-03-27 19:05:03 +00:00
|
|
|
|
|
|
|
VERSION=1.2.4
|
2019-02-13 09:33:48 +00:00
|
|
|
COVER=/tmp/kunst.jpg
|
2019-08-01 07:54:40 +01:00
|
|
|
MUSIC_DIR=~/Music/
|
2020-03-27 19:05:03 +00:00
|
|
|
SIZE=250x250
|
2019-10-14 13:27:05 +01:00
|
|
|
POSITION="+0+0"
|
2019-02-19 09:56:11 +00:00
|
|
|
|
2020-03-27 19:05:03 +00:00
|
|
|
|
2019-02-19 16:41:58 +00:00
|
|
|
show_help() {
|
2020-03-27 19:05:03 +00:00
|
|
|
printf "%s" "\
|
|
|
|
usage: kunst [-h] [--size \"px\"] [--position \"+x+y\"][--music_dir \"path/to/dir\"] [--silent] [--version]
|
|
|
|
|
|
|
|
┬┌─┬ ┬┌┐┌┌─┐┌┬┐
|
|
|
|
├┴┐│ ││││└─┐ │
|
|
|
|
┴ ┴└─┘┘└┘└─┘ ┴
|
|
|
|
Download and display album art or display embedded album art
|
|
|
|
|
|
|
|
optional arguments:
|
|
|
|
-h, --help show this help message and exit
|
|
|
|
--size what size to display the album art in
|
|
|
|
--position the position where the album art should be displayed
|
|
|
|
--music_dir the music directory which MPD plays from
|
|
|
|
--silent dont show the output
|
|
|
|
--version show the version of kunst you are using
|
|
|
|
"
|
2019-02-19 16:41:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:05:03 +00:00
|
|
|
|
2019-03-05 16:03:43 +00:00
|
|
|
# Parse the arguments
|
2019-10-14 13:27:37 +01:00
|
|
|
options=$(getopt -o h --long position:,size:,music_dir:,version,silent,help -- "$@")
|
2019-02-19 09:56:11 +00:00
|
|
|
eval set -- "$options"
|
|
|
|
|
|
|
|
while true; do
|
2020-03-27 19:05:03 +00:00
|
|
|
case "$1" in
|
|
|
|
--size)
|
|
|
|
shift;
|
|
|
|
SIZE=$1
|
|
|
|
;;
|
|
|
|
--position)
|
|
|
|
shift;
|
|
|
|
POSITION=$1
|
|
|
|
;;
|
|
|
|
--music_dir)
|
|
|
|
shift;
|
|
|
|
MUSIC_DIR=$1
|
|
|
|
;;
|
|
|
|
-h|--help)
|
|
|
|
show_help
|
2019-02-19 09:56:11 +00:00
|
|
|
exit
|
2020-03-27 19:05:03 +00:00
|
|
|
;;
|
2019-02-19 16:41:58 +00:00
|
|
|
--version)
|
2020-03-27 19:05:03 +00:00
|
|
|
echo $VERSION
|
2019-02-19 16:41:58 +00:00
|
|
|
exit
|
|
|
|
;;
|
2020-03-27 19:05:03 +00:00
|
|
|
--silent)
|
|
|
|
SILENT=true
|
|
|
|
;;
|
|
|
|
--)
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
2019-02-19 09:56:11 +00:00
|
|
|
done
|
2019-02-13 09:33:48 +00:00
|
|
|
|
2019-03-05 16:03:43 +00:00
|
|
|
# This is a base64 endcoded image which will be used if
|
|
|
|
# the file does not have an emmbeded album art.
|
|
|
|
# The image is an image of a music note
|
2020-03-27 19:05:03 +00:00
|
|
|
read -r -d '' MUSIC_NOTE << EOF
|
2019-02-13 20:37:43 +00:00
|
|
|
iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAJESURBVGhD7Zg/axRRFMVXAtpYphEVREKClnHfJI0MmReSfAC3tRejhaBgo70fwN7aD2BvEU0gfztbu5AqMxNjoVnvG87KZXy7z5m5dxLI/OCw8Pade+7M3n3Dbq+jo6OjY8RwMJhKk+hhlph3eRJ9w/LF5jCOr1PTj6jpD7mNjkjDkbDl4vFjpX87teZJlkSfSD9501zYfv5QJ1fyZHGexuJtZs12ZqMzX8NlwX4+nK3NXMutWaOm39Nd/u5rMCSUao80fjBNwY+p8Y+krNxQVaGsLsfWzFLYS2r4M30Rf5WbaCJE6OILlhIidPEFSwkRuviCpYQIXXzB1WX26bR6ky4v3OPriNCFB1YRHa079Pr6eKk/h1IFfA+WdOGBk+QeXtT0Ft3pV6e2fxf2f+AeLOnCA8tC0xv09H1xGi/cgWUi3I8lXXigEzX8u3gmWPP8JI5uYdt/w2thSRceSM0/zVfnb+CtWvB6WNJFOlC6XhDpQOl6QaQDpesFkQ6UrhdEOlC6XpA6gcPB/avumKXnxCadXHkha766tTr1GlE18CRZvEmN7nHfOMGiS5XA4mdmYg64Z5Jg06VKYHlEQoKtOVIz6zx8f0iwNUNyZt2F+3zjBFt9pGe22gWYFLb6lEckJNjGUmWEssR8ga0+0jNL9Z75fD7Rp7UOW32kZxb/1u37vFyUu+sODtjqozGzxaFADfprFM3vuD3Y3gytmf17LJPHXbgTNb5BWhe58yNan1lpWp9ZDVqdWS1am9mOjo7LRq/3B1ESKyYUVquzAAAAAElFTkSuQmCC
|
|
|
|
EOF
|
2019-02-13 09:33:48 +00:00
|
|
|
|
2020-03-27 19:05:03 +00:00
|
|
|
|
2019-02-19 05:51:14 +00:00
|
|
|
is_connected() {
|
2019-03-05 16:03:43 +00:00
|
|
|
# Check if internet is connected. We are using api.deezer.com to test
|
2020-03-27 19:05:03 +00:00
|
|
|
# if the internet is connected because if api.deezer.com is down or
|
2019-02-22 06:10:50 +00:00
|
|
|
# the internet is not connected this script will work as expected
|
|
|
|
if ping -q -c 1 -W 1 api.deezer.com >/dev/null; then
|
2019-02-19 05:51:14 +00:00
|
|
|
connected=true
|
|
|
|
else
|
2020-03-27 19:05:03 +00:00
|
|
|
[ ! "$SILENT" ] && echo "kunst: unable to check online for the album art"
|
2019-02-19 05:51:14 +00:00
|
|
|
connected=false
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:05:03 +00:00
|
|
|
|
2019-02-18 19:57:53 +00:00
|
|
|
get_cover_online() {
|
2019-03-05 16:03:43 +00:00
|
|
|
# Check if connected to internet
|
2019-02-19 05:51:14 +00:00
|
|
|
is_connected
|
|
|
|
|
2020-03-27 19:05:03 +00:00
|
|
|
if [ "$connected" == false ];then
|
2019-02-19 05:51:14 +00:00
|
|
|
ARTLESS=true
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2019-03-05 16:03:43 +00:00
|
|
|
# Try to get the album cover online from api.deezer.com
|
2020-02-29 02:13:20 +00:00
|
|
|
API_URL="https://api.deezer.com/search/autocomplete?q=$(mpc current)" && API_URL=${API_URL//' '/'%20'}
|
2019-02-18 19:57:53 +00:00
|
|
|
|
2019-03-05 16:03:43 +00:00
|
|
|
# Extract the albumcover from the json returned
|
2020-03-27 19:05:03 +00:00
|
|
|
IMG_URL=$(curl -s "$API_URL" | jq -r '.playlists.data[0] | .picture_big')
|
2019-06-02 14:27:45 +01:00
|
|
|
|
2020-03-27 19:05:03 +00:00
|
|
|
if [ "$IMG_URL" = '' ] || [ "$IMG_URL" = 'null' ];then
|
|
|
|
[ ! "$SILENT" ] && echo "error: cover not found online"
|
2019-02-18 19:57:53 +00:00
|
|
|
ARTLESS=true
|
|
|
|
else
|
2020-03-27 19:05:03 +00:00
|
|
|
[ ! "$SILENT" ] && echo "kunst: cover found online"
|
|
|
|
curl -o "$COVER" -s "$IMG_URL"
|
2019-02-18 19:57:53 +00:00
|
|
|
ARTLESS=false
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:05:03 +00:00
|
|
|
|
2019-02-13 09:33:48 +00:00
|
|
|
update_cover() {
|
2019-03-05 16:03:43 +00:00
|
|
|
# Extract the album art from the mp3 file and dont show the messsy
|
2019-02-13 09:33:48 +00:00
|
|
|
# output of ffmpeg
|
2020-03-27 19:05:03 +00:00
|
|
|
ffmpeg -i "$MUSIC_DIR$(mpc current -f %file%)" "$COVER" -y &> /dev/null
|
2019-02-14 05:21:15 +00:00
|
|
|
|
2019-03-05 16:03:43 +00:00
|
|
|
# Get the status of the previous command
|
2019-02-13 20:37:43 +00:00
|
|
|
STATUS=$?
|
2019-02-14 05:21:15 +00:00
|
|
|
|
2019-03-05 16:03:43 +00:00
|
|
|
# Check if the file has a embbeded album art
|
2020-03-27 19:05:03 +00:00
|
|
|
if [ "$STATUS" -eq 0 ];then
|
|
|
|
[ ! "$SILENT" ] && echo "kunst: extracted album art"
|
2019-02-13 20:37:43 +00:00
|
|
|
ARTLESS=false
|
|
|
|
else
|
2020-03-27 19:05:03 +00:00
|
|
|
DIR="$MUSIC_DIR$(dirname "$(mpc current -f %file%)")"
|
|
|
|
[ ! "$SILENT" ] && echo "kunst: inspecting $DIR"
|
2019-02-21 15:49:27 +00:00
|
|
|
|
2019-03-05 16:03:43 +00:00
|
|
|
# Check if there is an album cover/art in the folder.
|
|
|
|
# Look at issue #9 for more details
|
2020-03-27 19:05:03 +00:00
|
|
|
for CANDIDATE in "$DIR/cover."{png,jpg}; do
|
|
|
|
if [ -f "$CANDIDATE" ]; then
|
|
|
|
STATUS=0
|
|
|
|
ARTLESS=false
|
|
|
|
convert "$CANDIDATE" $COVER &> /dev/null
|
|
|
|
[ ! "$SILENT" ] && echo "kunst: found cover.png"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$STATUS" -ne 0 ];then
|
|
|
|
[ ! "$SILENT" ] && echo "error: file does not have an album art"
|
2019-02-18 19:57:53 +00:00
|
|
|
get_cover_online
|
2019-02-13 20:37:43 +00:00
|
|
|
fi
|
2019-02-13 09:33:48 +00:00
|
|
|
|
2019-03-05 16:03:43 +00:00
|
|
|
# Resize the image to 250x250
|
2020-03-27 19:05:03 +00:00
|
|
|
if [ "$ARTLESS" == false ]; then
|
|
|
|
convert "$COVER" -resize "$SIZE" "$COVER" &> /dev/null
|
|
|
|
[ ! "$SILENT" ] && echo "kunst: resized album art to $SIZE"
|
2019-02-18 20:20:24 +00:00
|
|
|
fi
|
2019-02-13 09:33:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-04 18:49:53 +00:00
|
|
|
pre_exit() {
|
2019-03-05 18:47:54 +00:00
|
|
|
# Get the proccess ID of kunst and kill it.
|
2020-03-27 19:05:03 +00:00
|
|
|
# We are dumping the output of kill to /dev/null
|
|
|
|
# because if the user quits sxiv before they
|
|
|
|
# exit kunst, an error will be shown
|
|
|
|
# from kill and we dont want that
|
|
|
|
kill -9 "$(cat /tmp/kunst.pid) &> /dev/null"
|
|
|
|
|
2019-03-04 18:49:53 +00:00
|
|
|
}
|
2019-02-13 09:33:48 +00:00
|
|
|
|
|
|
|
main() {
|
2020-03-27 19:05:03 +00:00
|
|
|
|
|
|
|
[ "$KUNST_MUSIC_DIR" != "" ] && MUSIC_DIR="$KUNST_MUSIC_DIR"
|
|
|
|
[ "$KUNST_SIZE" != "" ] && SIZE="$KUNST_SIZE"
|
|
|
|
[ "$KUNST_POSITION" != "" ] && POSITION="$KUNST_POSITION"
|
2019-02-13 09:33:48 +00:00
|
|
|
|
2019-03-05 16:03:43 +00:00
|
|
|
# Flag to run some commands only once in the loop
|
2019-02-19 12:58:32 +00:00
|
|
|
FIRST_RUN=true
|
2019-06-02 14:27:45 +01:00
|
|
|
|
2019-02-13 09:33:48 +00:00
|
|
|
while true; do
|
2019-06-02 14:27:45 +01:00
|
|
|
|
2019-02-13 20:37:43 +00:00
|
|
|
update_cover
|
2019-06-02 14:27:45 +01:00
|
|
|
|
2020-03-27 19:05:03 +00:00
|
|
|
if [ "$ARTLESS" == true ];then
|
2019-03-05 16:03:43 +00:00
|
|
|
# Dhange the path to COVER because the music note
|
2019-02-14 05:21:15 +00:00
|
|
|
# image is a png not jpg
|
2019-06-02 14:27:45 +01:00
|
|
|
COVER=/tmp/kunst.png
|
2019-02-14 05:21:15 +00:00
|
|
|
|
2019-03-05 16:03:43 +00:00
|
|
|
# Decode the base64 encoded image and save it
|
2019-02-14 05:21:15 +00:00
|
|
|
# to /tmp/kunst.png
|
2020-03-27 19:05:03 +00:00
|
|
|
echo "$MUSIC_NOTE" | base64 --decode > "$COVER"
|
2020-03-06 15:27:30 +00:00
|
|
|
fi
|
2020-03-27 19:05:03 +00:00
|
|
|
|
|
|
|
if [ ! "$SILENT" ];then
|
|
|
|
echo "kunst: swapped album art to $(mpc current)"
|
|
|
|
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
|
|
|
|
fi
|
2020-03-06 15:27:30 +00:00
|
|
|
|
2020-03-27 19:05:03 +00:00
|
|
|
if [ "$FIRST_RUN" == true ]; then
|
2019-02-19 12:58:32 +00:00
|
|
|
FIRST_RUN=false
|
|
|
|
|
2020-03-27 19:05:03 +00:00
|
|
|
# Display the album art using sxiv
|
|
|
|
sxiv -g "$SIZE$POSITION" -b "$COVER" -N "Kunst" &
|
|
|
|
|
|
|
|
# Save the process ID so that we can kill
|
|
|
|
# sxiv when the user exits the script
|
2019-03-04 18:49:53 +00:00
|
|
|
echo $! >/tmp/kunst.pid
|
2019-02-19 12:58:32 +00:00
|
|
|
fi
|
|
|
|
|
2019-03-05 16:03:43 +00:00
|
|
|
# Waiting for an event from mpd; play/pause/next/previous
|
2019-02-19 12:58:32 +00:00
|
|
|
# this is lets kunst use less CPU :)
|
2019-08-01 11:35:07 +01:00
|
|
|
while true; do
|
2020-03-27 19:05:03 +00:00
|
|
|
mpc idle player &>/dev/null && (mpc status | grep "\[playing\]" &>/dev/null) && break
|
2019-08-01 11:35:07 +01:00
|
|
|
done
|
2020-03-27 19:05:03 +00:00
|
|
|
[ ! "$SILENT" ] && echo "kunst: received event from mpd"
|
2019-02-13 09:33:48 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:03:43 +00:00
|
|
|
# Disable CTRL-Z because if we allowed this key press,
|
2020-03-27 19:05:03 +00:00
|
|
|
# then the script would exit but, sxiv would still be
|
|
|
|
# running
|
2019-03-05 16:03:43 +00:00
|
|
|
trap "" SIGTSTP
|
|
|
|
|
2019-03-04 18:49:53 +00:00
|
|
|
trap pre_exit EXIT
|
2019-02-13 09:33:48 +00:00
|
|
|
main
|