<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Mouse Vs. The Python &#187; mp3</title>
	<atom:link href="http://www.blog.pythonlibrary.org/tag/mp3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.blog.pythonlibrary.org</link>
	<description>Python Programming from the Frontlines</description>
	<lastBuildDate>Sun, 08 Jan 2012 12:45:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Parsing ID3 Tags from MP3s using Python</title>
		<link>http://www.blog.pythonlibrary.org/2010/04/22/parsing-id3-tags-from-mp3s-using-python/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/04/22/parsing-id3-tags-from-mp3s-using-python/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 01:36:05 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[mp3]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=821</guid>
		<description><![CDATA[While working on my Python mp3 player I realized I needed to research what Python had to offer for parsing ID3 tags. There are tons of projects out there, but most of them appear to be either dead, don&#8217;t have documentation or both. In this post, you will discover the wild world of MP3 tag [...]]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-counturl="http://www.blog.pythonlibrary.org/2010/04/22/parsing-id3-tags-from-mp3s-using-python/" data-url="http://bit.ly/vUhRGw" data-text="Parsing ID3 Tags from MP3s using Python" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-left"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.blog.pythonlibrary.org/2010/04/22/parsing-id3-tags-from-mp3s-using-python/&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px !important; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-left"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.blog.pythonlibrary.org/2010/04/22/parsing-id3-tags-from-mp3s-using-python/";
			reddit_title = "Parsing ID3 Tags from MP3s using Python";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-left"><g:plusone size="small" href="http://www.blog.pythonlibrary.org/2010/04/22/parsing-id3-tags-from-mp3s-using-python/"></g:plusone></div></div><p>While working on my Python <a href="http://www.blog.pythonlibrary.org/2010/04/20/wxpython-creating-a-simple-mp3-player/">mp3 player</a> I realized I needed to research what Python had to offer for parsing ID3 tags. There are tons of projects out there, but most of them appear to be either dead, don&#8217;t have documentation or both. In this post, you will discover the wild world of MP3 tag parsing in Python along with me and we&#8217;ll see if we can find something that I can use to enhance my mp3 player project.<span id="more-821"></span></p>
<p>For this exercise, we&#8217;ll try to get the following information from our parsers:</p>
<ul>
<li>Artist</li>
<li>Title of Album</li>
<li>Track Title</li>
<li>Length of Track</li>
<li>Album Release Date</li>
</ul>
<p>We&#8217;ll probably need more metadata than that, but this is the usual stuff I care about in my mp3 playing experience. We will look at the following 3rd party libraries to see how they hold up:</p>
<ul>
<li><a href="http://pypi.python.org/pypi/mutagen/1.12">Mutagen</a></li>
<li><a href="http://eyed3.nicfit.net/">eyeD3</a></li>
<li><a href="http://nedbatchelder.com/code/modules/id3reader.html">id3reader.py</a></li>
</ul>
<p>Let&#8217;s get started!</p>
<h2>Can Mutagen Save the Day?</h2>
<p>One of the reasons to include Mutagen in this round up is because it supports ASF, FLAC, M4A, Monkey&#8217;s Audio, Musepack, Ogg FLAC, Ogg Speex, Ogg Theora, Ogg Vorbis, True Audio, WavPack and OptimFROG in addition to MP3 parsing. Thus, we could potentially expand our MP3 player quite a bit. I was pretty excited when I found this package. However, while the package appears to be actively developed, the documentation is almost non-existent. If you are a new Python programmer, you will find this library difficult to just jump into and use.</p>
<p>To install Mutagen, you&#8217;ll need to unpack it and navigate to its folder using the command line. Then execute the following:</p>
<p><code><br />
python setup.py install<br />
</code></p>
<p>You may be able to use <a href="http://pypi.python.org/pypi/setuptools">easy_install</a> or <a href="http://pypi.python.org/pypi/pip">pip</a> as well, although their website doesn&#8217;t really say one way or the other. Now comes the fun part: trying to figure out how to use the module without documentation! Fortunately, I found a <a href="http://www.mydigitallife.co.za/index.php?option=com_content&#038;task=view&#038;id=1046123&#038;Itemid=43">blog post</a> that gave me some clues. From what I gather, Mutagen follows the <a href="http://id3.org/">ID3 specification</a> pretty closely, so rather than abstracting it so that you would have functions like GetArtist, you actually end up reading ID3 text frames and use their terminology. Thus, TPE1 = Artist (or Lead Singer), TIT2 = Title, etc. Let&#8217;s look at an example:</p>
<pre class="python"><span style="color: #66cc66;">&gt;&gt;&gt;</span> path = r<span style="color: #483d8b;">'D:<span style="color: #000099; font-weight: bold;">\m</span>p3<span style="color: #000099; font-weight: bold;">\1</span>2 Stones<span style="color: #000099; font-weight: bold;">\2</span>002 - 12 Stones<span style="color: #000099; font-weight: bold;">\0</span>1 - Crash.mp3'</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">from</span> mutagen.<span style="color: black;">id3</span> <span style="color: #ff7700;font-weight:bold;">import</span> ID3
<span style="color: #66cc66;">&gt;&gt;&gt;</span> audio = ID3<span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> audio
<span style="color: #66cc66;">&gt;&gt;&gt;</span> audio<span style="color: black;">&#91;</span><span style="color: #483d8b;">'TPE1'</span><span style="color: black;">&#93;</span>
TPE1<span style="color: black;">&#40;</span>encoding=<span style="color: #ff4500;">0</span>, text=<span style="color: black;">&#91;</span>u<span style="color: #483d8b;">'12 Stones'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> audio<span style="color: black;">&#91;</span><span style="color: #483d8b;">'TPE1'</span><span style="color: black;">&#93;</span>.<span style="color: black;">text</span>
<span style="color: black;">&#91;</span>u<span style="color: #483d8b;">'12 Stones'</span><span style="color: black;">&#93;</span></pre>
<p>Here&#8217;s a more proper example:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> mutagen.<span style="color: black;">id3</span> <span style="color: #ff7700;font-weight:bold;">import</span> ID3
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> getMutagenTags<span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    audio = ID3<span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Artist: %s&quot;</span> <span style="color: #66cc66;">%</span> audio<span style="color: black;">&#91;</span><span style="color: #483d8b;">'TPE1'</span><span style="color: black;">&#93;</span>.<span style="color: black;">text</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Track: %s&quot;</span> <span style="color: #66cc66;">%</span> audio<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;TIT2&quot;</span><span style="color: black;">&#93;</span>.<span style="color: black;">text</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Release Year: %s&quot;</span> <span style="color: #66cc66;">%</span> audio<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;TDRC&quot;</span><span style="color: black;">&#93;</span>.<span style="color: black;">text</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span></pre>
<p>I personally find this to be difficult to read and use, so I won&#8217;t be using this module for my mp3 player unless I need to add additional digital file formats to it. Also note that I wasn&#8217;t able to figure out how to get the track&#8217;s play length or album title. Let&#8217;s move on to our next ID3 parser and see how it fares.</p>
<h2>eyeD3</h2>
<p>If you go to eyeD3&#8242;s website, you&#8217;ll notice that it doesn&#8217;t seem to support Windows. This is a problem for many users and almost caused me to drop it from this round-up. Fortunately, I found a forum that mentioned a way to make it work. The idea was to rename the &#8220;setup.py.in&#8221; file in the main folder to just &#8220;setup.py&#8221; and the &#8220;__init__.py.in&#8221; file to &#8220;__init__.py&#8221;, which you&#8217;ll find in &#8220;src\eyeD3&#8243;. Then you can install it using the usual &#8220;python setup.py install&#8221;. Once you have it installed, it&#8217;s really easy to use. Check out the following function:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> eyeD3
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> getEyeD3Tags<span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    trackInfo = eyeD3.<span style="color: black;">Mp3AudioFile</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>
    tag = trackInfo.<span style="color: black;">getTag</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    tag.<span style="color: black;">link</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Artist: %s&quot;</span> <span style="color: #66cc66;">%</span> tag.<span style="color: black;">getArtist</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Album: %s&quot;</span> <span style="color: #66cc66;">%</span> tag.<span style="color: black;">getAlbum</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Track: %s&quot;</span> <span style="color: #66cc66;">%</span> tag.<span style="color: black;">getTitle</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Track Length: %s&quot;</span> <span style="color: #66cc66;">%</span> trackInfo.<span style="color: black;">getPlayTimeString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Release Year: %s&quot;</span> <span style="color: #66cc66;">%</span> tag.<span style="color: black;">getYear</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>This package does meet our arbitrary requirements. The only regrettable aspect of the package is its lack of official Windows support. We&#8217;ll reserve judgment until after we&#8217;ve tried out our third possibility though.</p>
<h2>Ned Batchelder&#8217;s id3reader.py</h2>
<p>This module is probably the easiest of the three to install since it&#8217;s just one file. All you need to do is download it and put the file into the site-packages or somewhere else on your Python path. The primary problem of this parser is that Batchelder no longer supports it. Let&#8217;s see if there&#8217;s an easy way to get the information that we need.</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> id3reader
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> getTags<span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    id3r = id3reader.<span style="color: black;">Reader</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Artist: %s&quot;</span> <span style="color: #66cc66;">%</span> id3r.<span style="color: black;">getValue</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'performer'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Album: %s&quot;</span> <span style="color: #66cc66;">%</span> id3r.<span style="color: black;">getValue</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'album'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Track: %s&quot;</span> <span style="color: #66cc66;">%</span> id3r.<span style="color: black;">getValue</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'title'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Release Year: %s&quot;</span> <span style="color: #66cc66;">%</span> id3r.<span style="color: black;">getValue</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'year'</span><span style="color: black;">&#41;</span></pre>
<p>Well, I didn&#8217;t see an obvious way to get the track length with this module without knowing the ID3 specification. Alas! While I like the simplicity and power of this module, the lack of support and a super simple API makes me reject it in favor of eyeD3. For now, that will be my library of choice for my mp3 player. If you know of a great ID3 parsing script, feel free to drop me a line in the comments. I saw others listed on Google as well, but quite a few of them were just as dead as Batchelder&#8217;s was.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/04/22/parsing-id3-tags-from-mp3s-using-python/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.809 seconds -->

