This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Script looping over every track

Tags: None
(comma "," separated)
niskel
Registered Member
Posts
3
Karma
0

Script looping over every track

Sun Oct 29, 2006 4:57 pm
I am trying to make a script that looks at every track in the collection (or a playlist) and then copies and resizes that track's album cover into the folder the track is contained in. I have made a script to do this but it is far from ideal. Right now it plays every track in the playlist to get its information. This takes forever, there must be a better way. Can anybody give me some advice? Here is the script I have now:
Code: Select all
#!/bin/sh

COVER_DIMENSIONS="85x85"
COVER_FILENAME="cover.bmp"
NUM_TRACKS=$((`dcop amarok playlist getTotalTrackCount` - 1))

for i in `seq 0 $NUM_TRACKS`; do
   dcop amarok playlist playByIndex $i;
   COVER=`dcop amarok player coverImage`;
   FILENAME=`dcop amarok player path`;
   ALBUMDIR=`dirname "$FILENAME"`;
   if [ "$1" == "force" ] || ! [ -e "$ALBUMDIR/$COVER_FILENAME" ] || ! [ `identify -format "%wx%h" "$ALBUMDIR/$COVER_FILENAME"` == $COVER_DIMENSIONS ] ; then
      convert "$COVER" -resize $COVER_DIMENSIONS "$ALBUMDIR/$COVER_FILENAME";
   fi
done
User avatar
marcel
Registered Member
Posts
595
Karma
0
OS

Re: Script looping over every track

Sun Oct 29, 2006 6:09 pm
Without knowing details, I would just use database lookups to get the information...
niskel
Registered Member
Posts
3
Karma
0

Re: Script looping over every track

Sun Oct 29, 2006 11:41 pm
So I figured it out. Instead of spending the 5 minutes I was hoping on this script, I spent the better part of the day; the result was satisfying though. So for anyone who cares, here it is without having to play every single song using the database instead.
Code: Select all
#!/bin/sh

COVER_DIMENSIONS="85x85"
COVER_FILENAME="cover.bmp"
COVER_PATH="$HOME/.kde3.5/share/apps/amarok/albumcovers/large"

for ARTIST_NUM in `dcop amarok collection query "SELECT DISTINCT artist FROM tags"`; do
   ARTIST=`dcop amarok collection query "SELECT name FROM artist WHERE id=$ARTIST_NUM" | tr A-Z a-z`;
   for ALBUM_NUM in `dcop amarok collection query "SELECT DISTINCT album FROM tags WHERE artist=$ARTIST_NUM"`; do
      ALBUM=`dcop amarok collection query "SELECT name FROM album WHERE id=$ALBUM_NUM" | tr A-Z a-z`;
      COVER_IMAGE=$COVER_PATH/`echo -n "${ARTIST}${ALBUM}" | md5sum | sed -e 's/^\([^ ]*\)  -$/\1/'`;
      ORIGIFS=$IFS;
      IFS=`echo -en "\n\b"`;
      for ALBUM_PATH in `dcop amarok collection query "SELECT DISTINCT dir FROM tags WHERE artist=$ARTIST_NUM AND album=$ALBUM_NUM" | sed -e 's/^\.\(.*\)\$/\1/'`; do
         if ! [ -e "$COVER_IMAGE" ]; then
            COVER_IMAGE=$COVER_PATH/`echo -n "various artists${ALBUM}" | md5sum | sed -e 's/^\([^ ]*\)  -$/\1/'`;
         fi;
         if [ -e "$COVER_IMAGE" ] && ([ "$1" = "force" ] || ! [ -e "$ALBUM_PATH/$COVER_FILENAME" ] || ! [ `identify -format "%wx%h" "$ALBUM_PATH/$COVER_FILENAME"` == "$COVER_DIMENSIONS" ]); then
            echo "$ARTIST - $ALBUM";
            convert "$COVER_IMAGE" -resize $COVER_DIMENSIONS! "$ALBUM_PATH/$COVER_FILENAME";
         fi;
      done;
      IFS=$ORIGIFS;
   done;
done;

It is very useful if you have a device that uses Rockbox and the album art patch that you sync with your music collection.
Jean Of mArc
Registered Member
Posts
4
Karma
0

Re: Script looping over every track

Wed Nov 08, 2006 7:17 pm
Hey, good work on this script, niskel. Very useful and helpful for reference on doing similar processes myself. Thanks!


Bookmarks



Who is online

Registered users: abc72656, Bing [Bot], daret, Google [Bot], Sogou [Bot], Yahoo [Bot]