Wednesday, September 8, 2010

Getting the track listing for a K3B project

This was harder than I expected. However...

Get your .k3b file, which is really a zip file:

$ unzip -l can-u-pick-em.k3b
Archive: can-u-pick-em.k3b
Length Date Time Name
--------- ---------- ----- ----
17 2106-02-07 17:28 mimetype
10623 2106-02-07 17:28 maindata.xml
--------- -------
10640 2 files

and extract the only useful part:

unzip -p can-u-pick-em.k3b maindata.xml > tmp.xml

That's an XML file that contains the project data. Then we feed it through an XSLT processor, using this stylesheet:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">

<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/k3b_audio_project">
<xsl:apply-templates select="contents">
</xsl:apply-templates>
</xsl:template>


<xsl:template match="contents">
<xsl:apply-templates select="track">
</xsl:apply-templates>
</xsl:template>

<xsl:template match="track">
<xsl:value-of select="cd-text/artist"/>
<xsl:text> - </xsl:text>
<xsl:value-of select="cd-text/title"/>
<xsl:text>
</xsl:text>
</xsl:template>

</xsl:stylesheet>


like this:

$ xsltproc stylesheet.xslt tmp.xml
Eleni Mandell - Pauline
Neko Case - Star Witness

etc

EDIT!
Another option which I tried initially was using dcop to interrogate K3B while it has the project open - there are tons of suggestions out there to do this, but I could never get it to work. It turns out that dcop doesn't work in KDE4, and has been replaced with DBUS. Ah, so here's some examples using DBUS to talk to amarok - so maybe this will work... though this suggests otherwise

3 comments:

  1. Thank you! This worked on the first try!

    ReplyDelete
  2. OMG, thank you! I had a lot of projects I had to resurrect from a crash, and this finally answered how to decode one of these files. Some of the data is still corrupted (boo!) but at least I have enough data now to rebuild it from scratch.

    ReplyDelete
    Replies
    1. Ha, I'd forgotten about this post (hell, I'd forgotten I once knew how to do XSLT!) but nice to see it's able to help someone out :-)

      Delete