Fixed the whole thing

This commit is contained in:
Siddharth Dushantha 2020-03-27 20:05:03 +01:00
parent 4dd9bb7650
commit 03e3520311
1 changed files with 106 additions and 129 deletions

235
kunst
View File

@ -4,105 +4,99 @@
# ┴ ┴└─┘┘└┘└─┘ ┴ # ┴ ┴└─┘┘└┘└─┘ ┴
# Created by Siddharth Dushantha # Created by Siddharth Dushantha
# #
# Dependencies: sxiv or imv, bash, ffmpeg, mpc, jq, mpd # Dependencies: sxiv, imagemagick, bash, ffmpeg, mpc, jq, mpd
VERSION=1.2.6
VERSION=1.2.4
COVER=/tmp/kunst.jpg COVER=/tmp/kunst.jpg
MUSIC_DIR=~/Music/ MUSIC_DIR=~/Music/
WIDTH=250 SIZE=250x250
SIZE="${WIDTH}x${WIDTH}"
POSITION="+0+0" POSITION="+0+0"
show_help() { show_help() {
echo "usage: kunst [-h] [--size "px"] [--position "+x+y"] [--viewer <imv|x11>] [--music_dir "path/to/dir"] [--silent] [--version]" printf "%s" "\
echo " " usage: kunst [-h] [--size \"px\"] [--position \"+x+y\"][--music_dir \"path/to/dir\"] [--silent] [--version]
echo "┬┌─┬ ┬┌┐┌┌─┐┌┬┐"
echo "├┴┐│ ││││└─┐ │" ┬┌─┬ ┬┌┐┌┌─┐┌┬┐
echo "┴ ┴└─┘┘└┘└─┘ ┴" ├┴┐│ ││││└─┐ │
echo "Download and display album art or display embedded album art" ┴ ┴└─┘┘└┘└─┘ ┴
echo " " Download and display album art or display embedded album art
echo "optional arguments:"
echo " -h, --help show this help message and exit" optional arguments:
echo " --size what size to display the album art in" -h, --help show this help message and exit
echo " --position the position where the album art should be displayed" --size what size to display the album art in
echo " --viewer whether to use imv or sxiv" --position the position where the album art should be displayed
echo " --music_dir the music directory which MPD plays from" --music_dir the music directory which MPD plays from
echo " --silent dont show the output" --silent dont show the output
echo " --version show the version of kunst you are using" --version show the version of kunst you are using
"
} }
# Parse the arguments # Parse the arguments
options=$(getopt -o h --long position:,size:,music_dir:,version,silent,help -- "$@") options=$(getopt -o h --long position:,size:,music_dir:,version,silent,help -- "$@")
eval set -- "$options" eval set -- "$options"
while true; do while true; do
case "$1" in case "$1" in
--size) --size)
shift shift;
SIZE=$1 SIZE=$1
;; ;;
--position) --position)
shift shift;
POSITION=$1 POSITION=$1
;; ;;
--viewer) --music_dir)
shift shift;
VIEWER=$1 MUSIC_DIR=$1
;; ;;
--music_dir) -h|--help)
shift show_help
MUSIC_DIR=$1
;;
-h | --help)
show_help
exit exit
;; ;;
--version) --version)
echo $VERSION echo $VERSION
exit exit
;; ;;
--silent) --silent)
SILENT=true SILENT=true
;; ;;
--) --)
shift shift
break break
;; ;;
esac esac
shift shift
done done
# If available, use imv instead of sxiv on Wayland,
# unless specified otherwise by the user.
if [ -n "$SWAYSOCK" ] || [ "$XDG_SESSION_TYPE" = 'wayland' ] \
&& [ -z "$VIEWER" ] && command -v imv >/dev/null; then
VIEWER=imv
fi
# This is a base64 endcoded image which will be used if # This is a base64 endcoded image which will be used if
# the file does not have an emmbeded album art. # the file does not have an emmbeded album art.
# The image is an image of a music note # The image is an image of a music note
read -d '' MUSIC_NOTE <<EOF read -r -d '' MUSIC_NOTE << EOF
iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAJESURBVGhD7Zg/axRRFMVXAtpYphEVREKClnHfJI0MmReSfAC3tRejhaBgo70fwN7aD2BvEU0gfztbu5AqMxNjoVnvG87KZXy7z5m5dxLI/OCw8Pade+7M3n3Dbq+jo6OjY8RwMJhKk+hhlph3eRJ9w/LF5jCOr1PTj6jpD7mNjkjDkbDl4vFjpX87teZJlkSfSD9501zYfv5QJ1fyZHGexuJtZs12ZqMzX8NlwX4+nK3NXMutWaOm39Nd/u5rMCSUao80fjBNwY+p8Y+krNxQVaGsLsfWzFLYS2r4M30Rf5WbaCJE6OILlhIidPEFSwkRuviCpYQIXXzB1WX26bR6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rhdEOlC6XpA6gcPB/avumKXnxCadXHkha766tTr1GlE18CRZvEmN7nHfOMGiS5XA4mdmYg64Z5Jg06VKYHlEQoKtOVIz6zx8f0iwNUNyZt2F+3zjBFt9pGe22gWYFLb6lEckJNjGUmWEssR8ga0+0jNL9Z75fD7Rp7UOW32kZxb/1u37vFyUu+sODtjqozGzxaFADfprFM3vuD3Y3gytmf17LJPHXbgTNb5BWhe58yNan1lpWp9ZDVqdWS1am9mOjo7LRq/3B1ESKyYUVquzAAAAAElFTkSuQmCC
EOF EOF
is_connected() { is_connected() {
# Check if internet is connected. We are using api.deezer.com to test # Check if internet is connected. We are using api.deezer.com to test
# if the internet is connected because if api.deezer.com is down or # if the internet is connected because if api.deezer.com is down or
# the internet is not connected this script will work as expected # the internet is not connected this script will work as expected
if ping -q -c 1 -W 1 api.deezer.com >/dev/null; then if ping -q -c 1 -W 1 api.deezer.com >/dev/null; then
connected=true connected=true
else else
[ ! $SILENT ] && echo "kunst: unable to check online for the album art" [ ! "$SILENT" ] && echo "kunst: unable to check online for the album art"
connected=false connected=false
fi fi
} }
get_cover_online() { get_cover_online() {
# Check if connected to internet # Check if connected to internet
is_connected is_connected
if [ $connected == false ]; then if [ "$connected" == false ];then
ARTLESS=true ARTLESS=true
return return
fi fi
@ -111,81 +105,76 @@ get_cover_online() {
API_URL="https://api.deezer.com/search/autocomplete?q=$(mpc current)" && API_URL=${API_URL//' '/'%20'} API_URL="https://api.deezer.com/search/autocomplete?q=$(mpc current)" && API_URL=${API_URL//' '/'%20'}
# Extract the albumcover from the json returned # Extract the albumcover from the json returned
IMG_URL=$(curl -s --ssl "$API_URL" | jq -r '.playlists.data[0] | .picture_big') IMG_URL=$(curl -s "$API_URL" | jq -r '.playlists.data[0] | .picture_big')
if [ "$IMG_URL" = '' ] || [ "$IMG_URL" = 'null' ]; then if [ "$IMG_URL" = '' ] || [ "$IMG_URL" = 'null' ];then
[ ! $SILENT ] && echo "error: cover not found online" [ ! "$SILENT" ] && echo "error: cover not found online"
ARTLESS=true ARTLESS=true
else else
[ ! $SILENT ] && echo "kunst: cover found online" [ ! "$SILENT" ] && echo "kunst: cover found online"
curl -o $COVER -s --ssl $IMG_URL curl -o "$COVER" -s "$IMG_URL"
ARTLESS=false ARTLESS=false
fi fi
} }
update_cover() { update_cover() {
# Extract the album art from the mp3 file and dont show the messsy # Extract the album art from the mp3 file and dont show the messsy
# output of ffmpeg # output of ffmpeg
ffmpeg -loglevel error -i "$MUSIC_DIR/$(mpc current -f %file%)" $COVER -y ffmpeg -i "$MUSIC_DIR$(mpc current -f %file%)" "$COVER" -y &> /dev/null
# Get the status of the previous command # Get the status of the previous command
STATUS=$? STATUS=$?
# Check if the file has a embbeded album art # Check if the file has a embbeded album art
if [ $STATUS -eq 0 ]; then if [ "$STATUS" -eq 0 ];then
[ ! $SILENT ] && echo "kunst: extracted album art" [ ! "$SILENT" ] && echo "kunst: extracted album art"
ARTLESS=false ARTLESS=false
else else
DIR="$MUSIC_DIR/$(dirname "$(mpc current -f %file%)")" DIR="$MUSIC_DIR$(dirname "$(mpc current -f %file%)")"
[ ! $SILENT ] && echo "kunst: inspecting $DIR" [ ! "$SILENT" ] && echo "kunst: inspecting $DIR"
# Check if there is an album cover/art in the folder. # Check if there is an album cover/art in the folder.
# Look at issue #9 for more details # Look at issue #9 for more details
# use same regex to find album art as mpDris2 for CANDIDATE in "$DIR/cover."{png,jpg}; do
candidates=$( if [ -f "$CANDIDATE" ]; then
find "$DIR" -type f \ STATUS=0
| grep -iE '/(([0-9| |-]*)?)(album|cover|\.?folder|front).*\.(gif|jpeg|jpg|png)$' ARTLESS=false
) convert "$CANDIDATE" $COVER &> /dev/null
while read -r CANDIDATE; do [ ! "$SILENT" ] && echo "kunst: found cover.png"
if [ -f "$CANDIDATE" ]; then fi
STATUS=0 done
ARTLESS=false fi
ffmpeg -loglevel error -i "$CANDIDATE" -vframes 1 $COVER -y
[ ! $SILENT ] && echo "kunst: found cover $CANDIDATE"
break
fi
done <<<"$candidates" # use here string so loop changes vars in main process
fi
if [ $STATUS -ne 0 ]; then if [ "$STATUS" -ne 0 ];then
[ ! $SILENT ] && echo "error: file does not have an album art" [ ! "$SILENT" ] && echo "error: file does not have an album art"
get_cover_online get_cover_online
fi fi
# Resize the image to 250x250 # Resize the image to 250x250
if [ $ARTLESS == false ]; then if [ "$ARTLESS" == false ]; then
ffmpeg -loglevel error -i $COVER -vframes 1 -vf scale=$WIDTH:-1 $COVER -y convert "$COVER" -resize "$SIZE" "$COVER" &> /dev/null
[ ! $SILENT ] && echo "kunst: resized album art to $SIZE" [ ! "$SILENT" ] && echo "kunst: resized album art to $SIZE"
fi fi
} }
pre_exit() { pre_exit() {
# Get the proccess ID of kunst and kill it. # Get the proccess ID of kunst and kill it.
# We are dumping the output of kill to /dev/null # We are dumping the output of kill to /dev/null
# because if the user quits the image viewer # because if the user quits sxiv before they
# before they exit kunst, an error will be shown # exit kunst, an error will be shown
# from kill and we dont want that # from kill and we dont want that
kill -9 $(cat /tmp/kunst.pid) &>/dev/null kill -9 "$(cat /tmp/kunst.pid) &> /dev/null"
} }
main() { main() {
[ -z $KUNST_MUSIC_DIR ] && MUSIC_DIR=$KUNST_MUSIC_DIR [ "$KUNST_MUSIC_DIR" != "" ] && MUSIC_DIR="$KUNST_MUSIC_DIR"
[ -z $KUNST_SIZE ] && SIZE=$KUNST_SIZE [ "$KUNST_SIZE" != "" ] && SIZE="$KUNST_SIZE"
[ -z $KUNST_POSITION ] && POSITION=$KUNST_POSITION [ "$KUNST_POSITION" != "" ] && POSITION="$KUNST_POSITION"
# Flag to run some commands only once in the loop # Flag to run some commands only once in the loop
FIRST_RUN=true FIRST_RUN=true
@ -194,56 +183,44 @@ main() {
update_cover update_cover
if [ $ARTLESS == true ]; then if [ "$ARTLESS" == true ];then
# Dhange the path to COVER because the music note # Dhange the path to COVER because the music note
# image is a png not jpg # image is a png not jpg
COVER=/tmp/kunst.png COVER=/tmp/kunst.png
# Decode the base64 encoded image and save it # Decode the base64 encoded image and save it
# to /tmp/kunst.png # to /tmp/kunst.png
echo "$MUSIC_NOTE" | base64 --decode >$COVER echo "$MUSIC_NOTE" | base64 --decode > "$COVER"
fi fi
if [ ! "$SILENT" ];then
echo "kunst: swapped album art to $(mpc current)"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
fi
if [ ! $SILENT ]; then if [ "$FIRST_RUN" == true ]; then
echo "kunst: swapped album art to $(mpc current)"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
fi
if [ $FIRST_RUN == true ]; then
FIRST_RUN=false FIRST_RUN=false
# Display the album art # Display the album art using sxiv
if [ "$VIEWER" = 'imv' ]; then sxiv -g "$SIZE$POSITION" -b "$COVER" -N "Kunst" &
# if running SwayWM, tell sway to resize the imv window
[ -n "$SWAYSOCK" ] \ # Save the process ID so that we can kill
&& swaymsg for_window '[app_id="^imv$" title="^imv.*\/tmp\/kunst\.(jpg|png)"]' resize set width "$WIDTH" px height "$WIDTH" px # sxiv when the user exits the script
imv "$COVER" &
else
sxiv -g $SIZE$POSITION -b $COVER -N "Kunst" &
fi
# Save the process ID so that we can kill the
# image viewer when the user exits the script
echo $! >/tmp/kunst.pid echo $! >/tmp/kunst.pid
fi fi
# Waiting for an event from mpd; play/pause/next/previous # Waiting for an event from mpd; play/pause/next/previous
# this is lets kunst use less CPU :) # this is lets kunst use less CPU :)
while true; do while true; do
mpc idle player &>/dev/null && (mpc status | grep "\[playing\]" &>/dev/null) && break mpc idle player &>/dev/null && (mpc status | grep "\[playing\]" &>/dev/null) && break
done done
[ ! "$SILENT" ] && echo "kunst: received event from mpd"
[ ! $SILENT ] && echo "kunst: received event from mpd"
if [ "$VIEWER" = 'imv' ]; then
imv-msg "$(cat /tmp/kunst.pid)" close all
imv-msg "$(cat /tmp/kunst.pid)" open "$COVER"
fi
done done
} }
# Disable CTRL-Z because if we allowed this key press, # Disable CTRL-Z because if we allowed this key press,
# then the script would exit but, the image viewer # then the script would exit but, sxiv would still be
# would still be running # running
trap "" SIGTSTP trap "" SIGTSTP
trap pre_exit EXIT trap pre_exit EXIT