<?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>LolocoJr &#187; mplayer</title>
	<atom:link href="http://www.railsguru.com/articles/tag/mplayer/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.railsguru.com</link>
	<description>Andy Lo-A-Foe&#039;s blog</description>
	<lastBuildDate>Thu, 21 Jul 2011 13:31:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Using MPlayer identify with Ruby</title>
		<link>http://www.railsguru.com/articles/2006/07/12/using-mplayer-identify-with-ruby/</link>
		<comments>http://www.railsguru.com/articles/2006/07/12/using-mplayer-identify-with-ruby/#comments</comments>
		<pubDate>Wed, 12 Jul 2006 12:13:00 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[Home]]></category>
		<category><![CDATA[mplayer]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.railsguru.com/2007/07/30/using-mplayer-identify-with-ruby</guid>
		<description><![CDATA[For a project I need to fetch some metadata from WAV recordings. Mplayer can extract this information nicely with the -identify command. I&#8217;m using Ruby Sessions for executing the external mplayer command, although a simple IO.POpen() might do fine too. &#8230; <a href="http://www.railsguru.com/articles/2006/07/12/using-mplayer-identify-with-ruby/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For a project I need to fetch some metadata from <span class="caps">WAV</span> recordings. Mplayer can extract this information nicely with the -identify command. I&#8217;m using <a href="http://raa.ruby-lang.org/project/session/">Ruby Sessions</a> for executing the external mplayer command, although a simple IO.POpen() might do fine too. The code wrapper class looks like this:<br/></p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt>13<tt>
</tt>14<tt>
</tt>15<tt>
</tt>16<tt>
</tt>17<tt>
</tt>18<tt>
</tt>19<tt>
</tt><strong>20</strong><tt>
</tt>21<tt>
</tt>22<tt>
</tt>23<tt>
</tt>24<tt>
</tt>25<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">require <span class="s"><span class="dl">'</span><span class="k">rubygems</span><span class="dl">'</span></span><tt>
</tt>require_gem <span class="s"><span class="dl">'</span><span class="k">session</span><span class="dl">'</span></span><tt>
</tt><tt>
</tt><span class="r">class</span> <span class="cl">MediafileInfo</span><tt>
</tt>        <span class="co">MPLAYER_BINARY</span> = <span class="s"><span class="dl">&quot;</span><span class="k">mplayer</span><span class="dl">&quot;</span></span><tt>
</tt>        <span class="co">MPLAYER_IDENTIFY</span> = <span class="s"><span class="dl">&quot;</span><span class="k">-identify -vo null -ao null -frames 0</span><span class="dl">&quot;</span></span><tt>
</tt><tt>
</tt>        <span class="r">def</span> <span class="fu">initialize</span>(filename)<tt>
</tt>                stdout, stderr = <span class="s"><span class="dl">'</span><span class="dl">'</span></span>, <span class="s"><span class="dl">'</span><span class="dl">'</span></span><tt>
</tt>                shell = <span class="co">Session</span>::<span class="co">Shell</span>.new<tt>
</tt>                shell.execute <span class="s"><span class="dl">&quot;</span><span class="il"><span class="dl">#{</span><span class="co">MPLAYER_BINARY</span><span class="dl">}</span></span><span class="k"> </span><span class="il"><span class="dl">#{</span><span class="co">MPLAYER_IDENTIFY</span><span class="dl">}</span></span><span class="k"> </span><span class="il"><span class="dl">#{</span>filename<span class="dl">}</span></span><span class="dl">&quot;</span></span>, <span class="sy">:stdout</span> =&gt; stdout, <span class="sy">:stderr</span> =&gt; stderr<tt>
</tt><tt>
</tt>                vars = (stdout.split(<span class="rx"><span class="dl">/</span><span class="ch">\n</span><span class="dl">/</span></span>).collect! { |o| o <span class="r">if</span> o =~ <span class="rx"><span class="dl">/</span><span class="k">^ID_</span><span class="dl">/</span></span> } ).compact!<tt>
</tt><tt>
</tt>                vars.each { |v|<tt>
</tt>                        a, b = v.split(<span class="s"><span class="dl">&quot;</span><span class="k">=</span><span class="dl">&quot;</span></span>)<tt>
</tt>                        eval <span class="s"><span class="dl">&quot;</span><span class="k">@</span><span class="il"><span class="dl">#{</span>a.to_s.downcase<span class="dl">}</span></span><span class="k"> = </span><span class="ch">\&quot;</span><span class="il"><span class="dl">#{</span>b<span class="dl">}</span></span><span class="ch">\&quot;</span><span class="dl">&quot;</span></span><tt>
</tt>                }<tt>
</tt>        <span class="r">end</span><tt>
</tt><tt>
</tt>        <span class="c"># Intercept calls</span><tt>
</tt>        <span class="r">def</span> <span class="fu">method_missing</span>(method_name, *args)<tt>
</tt>                value = eval <span class="s"><span class="dl">&quot;</span><span class="k">@id_</span><span class="il"><span class="dl">#{</span>method_name.to_s.downcase<span class="dl">}</span></span><span class="dl">&quot;</span></span><tt>
</tt>        <span class="r">end</span><tt>
</tt><span class="r">end</span></pre>
</td>
</tr>
</table>
<p><br/><br />
All ID_* lines that mplayer spits out will now be method calls (and instance variables) of your MediafileInfo object. The method_missing call is there to do proper value defaulting if the variable doesn&#8217;t exist, but I&#8217;m not using it as such right now. To get the playing time of every type of mediafile that mplayer understands you simply do someting like:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">info = <span class="co">MediafileInfo</span>.new(<span class="s"><span class="dl">&quot;</span><span class="k">voicemail.wav</span><span class="dl">&quot;</span></span>)<tt>
</tt><tt>
</tt>puts <span class="s"><span class="dl">&quot;</span><span class="k">Voicemail playtime: </span><span class="il"><span class="dl">#{</span>info.length.to_i<span class="dl">}</span></span><span class="k"> seconds</span><span class="dl">&quot;</span></span></pre>
</td>
</tr>
</table>
<p>Ruby 0wnz! <img src='http://www.railsguru.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> <br/><br />
<br/></p>
<div id="in_post_ad_bottom_1" style="clear:both;margin: 5px;padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "pub-6322765154114793";
/* 300x250, created 7/3/11 */
google_ad_slot = "6228709946";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://www.railsguru.com/articles/2006/07/12/using-mplayer-identify-with-ruby/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

