]> git.draconx.ca Git - mpdhacks.git/blob - mpdthumb.sh
mpdmenu: Use MBIDs for track matching instead of name matching.
[mpdhacks.git] / mpdthumb.sh
1 #!/bin/sh
2 #
3 # Copyright © 2019 Nick Bowler
4 #
5 # Generate thumbnails for cover art retrieved from MPD.
6 #
7 # License GPLv3+: GNU General Public License version 3 or any later version.
8 # This is free software: you are free to change and redistribute it.
9 # There is NO WARRANTY, to the extent permitted by law.
10
11 : "${XDG_CACHE_HOME=$HOME/.cache}"
12 : "${THUMBNAILDIR=$XDG_CACHE_HOME/mpdthumb}"
13
14 # Try to find mpdexec...
15 case $0 in
16 /*) self=$0 ;;
17 */*) self=$PWD/${0#./} ;;
18 *) self=`command -v $0` ;;
19 esac
20 owndir=${self%/*}
21
22 if command -v "$owndir/mpdexec.pl" >/dev/null; then
23   : "${MPDEXEC=$owndir/mpdexec.pl}"
24 elif command -v mpdexec.pl >/dev/null; then
25   : "${MPDEXEC=mpdexec.pl}"
26 else
27   : "${MPDEXEC=mpdexec}"
28 fi
29
30 if command -v gm >/dev/null; then
31   : "${CONVERT=gm convert}"
32 else
33   : "${CONVERT=convert}"
34 fi
35
36 size=x128
37
38 lastarg=
39 dashdash=
40 for arg; do
41   if test ${lastarg:+y}; then
42     arg=$lastarg=$arg
43     lastarg=
44   fi
45
46   case $dashdash$arg in
47   --size=*) size=${arg#--size=} ;;
48   --small) size=56 ;;
49   --size) lastarg=$arg ;;
50   --) dashdash=: ;;
51   -*) printf '%s: unrecognized argument: %s\n' "$0" "$arg" 1>&2; exit 1 ;;
52   *) set x "$@" "$arg"; shift
53   esac
54
55   shift
56 done
57
58 w=${size%x*} h=${size#*x}
59 if expr "$w$h" : '[0-9][0-9]*$' >/dev/null; then :; else
60   printf '%s: invalid --size setting: %s\n' "$0" "$size" 1>&2
61   exit 1
62 fi
63
64 case $# in 0)
65   printf 'usage: %s [options] file [file ...]\n' "$0" 1>&2
66   exit 1
67 esac
68
69 tmp=`mktemp`
70 exec 3>"$tmp" 4<"$tmp"
71 rm -f "$tmp"
72
73 tmp=`mktemp`
74 exec 5>"$tmp" 6<"$tmp"
75 rm -f "$tmp"
76
77 for arg; do
78   arg=${arg%/*}/
79   shift; set x "$@" "$arg"; shift
80
81   printf '%s\n' "$arg" | sed '/[        \\"]/ {
82     s/[\\"]/\\&/g
83     s/.*/"&"/
84   }
85   s/.*/albumart & 2147483647/' >&3;
86 done
87
88 <&4 $MPDEXEC --ignore-errors >&5 2>&1 || exit
89 while read a b <&6; do
90   case $a in
91   size:) :;;
92   ACK) echo; shift || exit; continue ;;
93   *) continue ;;
94   esac
95
96   # We combine the filename and the size to compute the cache key and
97   # hope this suffices to detect stale entries.  Unfortunately MPD does
98   # not currently give us the modified date which would be more useful...
99   file=$1; shift || exit
100   cache_id=`printf 'MPD:%s:%s' "$file" "$b" | md5sum`
101   cache_id=${cache_id:+${cache_id%% *}_$size.png}
102
103   if test ! -f "$THUMBNAILDIR/$cache_id"; then
104     if test ! -f "$THUMBNAILDIR/CACHEDIR.TAG"; then
105       mkdir -p "$THUMBNAILDIR"
106       { cat >"$THUMBNAILDIR/CACHEDIR.TAG~" || exit; } <<'EOF'
107 Signature: 8a477f597d28d172789f06886806bc55
108 EOF
109       mv -f "$THUMBNAILDIR/CACHEDIR.TAG~" "$THUMBNAILDIR/CACHEDIR.TAG"
110     fi
111
112     # Not cached, retrieve the entire image
113     $MPDEXEC --binary --download albumart "$file" >&3 || exit
114     <&4 $CONVERT -scale "$size" - "$THUMBNAILDIR/tmp.$cache_id" ||
115         { rc=$? rm -f "$THUMBNAILDIR/tmp.$cache_id"; exit $rc; }
116     mv -f "$THUMBNAILDIR/tmp.$cache_id" "$THUMBNAILDIR/$cache_id"
117   fi
118
119   printf '%s\n' "$THUMBNAILDIR/$cache_id"
120 done