Album art (Part 1 of3)
authorAndrew Strong <andrew.david.strong@gmail.com>
Wed, 22 Apr 2015 08:24:45 +0000 (18:24 +1000)
committerAndrew Strong <andrew.david.strong@gmail.com>
Wed, 22 Apr 2015 08:24:45 +0000 (18:24 +1000)
Automated downloading of album art in up to three steps:

 1. If CDDMETHOD is musicbrainz cover is downloaded
    from coverartarchive by MBID
 2. If method 1 fails download is tried from amazon
    by ASIN
 3. If 1 and 2 fail or CDDBMETHOD is cddb, album will
    be downloaded using glyrc by artist + album title

Thanks to Johannes Gernemann for the patch and thanks
to Richard for further development of the patch.
This partially addresses Issue 33 which will be fully
addressed in Parts 2 & 3 of this series of commits.

abcde
abcde-musicbrainz-tool
abcde.conf

diff --git a/abcde b/abcde
index d941712..4830fad 100755 (executable)
--- a/abcde
+++ b/abcde
@@ -2975,6 +2975,81 @@ do_cddbedit ()
        echo "cddb-edit" >> "$ABCDETEMPDIR/status"
 }
 
+# do_getalbumart
+# try to download CD cover
+do_getalbumart()
+{
+       # set variables
+       ALBUMFILE="$(mungefilename "$DALBUM")"
+       ARTISTFILE="$(mungefilename "$DARTIST")"
+       # cover file name
+       if [ x"$ALBUMARTFILE" = "x" ]; then
+               ALBUMARTFILE="cover.jpg"
+       fi
+       # have we got a musicbrainz mbid or amazon asin?
+       case "$CDDBMETHOD" in
+               musicbrainz)
+                       # try musicbrainz mbid
+                       if [ -s "$ABCDETEMPDIR/mbid.$(checkstatus cddb-choice)" ]; then
+                               MBID=$(cat "$ABCDETEMPDIR/mbid.$(checkstatus cddb-choice)")
+                               vecho "trying to get cover from coverartarchive.orq with musicbrainz mbid $MBID" >&2
+                               ALBUMARTURL="http://coverartarchive.org/release/$MBID/front"
+                               vecho "cover URL: $ALBUMARTURL" >&2
+                               $HTTPGET "$ALBUMARTURL" > "$ABCDETEMPDIR/$ALBUMARTFILE"
+                               if [ $? -ne 0 ]; then
+                                       vecho "could not download cover from musicbrainz" >&2
+                                       # try amazon asin
+                                       if [ -s "$ABCDETEMPDIR/asin.$(checkstatus cddb-choice)" ]; then
+                                               ASIN=$(cat "$ABCDETEMPDIR/asin.$(checkstatus cddb-choice)")
+                                               vecho "trying to get cover from amazon.com with asin $ASIN" >&2
+                                               ALBUMARTURL="http://ec1.images-amazon.com/images/P/$ASIN.01.LZZZZZZZZ.jpg"
+                                               vecho "cover URL: $ALBUMARTURL" >&2
+                                               $HTTPGET "$ALBUMARTURL" > "$ABCDETEMPDIR/$ALBUMARTFILE"
+                                               if [ $? -ne 0 ]; then
+                                                       vecho "could not download cover from amazon" >&2
+                                               fi
+                                       else
+                                               vecho "no amazon ID found" >&2
+                                       fi
+                               fi
+                       else
+                               vecho "no musicbrainz ID found" >&2
+                       fi
+                       ;;
+       esac
+       # use glyrc
+       if [ ! -s "$ABCDETEMPDIR/$ALBUMARTFILE" ]; then
+               vecho "trying to get cover with glyrc for $ARTISTFILE / $ALBUMFILE" >&2
+               $GLYRC cover --artist "$ARTISTFILE" --album "$ALBUMFILE" --write "$ABCDETEMPDIR/$ALBUMARTFILE" $GLYRCOPTS
+               if [ $? -ne 0 ]; then
+                       vecho "could not download cover with glyrc" >&2
+               else
+                       ALBUMARTURL="glyrc"
+               fi
+       fi
+       # copy to target directories
+       if [ -s "$ABCDETEMPDIR/$ALBUMARTFILE" ]; then
+               for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
+               do
+                       # put cover in the same place as the album
+                       if [ "$VARIOUSARTISTS" = "y" ] ; then
+                               ALBUMARTDIR="$(eval echo "$VAOUTPUTFORMAT")"
+                       else
+                               ALBUMARTDIR="$(eval echo "$OUTPUTFORMAT")"
+                       fi
+                       FINALALBUMARTDIR="$(dirname "$OUTPUTDIR/$ALBUMARTDIR")"
+                       vecho "copying cover to target directory $FINALALBUMARTDIR" >&2
+                       mkdir -p "$FINALALBUMARTDIR"
+                       cp "$ABCDETEMPDIR/$ALBUMARTFILE" "$FINALALBUMARTDIR"
+               done
+               rm -f "$ABCDETEMPDIR/$ALBUMARTFILE"
+               echo "get-album-art=$ALBUMARTURL" >> "$ABCDETEMPDIR/status"
+       else
+               log warning "could not get cover"
+               echo "get-album-art=none" >> "$ABCDETEMPDIR/status"
+       fi
+}
+
 # do_cdread [tracknumber]
 # do_cdread onetrack [firsttrack] [lasttrack]
 #
@@ -3328,6 +3403,7 @@ MKCUE=mkcue
 MKTOC=cdrdao
 DIFF=diff
 CUE2DISCID=builtin
+GLYRC=glyrc
 
 # Options for programs called from abcde
 # mp3
@@ -3386,6 +3462,7 @@ MKTOCOPTS=""
 VORBISCOMMENTOPTS="-R"
 METAFLACOPTS="--no-utf8-convert"
 DIFFOPTS=
+GLYRCOPTS="--formats jpg;jpeg"
 
 # Default to one process if -j isn't specified
 MAXPROCS=1
@@ -3689,6 +3766,7 @@ do
                replaygain) DOCDDB=y; DOREAD=y; DOENCODE=y; DOTAG=y; DOMOVE=y; DOREPLAYGAIN=y;;
                playlist) DOCDDB=y; DOPLAYLIST=y;;
                clean) DOCLEAN=y;;
+               getalbumart) GETALBUMART=y;;
        esac
 done
 
@@ -4163,6 +4241,11 @@ if [ X"$CDSPEEDVALUE" != "X" ] && [ "$DOREAD" = "y" ]; then
        esac
 fi
 
+if [ "$GETALBUMART" = "y" ]; then
+       NEEDHTTPGET="y"
+       NEEDGLYRC="y"
+fi
+
 ###USEPIPESSUPPORT###
 
 # Rippers with USEPIPE support
@@ -4243,7 +4326,8 @@ for X in $CDROMREADER $CDDISCID ${NEEDTAGGER+$TAGGER} $MP3ENCODER \
        ${NEEDVORBISGAIN+$VORBISGAIN} ${NEEDMP3GAIN+$MP3GAIN} \
        ${NEEDMPCGAIN+$MPCGAIN} ${NEEDCUEREADER+$CUEREADER} \
        ${NEEDWVGAIN+WVGAIN} ${NEEDAPETAG+$APETAG} \
-       ${NEEDCUE2DISCID+$CUE2DISCID} ${NEEDNEROAACTAG+$NEROAACTAG}
+       ${NEEDCUE2DISCID+$CUE2DISCID} ${NEEDNEROAACTAG+$NEROAACTAG} \
+       ${NEEDGLYRC+$GLYRC}
 do
        checkexec "$X"
 done
@@ -4383,6 +4467,10 @@ if [ ! "$ONETRACK" = "y" ]; then
        fi
 fi
 
+if [ "$GETALBUMART" = "y" ]; then
+       do_getalbumart
+fi
+
 # For the lowdisk option, only one program is running at once so the encoder
 # can be unsilenced right away.
 if [ "$LOWDISK" = "y" ] || [ "$ONETRACK" = "y" ]; then
index aadb56a..7c58951 100755 (executable)
@@ -140,6 +140,16 @@ if ($command =~ m/^id/) {
         print OUT ".\n";
         close OUT;
 
+        # save release mbid
+        open (OUT, "> $workdir/mbid.$releasenum");
+        print OUT $release->id;
+        close OUT;
+
+        # save release asin
+        open (OUT, "> $workdir/asin.$releasenum");
+        print OUT $release->asin;
+        close OUT;
+
         # Check to see that this entry is unique; generate a checksum
         # and compare to any previous checksums
         my $checksum = calc_sha1("$workdir/cddbread.$releasenum");
index 6e007ac..d747e15 100644 (file)
@@ -6,6 +6,7 @@
 
 # CDDB options
 # Choose whether you want to use CDDB or Musicbrainz. Default is CDDB
+# currently recognized options are 'cddb' and 'musicbrainz'
 #CDDBMETHOD=cddb
 
 # If you wish to use a different CDDB server, edit this line.
 #DIFF=diff
 #WVGAIN=wvgain
 #APETAG=apetag
+#GLYRC=glyrc
 
 # Options to call programs with:
 
 #DIFFOPTS=""
 #VORBISCOMMENTOPTS="-R"
 #METAFLACOPTS="--no-utf8-convert"
-#DIFFOPTS=""
 
 # Actions to take
 # Comma-separated list of one or more of the following:
-#  cddb,cue,read,normalize,encode,tag,move,replaygain,playlist,clean,default
+#  cddb,cue,read,normalize,encode,tag,move,replaygain,playlist,getalbumart,clean,default
 #   encode implies read
 #   normalize implies read
 #   tag implies cddb,read,encode
 #(some hardware players insist on CR-LF line-endings)
 #DOSPLAYLIST=n
 
+# album art download options (see glyrc's help for details)
+# '--from <provider>' and '--lang <langcode>' might also be useful
+#GLYRCOPTS="--formats jpg;jpeg"
+#ALBUMARTFILE="cover.jpg"
+
 # Custom filename munging:
 # By default, abcde will do the following to CDDB data to get a useful
 # filename: