Skip to content

Instantly share code, notes, and snippets.

@tremby
Created January 17, 2012 01:26
Show Gist options
  • Save tremby/1623989 to your computer and use it in GitHub Desktop.
Save tremby/1623989 to your computer and use it in GitHub Desktop.
view album art
#!/bin/bash
# aa -- view album art
# Expects either no argument (and will then query for the currently playing
# song according to MPD) or one argument which is the path to a music file.
#
# Expects to find a file titled something like cover.jpg (see prefixes,
# middles, suffixes below) in the same directory as the music file. If found an
# image viewer is launched. If no DISPLAY is available, use img2txt to get
# ASCII art if available.
#
# If getting the currently playing track from MPD, the MUSIC_PATH variable
# needs to be set below.
MUSIC_PATH="$HOME/media/music"
PREFIXES=("" .)
MIDDLES=(cover coverart frontcover front albumart albumcover album folder)
SUFFIXES=(.png .jpg)
if [ $# -lt 1 ]; then
mpc=$(mpc --format "%file%" | head -1)
if [[ $mpc == "volume:"* ]]; then
exit 3
fi
song="$MUSIC_PATH/$mpc"
else
song="$1"
fi
dir=$(dirname "$song")
found=""
for pre in "${PREFIXES[@]}"; do
for mid in "${MIDDLES[@]}"; do
for suf in "${SUFFIXES[@]}"; do
filename="$pre$mid$suf"
if [ $(find "$dir" -maxdepth 1 -iname "$filename" | wc -l) -ge 1 ]; then
found="$filename"
break 3
fi
done
done
done
if [ "x" = "x$found" ]; then
echo "album art not found" >&2
exit 1
fi
fullpath="$dir/$found"
# if there's a DISPLAY, try various gui image viewers
if [ $DISPLAY ]; then
type gliv >/dev/null && { gliv "$fullpath" & exit; }
type eog >/dev/null && { eog "$fullpath" & exit; }
type feh >/dev/null && { feh "$fullpath" & exit; }
type display >/dev/null && { display "$fullpath" & exit; }
fi
function size {
# try environment variables
x=$COLUMNS
y=$ROWS
if [ "z" = "z$x" -o "z" = "z$y" ]; then
# fall back to stty size
x=$(stty size | cut -d " " -f 2)
y=$(stty size | cut -d " " -f 1)
if [ "z" = "z$x" -o "z" = "z$y" ]; then
# fall back to tput cols and lines
x=$(tput cols)
y=$(tput lines)
if [ "z" = "z$x" -o "z" = "z$y" ]; then
# give up and use defaults
x=78
y=39
fi
fi
fi
# take 1 from height for prompt
let y=y-1
if [ $(expr $y "*" 2) -gt $x ]; then
# not enough columns to use full height
y=$(expr $x / 2)
else
# not enough lines to use full width
x=$(expr $y "*" 2)
fi
echo -e "$x\t$y"
}
size=$(size)
x=$(echo "$size" | cut -f 1)
y=$(echo "$size" | cut -f 2)
# if none of them existed or there was no DISPLAY, try text-mode image viewers
type img2txt >/dev/null && { img2txt -W $x -H $y "$fullpath"; exit; }
# fail
echo "found no suitable viewer" >&2
exit 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment