<?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>code-diesel &#187; mp3</title>
	<atom:link href="http://www.codediesel.com/tag/mp3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Reading MP3 file tags in PHP</title>
		<link>http://www.codediesel.com/pear/reading-mp3-file-tags-in-php/</link>
		<comments>http://www.codediesel.com/pear/reading-mp3-file-tags-in-php/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 06:59:04 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[pear]]></category>
		<category><![CDATA[mp3]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2080</guid>
		<description><![CDATA[how to read MP3 ID3 file tags in PHP]]></description>
			<content:encoded><![CDATA[<p>I recently had a small task to scan some MP3 files on a server to check whether the files had any metadata in them, and add them if necessary. All MP3&#8242;s have a metadata section that allows information such as the title, artist, album, track number etc. to be stored in the MP3 file itself. The metadata is stored in the ID3 format. Of course I could have used a desktop tag editor, but the files where on the server, which frequently kept changing. So a server solution was the only way to go.<br />
<span id="more-2080"></span><br />
For the solution I used the Pear MP3_Id package. There are many PHP libraries around that support manipulation of MP3 metadata, but this one was the one I narrowed down. The following examples show how you can use the package to manipulate mp3&#8242;s metadata.</p>
<h4>Installing MP3_Id</h4>
<p>MP3_Id being a Pear package we will use the Pear installer. Or you can manually download the package from <a href="http://pear.php.net/package/MP3_Id/download">Pear MP3_Id</a>.</p>

<div class="wp_codebox"><table><tr id="p20801"><td class="code" id="p2080code1"><pre class="text" style="font-family:monospace;">pear install MP3_Id-1.2.1</pre></td></tr></table></div>

<h4>Reading mp3 tags</h4>
<p>The following function reads the ID3v1 data from all the mp3 files in the given directory, returning the data as an array.</p>

<div class="wp_codebox"><table><tr id="p20802"><td class="code" id="p2080code2"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MP3/Id.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> read_mp3_tags<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    static <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    static <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$tag_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$mp3</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">&amp;new</span> MP3_Id<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">// Tags supported by the MP3_Id class</span>
    <span style="color: #000088;">$tags</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                  <span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;artists&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;album&quot;</span><span style="color: #339933;">,</span>
                  <span style="color: #0000ff;">&quot;year&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;comment&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;track&quot;</span><span style="color: #339933;">,</span>
                  <span style="color: #0000ff;">&quot;genre&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;genreno&quot;</span>
                  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    <span style="color: #009933; font-style: italic;">// Read the current directory</span>
    <span style="color: #000088;">$d</span> <span style="color: #339933;">=</span> <span style="color: #990000;">dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">// Loop through all the files in the current directory:</span>
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">!==</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$d</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #009933; font-style: italic;">// Skip '.' and '..'</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'.'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'..'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">// If this is a directory, then recursively call it</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$dir}</span>/<span style="color: #006699; font-weight: bold;">{$file}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            read_mp3_tags<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$dir}</span>/<span style="color: #006699; font-weight: bold;">{$file}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #009933; font-style: italic;">// It's a mp3 file so read the tags</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;mp3&quot;</span><span style="color: #009900;">&#41;</span> 
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mp3</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$dir}</span>/<span style="color: #006699; font-weight: bold;">{$file}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #009933; font-style: italic;">// OOPs, some error occured, just save the filename</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>PEAR<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span> 
                    <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$file</span><span style="color: #339933;">;</span>
                    <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'directory'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dir</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000000; font-weight: bold;">else</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$file</span><span style="color: #339933;">;</span>
                    <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'directory'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dir</span><span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #009933; font-style: italic;">// Read all the tags of the particular file</span>
                    <span style="color: #000000; font-weight: bold;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tags</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span>
                    <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mp3</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTag</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">// Call the above function on your mp3 directory</span>
<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> read_mp3_tags<span style="color: #009900;">&#40;</span>PATH_TO_YOUR_MP3_DIRECTORY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Writing or updating tags in an MP3</h4>
<p>After you have read ID3 tags, you may want to change or add some new tag information to the files. The following code for example adds the missing album name to the ID3 metadata.</p>
<p><b>Before you modify any files, be sure you backup them until you are sure your code works fine.</b></p>

<div class="wp_codebox"><table><tr id="p20803"><td class="code" id="p2080code3"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MP3/Id.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$mp3</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">&amp;new</span> MP3_Id<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Read the MP3 file</span>
<span style="color: #000088;">$mp3</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tears For Fears - Shout.mp3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Add the album name to the 'album' tag</span>
<span style="color: #000088;">$mp3</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setTag</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;album&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Songs from the Big Chair&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// This is essential. We need to write back the changes</span>
<span style="color: #000088;">$mp3</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>If you have duplicate files of the same song, you can also quickly copy tags from one file to the other.</p>

<div class="wp_codebox"><table><tr id="p20804"><td class="code" id="p2080code4"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MP3/Id.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$mp3_1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">&amp;new</span> MP3_Id<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mp3_2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">&amp;new</span> MP3_Id<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$mp3_1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tears For Fears - Shout.mp3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mp3_2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tears For Fears.mp3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Copy tags from 'Tears For Fears - Shout.mp3' to 'Tears For Fears.mp3'</span>
<span style="color: #000088;">$mp3_2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">copy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mp3_1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mp3_2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Some other PHP libraries for ID3 manipulation</h4>
<p><a href="http://code.google.com/p/php-reader/wiki/ID3v2">http://code.google.com/p/php-reader/wiki/ID3v2</a><br />
<a href="http://getid3.sourceforge.net/">http://getid3.sourceforge.net/</a></p>
<div  class="download2">
<a href="http://www.codediesel.com/downloads/MP3Scan">Download Source</a><br />
<span>Downloads : 1046  / File size : 925 B</span>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/pear/reading-mp3-file-tags-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

