]> git.draconx.ca Git - mpdhacks.git/commitdiff
mpdthumb: Add support for embedded cover art.
authorNick Bowler <nbowler@draconx.ca>
Sun, 25 Jul 2021 17:08:11 +0000 (13:08 -0400)
committerNick Bowler <nbowler@draconx.ca>
Sun, 25 Jul 2021 17:08:11 +0000 (13:08 -0400)
MPD has recently gained the "readpicture" command in order to retrieve
cover images embedded in files.  This works very similarly to albumart.

We now have the necessary features in mpdexec to implement this in
mpdthumb.  When the --embedded option is supplied, the script will
first try readpicture, then albumart for each file specified.

mpdthumb.sh

index 4ff4249042917920cdc9940b55086338e2500e7e..4faf7a61f7351e2ef9731e2d57aff9851789996a 100755 (executable)
@@ -79,6 +79,9 @@ Options:
                     'auto', which queries the server to determine the
                     appropriate method.
   --no-binarylimit  Equivalent to --binarylimit=no
                     'auto', which queries the server to determine the
                     appropriate method.
   --no-binarylimit  Equivalent to --binarylimit=no
+  --embedded        Attempt to retrieve embedded covers using readpicture.
+                    This requires (and implies) --binarylimit.
+  --no-embedded     Use only album-level covers.  This is the default.
   --version         Print a version message and then exit.
   --help            Print this message and then exit.
 
   --version         Print a version message and then exit.
   --help            Print this message and then exit.
 
@@ -87,6 +90,7 @@ EOF
 }
 
 opt_binarylimit=auto
 }
 
 opt_binarylimit=auto
+opt_embedded=no
 
 size=x128
 lastarg=
 
 size=x128
 lastarg=
@@ -101,6 +105,8 @@ for arg; do
   --binarylimit=*) opt_binarylimit=${arg#--*=} ;;
   --no-binarylimit) opt_binarylimit=no ;;
   --binarylimit) opt_binarylimit=yes ;;
   --binarylimit=*) opt_binarylimit=${arg#--*=} ;;
   --no-binarylimit) opt_binarylimit=no ;;
   --binarylimit) opt_binarylimit=yes ;;
+  --embedded) opt_embedded=yes ;;
+  --no-embedded) opt_embedded=no ;;
   --size=*) size=${arg#--size=} ;;
   --small) size=56 ;;
   --size) lastarg=$arg ;;
   --size=*) size=${arg#--size=} ;;
   --small) size=56 ;;
   --size) lastarg=$arg ;;
@@ -133,6 +139,13 @@ tmp=`mktemp`
 exec 5>"$tmp" 6<"$tmp"
 rm -f "$tmp"
 
 exec 5>"$tmp" 6<"$tmp"
 rm -f "$tmp"
 
+# --embedded implies --binarylimit
+case $opt_embedded in
+yes) use_embedded=true ;;
+*) use_embedded=false ;;
+esac
+$use_embedded && opt_binarylimit=yes
+
 # Test for binarylimit command in MPD server.  We want to minimize data
 # transfer in order to make cache hits as fast as possible.
 #
 # Test for binarylimit command in MPD server.  We want to minimize data
 # transfer in order to make cache hits as fast as possible.
 #
@@ -158,34 +171,55 @@ else
   binarylimit_offset=2147483647
 fi
 
   binarylimit_offset=2147483647
 fi
 
-for arg; do
-  arg=${arg%/*}/
-  shift; set x "$@" "$arg"; shift
+for file; do
+  case $file in
+  */*) dir=${file%/*}/ ;;
+  *) dir=/ ;;
+  esac
 
 
-  printf '%s\n' "$arg" | sed '/[       \\"'\'']/ {
+  if $use_embedded; then
+    shift; set x "$@" "$file" "$dir"; shift
+  else
+    file=
+  fi
+
+  printf '%s\n' "$dir" ${file:+"$file"} | sed -n '/[   \\"'\'']/ {
     s/[\\"]/\\&/g
     s/.*/"&"/
   }
     s/[\\"]/\\&/g
     s/.*/"&"/
   }
-  s/.*/albumart & '"$binarylimit_offset"'/' >&3;
+  s/.*/albumart & '"$binarylimit_offset"'/
+  1h
+  2s/^albumart/readpicture/p
+  $ {
+    g
+    p
+  }' >&3;
 done
 
 done
 
-<&4 $MPDEXEC --ignore-errors >&5 2>&1 || exit
+valid=
+<&4 $MPDEXEC --verbose --ignore-errors >&5 2>&1 || exit
 while read a b <&6; do
   case $a in
 while read a b <&6; do
   case $a in
-  size:) :;;
+  readpicture|albumart)
+    prevn=$# file=$1 mode=$a; shift || exit; continue
+    ;;
+  size:)
+    valid=$mode$#
+    ;;
   ACK)
     case $b in
   ACK)
     case $b in
-    *binarylimit*) printf '%s: %s\n' "$argv0" "$a $b" 1>&2; exit 1 ;;
+    *'{binarylimit}'*|*'{readpicture}'*)
+      printf '%s: %s\n' "$argv0" "$a $b" 1>&2; exit 1 ;;
     esac
     esac
-
-    echo; shift || exit; continue ;;
+    test x"$valid" = x"readpicture$prevn" || echo
+    continue
+    ;;
   *) continue ;;
   esac
 
   # We combine the filename and the size to compute the cache key and
   # hope this suffices to detect stale entries.  Unfortunately MPD does
   # not currently give us the modified date which would be more useful...
   *) continue ;;
   esac
 
   # We combine the filename and the size to compute the cache key and
   # hope this suffices to detect stale entries.  Unfortunately MPD does
   # not currently give us the modified date which would be more useful...
-  file=$1; shift || exit
   cache_id=`printf 'MPD:%s:%s' "$file" "$b" | md5sum`
   cache_id=${cache_id:+${cache_id%% *}_$size.png}
 
   cache_id=`printf 'MPD:%s:%s' "$file" "$b" | md5sum`
   cache_id=${cache_id:+${cache_id%% *}_$size.png}
 
@@ -199,7 +233,7 @@ EOF
     fi
 
     # Not cached, retrieve the entire image
     fi
 
     # Not cached, retrieve the entire image
-    $MPDEXEC --binary --download albumart "$file" >&3 || exit
+    $MPDEXEC --binary --download "$mode" "$file" >&3 || exit
     <&4 $CONVERT -scale "$size" - "$THUMBNAILDIR/tmp.$cache_id" ||
         { rc=$? rm -f "$THUMBNAILDIR/tmp.$cache_id"; exit $rc; }
     mv -f "$THUMBNAILDIR/tmp.$cache_id" "$THUMBNAILDIR/$cache_id"
     <&4 $CONVERT -scale "$size" - "$THUMBNAILDIR/tmp.$cache_id" ||
         { rc=$? rm -f "$THUMBNAILDIR/tmp.$cache_id"; exit $rc; }
     mv -f "$THUMBNAILDIR/tmp.$cache_id" "$THUMBNAILDIR/$cache_id"