Skip to content

Instantly share code, notes, and snippets.

@mamins1376
Last active December 6, 2017 14:36
Show Gist options
  • Save mamins1376/c35b2a4e7b383e7dcb9861024f60c10a to your computer and use it in GitHub Desktop.
Save mamins1376/c35b2a4e7b383e7dcb9861024f60c10a to your computer and use it in GitHub Desktop.
Rewallpaper makes a wallpaper from mpd's currently playing music and set's that as desktop's background!
#!/bin/bash
# configurations
RW_MUSIC=~/music
RW_CACHE=~/.cache/rewallpaper
RW_DEFAULT=~/.wallpaper.jpg
RW_WIDTH=1366
RW_HEIGHT=768
RW_MARGIN=60
# default values
RW_SONG=''
RW_WALL=''
RW_DB="$RW_CACHE/cache.db"
RW_TMP1="$( mktemp --suffix=.jpg )"
RW_TMP2="$( mktemp --suffix=.jpg )"
# functions
function rw_init {
# initialize environment
rw_reset
mkdir -p "$RW_CACHE"
if [ ! -f "$RW_DB" ]; then
echo 'CREATE TABLE CACHED_FILES(FILE TEXT NOT NULL,
MD5 CHAR(32) NOT NULL);' | sqlite3 "$RW_DB"
fi
}
function rw_generate {
# generate a wallpaper for $RW_SONG and set that as wallpaper
# look for cached wallpaper
ART_MD5=$( echo "SELECT MD5 FROM CACHED_FILES WHERE FILE = '$RW_SONG'" | sqlite3 "$RW_DB" )
if [ "$ART_MD5" ]; then
rw_set $ART_MD5
return
fi
# extract cover art
[ -f "$RW_TMP1" ] && rm "$RW_TMP1"
ALB_COVER="$(dirname "$RW_SONG")/cover.jpg"
if [ -f "$ALB_COVER" ]; then
cp "$ALB_COVER" "$RW_TMP1"
elif ! ffmpeg -i "$RW_SONG" "$RW_TMP1" > /dev/null 2>&1; then
rw_reset
return
fi
ART_MD5=$( md5sum "$RW_TMP1" | head -c 32 )
echo "INSERT INTO CACHED_FILES VALUES ('$RW_SONG', '$ART_MD5');" | sqlite3 "$RW_DB"
# generate wallpaper if not cached
CACHED_WALL="$RW_CACHE/$ART_MD5.jpg"
if [ ! -f "$CACHED_WALL" ]; then
# create blured background
convert "$RW_TMP1" -resize ${RW_WIDTH}x$RW_WIDTH \
-gravity center -extent ${RW_WIDTH}x$RW_HEIGHT \
-blur 0x25 "$RW_TMP2"
# calculate cover size
NEWSIZE=$(( $RW_HEIGHT - 2 * $RW_MARGIN ))
# merge
composite \( "$RW_TMP1" -resize ${NEWSIZE}x$NEWSIZE \) \
-gravity northeast -geometry +$RW_MARGIN+$RW_MARGIN \
"$RW_TMP2" "$CACHED_WALL"
fi
# set wallpaper
rw_set $ART_MD5
}
function rw_set {
# Set wallpaper to $1.jpg. $1 is MD5 hash of song's cover art
# (not it's wallpaper).
rw_change "$RW_CACHE/$1.jpg"
}
function rw_reset {
# Restore default wallpaper
rw_change "$RW_DEFAULT"
}
function rw_change {
# Set $1 as current wallpaper. Change if you are using a different method
# (e.g. feh instead of pcmanfm
# Make sure pcmanfm daemon is already started
while ! pgrep pcmanfm > /dev/null; do sleep 1; done
pcmanfm -w $@
}
function rw_changed {
# if there is a song in playing or paused state, generate a wallpaper for
# it. set the default wallpaper otherwise.
STATUS="$( mpc status -f %file% )"
if echo "$STATUS" | grep -q '\[p\(laying\|aused\)\]'; then
RW_SONG="$RW_MUSIC/$( echo -e "$STATUS" | head -n 1 )"
rw_generate
return
fi
rw_reset
}
function rw_main {
# main precedure. acts like an event loop listenning for mpd events.
rw_init
mpc idleloop | while read JUNK; do rw_changed; done
}
rw_main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment