<?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>JAW Speak &#187; music</title>
	<atom:link href="http://jawspeak.com/category/music/feed/" rel="self" type="application/rss+xml" />
	<link>http://jawspeak.com</link>
	<description>Jonathan Andrew Wolter</description>
	<lastBuildDate>Fri, 30 Jul 2010 20:39:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ruby Script to Organize Mp3&#8217;s based on ID3 Genre Tag</title>
		<link>http://jawspeak.com/2009/09/05/ruby-script-to-organize-mp3s-based-on-id3-genre-tag/</link>
		<comments>http://jawspeak.com/2009/09/05/ruby-script-to-organize-mp3s-based-on-id3-genre-tag/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 00:06:57 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[automation]]></category>
		<category><![CDATA[music]]></category>

		<guid isPermaLink="false">http://jawspeak.com/?p=136</guid>
		<description><![CDATA[Reading time: 2 &#8211; 4 minutes

			
				
			
		
I had one gigantic directory of all my tagged and organized mp3 files. Problem is it was too big to use. This bloated my library and I have since not been able to fit my music on my laptop. I needed to manipulate mp3 files by genre and extract them [...]]]></description>
			<content:encoded><![CDATA[<p>Reading time: 2 &#8211; 4 minutes</p>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjawspeak.com%2F2009%2F09%2F05%2Fruby-script-to-organize-mp3s-based-on-id3-genre-tag%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fjawspeak.com%2F2009%2F09%2F05%2Fruby-script-to-organize-mp3s-based-on-id3-genre-tag%2F&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><br />
			</a>
		</div>
<p>I had one gigantic directory of all my tagged and organized mp3 files. Problem is it was too big to use. This bloated my library and I have since not been able to fit my music on my laptop. I needed to manipulate mp3 files by genre and extract them out of this single directory to create smaller libraries. I spent all of about two minutes looking for a program to do this before deciding to write a script. Truthfully, it was worse: once upon a time I over-enthusiastically downloaded <a href="http://www.stepmania.com/">StepMania</a> and 493 DDR games/songs. And then, I added all the songs into my music library. It&#8217;s a great party game, but not the kind of music I want to listen to.</p>
<p>Many <a href="http://www.id3.org/Implementations">implementations</a> exist for reading <a href="http://en.wikipedia.org/wiki/ID3">ID3</a> tags. I first tried <a href="http://ruby-mp3info.rubyforge.org/">ruby-mp3info</a>, however it didn&#8217;t read my custom genre (&#8216;DDR&#8217;) so then I moved to <a href="http://id3lib-ruby.rubyforge.org/">id3lib-ruby</a> which uses the c++ <a href="http://id3lib.sourceforge.net/">id3lib</a> library.</p>
<p>This worked like a charm. I ran the script over all my directories and built up a list of the directories.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
<span style="color:#008000; font-style:italic;"># find_music.sh</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;rubygems&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'id3lib'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'find'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'set'</span>
&nbsp;
ddr_files = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
ddr_dirs = <span style="color:#CC00FF; font-weight:bold;">Set</span>.<span style="color:#9900CC;">new</span>
&nbsp;
search_dir = <span style="color:#996600;">'~/media/music/music_categorized'</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">Find</span>.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>search_dir<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">if</span> file !~ <span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#006600; font-weight:bold;">*</span>mp3$<span style="color:#006600; font-weight:bold;">/</span>
  mp3 = <span style="color:#6666ff; font-weight:bold;">ID3Lib::Tag</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>file<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">if</span> mp3.<span style="color:#9900CC;">genre</span> != <span style="color:#996600;">'DDR'</span>
  ddr_dirs <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span>file<span style="color:#006600; font-weight:bold;">&#41;</span>
  ddr_files <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> file   <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;%s, %s --&gt; AT: %s&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#91;</span>mp3.<span style="color:#9900CC;">genre</span>, mp3.<span style="color:#9900CC;">album</span>, file<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'result-ddr-files.txt'</span>, <span style="color:#996600;">'w'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
  f.<span style="color:#9900CC;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span>ddr_files.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'result-ddr-dirs.txt'</span>, <span style="color:#996600;">'w'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
  ddr_dirs.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>d<span style="color:#006600; font-weight:bold;">|</span> f.<span style="color:#9900CC;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;%s<span style="color:#000099;">\n</span>&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> d<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Next I reviewed the two output files, then ran the file <tt>result-ddr-dirs.txt</tt> in as an argument into this next script. That removed almost a gig of music from my library.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>ARGV.<span style="color:#9900CC;">length</span> != <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Usage: #{__FILE__} input_file&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">exit</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
destination=<span style="color:#996600;">&quot;/home/jwolter/media/music/music_ddr_questionable_value/&quot;</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">foreach</span><span style="color:#006600; font-weight:bold;">&#40;</span>ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>line<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">if</span> line.<span style="color:#9900CC;">strip</span> == <span style="color:#996600;">&quot;&quot;</span>
  cmd =  <span style="color:#996600;">&quot;mv <span style="color:#000099;">\&quot;</span>#{line.strip}<span style="color:#000099;">\&quot;</span> <span style="color:#000099;">\&quot;</span>#{destination}<span style="color:#000099;">\&quot;</span>&quot;</span>
  <span style="color:#008000; font-style:italic;">#puts cmd</span>
  <span style="color:#996600;">`#{cmd}`</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Bonus: In the process searching for this, I ran into the <a href="http://rubyquiz.com/quiz136.html">ID3 Tags RubyQuiz</a>.</p>
<p>One of the nicest benefits of being a software engineer is I avoid doing boring manual tasks on my computer. Writing a script is more fun, and faster. I&#8217;ve got many scripts to automate file manipulation, online banking, and more. What bit of your automation scripts do you think is the most helpful?</p>
]]></content:encoded>
			<wfw:commentRss>http://jawspeak.com/2009/09/05/ruby-script-to-organize-mp3s-based-on-id3-genre-tag/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
