<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<font face="Trebuchet MS">One of the side effects of having the many
different devices we can play music on is that we often want to
see cover images to go with the sound. It's easy enough to copy
our music files from Amarok to a tablet or MP3 player. The hard
part has been getting the covers to go along for the ride. In
Amarok, which is my core music repository, the covers are all
uniquely named in the .kde/share/apps/amarok/albumcovers/large
directory. That works great for Amarok, but not so well for one
of my tablets.<br>
<br>
To make things easier for those who want the same cover image
Amarok uses to also be on their tablet, I've put together a quick
little python script that gets the unique image name, and creates
a copy command to put that image in the directory where the albums
tracks are (presumably). It isn't pretty, and it isn't perfect,
but it does go through albums quickly and creates the proper
output (mostly). It's simple enough that anyone can fine-tune it
for their own needs.<br>
<br>
The python script is less than 60 lines long. Given that I am
using the embedded mysql with Amarok, it starts mysql as a
separate task to do the SQL against, then shuts it down when
finished. That was easier than trying to learn embedded mysql.
The script simply writes the output to the terminal. It's easy
enough to redirect to a file for verification and editing.<br>
<br>
In the script, replace "scott" with whatever your login name is.
My music files are in a directory structure with a base of
/shared/music. The final print command corrects for the name
received from Amarok so the copy of the cover image will go into
the correct album location within that base. The imghdr module is
used to obtain the image type, like jpeg or png. The script
corrects the result from jpeg to jpg, although it probably doesn't
have to. Because the result of the select on the directories
table starts with "./", a temporary variable is used to help trim
that off the front of the path.<br>
<br>
Note that this script does nothing with images that are not in the
users amarok/albumcovers directory. It also does not change
anything within the Amarok database - it is a read-only set of
statements to figure out what the image is called.<br>
<br>
You are welcome to use this script however you want. It has
helped me with getting my album covers out of Amarok and onto my
tablets. If someone wanted to burn a CD of their music, the
images might help them also. Run time on this is less than 20
seconds for 1,600 albums. Personally I wish Cover Manager had an
export tool that did this.<br>
<br>
#!/usr/bin/python<br>
# -*- coding: utf-8 -*-<br>
<br>
import MySQLdb as mdb<br>
import sys<br>
import os<br>
import time<br>
import imghdr<br>
<br>
con = None<br>
# start an instance of mysqld to query against.<br>
os.system("/usr/libexec/mysqld
--defaults-file=/home/scott/.kde/share/apps/amarok/my.cnf
--socket=/var/local/mysql_amarok.sock
--datadir=/home/scott/.kde/share/apps/amarok/mysqle
--default-storage-engine=MyISAM --skip-grant-tables --port=33306
--user=scott &")<br>
# wait for mysqld to fully start up.<br>
time.sleep(10)<br>
<br>
try:<br>
# open a connection to the instance of mysqld that was just
started. Note the port and unix_socket requirements.<br>
con = mdb.Connect(host='localhost', port=33306, user='scott',
passwd='', db='amarok',
unix_socket='/var/local/mysql_amarok.sock')<br>
<br>
cur.execute("select id, image, name from albums order by
albums.id")<br>
rows = cur.fetchall()<br>
for row in rows:<br>
if (row[1] > 0):<br>
album_name = row[2];<br>
cur1 = con.cursor()<br>
# for each album we will loop through getting the
image path, track location, url of the tracks, and finally the
directory name of A track for that album<br>
cur1.execute('SELECT images.id, images.path FROM
images, albums WHERE albums.image = images.id and albums.id =
%s',(row[0]))<br>
if (cur1.rowcount > 0):<br>
data = cur1.fetchone()<br>
cached_image_path = data[1];<br>
# only deal with images that are cached, and not
elsewhere<br>
if (cached_image_path.find('/home/scott',0) >
-1):<br>
img_type = imghdr.what(cached_image_path)<br>
if (img_type == 'jpeg'):<br>
img_type = 'jpg'<br>
cur1.execute('select url from tracks where
tracks.album = %s',(row[0]))<br>
if (cur1.rowcount > 0):<br>
url_id = cur1.fetchone()<br>
if (url_id[0] > 0):<br>
cur1.execute('select directory from
urls where id = %s',(url_id[0]))<br>
directory_id = cur1.fetchone()<br>
cur1.execute('select dir from
directories where id = %s',(directory_id[0]))<br>
if (cur1.rowcount > 0):<br>
dir_name = cur1.fetchone()<br>
# this makes it easier to trim the
first two characters from the directory name when building the
print line<br>
t1 = dir_name[0]<br>
print 'cp -i "%s"
"/shared/%s%s.%s"' %
(cached_image_path,t1[2:],album_name,img_type)<br>
cur1.close<br>
cur.close<br>
<br>
except mdb.Error, e:<br>
<br>
print "Error %d: %s" % (e.args[0],e.args[1])<br>
sys.exit(1)<br>
<br>
finally:<br>
<br>
if con: <br>
con.close()<br>
os.system("mysqladmin --socket=/var/local/mysql_amarok.sock
shutdown") <br>
<br>
===============================================================================================================================<br>
This is an example of the output of the script that was used to
copy the images to the music directories. As you can see, the
final file names are the album names as Amarok knows them. The
copy is interactive, just to make sure I'm not wiping out an
existing image I might want.<br>
<br>
cp -i
"/home/scott/.kde/share/apps/amarok/albumcovers/large/ed6cdb11ab1fe164cf8f27dc6496b7e9_"
"/shared/music/Beatles/Abbey Road/Abbey Road.jpg"<br>
cp -i
"/home/scott/.kde/share/apps/amarok/albumcovers/large/e212883f2b4d21cb182b66dcd2406c44"
"/shared/music/Beatles/Beatles for Sale/Beatles For Sale.jpg"<br>
cp -i
"/home/scott/.kde/share/apps/amarok/albumcovers/large/6cd152daca5a3a7614e77f6868338d80"
"/shared/music/Beatles/Hard Days Night/A Hard Day's Night.jpg"<br>
cp -i
"/home/scott/.kde/share/apps/amarok/albumcovers/large/4c0968a4a64b01360f29c769b6e1e140"
"/shared/music/Beatles/Help/Help!.jpg"<br>
cp -i
"/home/scott/.kde/share/apps/amarok/albumcovers/large/25f700f3909b13004413c72f4085f7cf"
"/shared/music/Beatles/Let It Be/Let It Be.jpg"<br>
cp -i
"/home/scott/.kde/share/apps/amarok/albumcovers/large/8940ae83622110785b68ed3f934336d4"
"/shared/music/Beatles/Magical Mystery Tour/Magical Mystery
Tour.jpg"<br>
cp -i
"/home/scott/.kde/share/apps/amarok/albumcovers/large/73fd61a9ae7606c343030447310c014a"
"/shared/music/Beatles/Past Masters/Past Masters.jpg"<br>
cp -i
"/home/scott/.kde/share/apps/amarok/albumcovers/large/18aed807d397bc66404c56931c7c66e0"
"/shared/music/Beatles/Please Please Me/Please Please Me.jpg"<br>
cp -i
"/home/scott/.kde/share/apps/amarok/albumcovers/large/8776808ff700ded76d32cc3b373e263c"
"/shared/music/Beatles/Revolver/Revolver.jpg"<br>
cp -i
"/home/scott/.kde/share/apps/amarok/albumcovers/large/887c8359aa717138f0ef5cb3dd054eef"
"/shared/music/Beatles/Rubber Soul/Rubber Soul.jpg"<br>
cp -i
"/home/scott/.kde/share/apps/amarok/albumcovers/large/9d6bdee11a958449e06d3bac53c5bd76_"
"/shared/music/Beatles/Sgt Peppers Lonely Hearts Club Band/Sgt.
Pepper's Lonely Hearts Club Band.jpg"<br>
<br>
<br>
<br>
</font>
<div class="moz-signature">-- <br>
Scott Yanke</div>
</body>
</html>