<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>@Lathi.net: Transcoding and Tagging Video</title>
    <link>http://blog.lathi.net/articles/2007/04/05/transcoding-and-tagging-video</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>On Life, Fatherhood, Christianity, and Computers</description>
    <item>
      <title>Transcoding and Tagging Video</title>
      <description>&lt;p&gt;A while back I posted I was &lt;a href="http://blog.lathi.net/articles/2007/01/25/officially-cable-tv-free"&gt;Officially Cable TV Free&lt;/a&gt;.  We&amp;#8217;re getting our TV content pretty much only from bittorrent.  I personally think it&amp;#8217;s going pretty well.  My wife has one complaint: the time it takes me to get new shows loaded into iTunes so she can watch them through FrontRow.  The solution involves some nitty-gritty details of &lt;a href="http://ffmpeg.mplayerhq.hu/"&gt;ffmpeg&lt;/a&gt; and some fun use of &lt;a href="http://code.whytheluckystiff.net/hpricot/"&gt;Hpricot&lt;/a&gt; to automatically parse &lt;a href="http://www.tv.com"&gt;TV.com&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Let me recap my setup.  I have a Linux file server.  I have a 12&amp;#8221; G4 Apple PowerBook &lt;a href="http://andrewescobar.com/frontrow"&gt;hacked to run FrontRow&lt;/a&gt;.  I use &lt;a href="http://www.torrentflux.com/"&gt;Torrentflux&lt;/a&gt; on my Linux file server to download TV shows.  By pushing most of the background work to the Linux box (which is always on) I&amp;#8217;m able to still load up content even while I&amp;#8217;m traveling.  My experience has been that while most video off bittorrent is DivX avi, it plays a lot smother when I transcode it to h.264 mp4.&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s a shell script I wrote that uses &lt;code&gt;midentify&lt;/code&gt; (part of the &lt;a href="http://www.mplayerhq.hu/"&gt;mplayer&lt;/a&gt; package that uses the &lt;code&gt;-identify&lt;/code&gt; flag of &lt;code&gt;mplayer&lt;/code&gt;) to figure out the parameters of the input file (like picture dimensions) and then run &lt;code&gt;ffmpeg&lt;/code&gt; with what I&amp;#8217;ve found are reasonable settings.  I ripped these command-line options off from &lt;a href="http://www.isquint.org/"&gt;iSquint&lt;/a&gt; by using it to transcode then checking the process list to see what &lt;code&gt;ffmpeg&lt;/code&gt; command it was running.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_shell "&gt;#!/bin/sh
#set -x

file=$1
outfile=`echo $file | sed -e 's/\.avi/.mp4/'`
donefile=`echo $file | sed -e 's/\.avi/.avi.done/'`
midentify &amp;quot;$file&amp;quot; | sed -e 's/^/export /' &amp;gt; /tmp/midentify.sh
. /tmp/midentify.sh
# from visual hub:
echo &amp;quot;****************************&amp;quot;
echo &amp;quot;$outfile - $ID_LENGTH seconds&amp;quot;
echo &amp;quot;****************************&amp;quot;
time ffmpeg -y  -i &amp;quot;$file&amp;quot;  -threads 4 -s \
&amp;quot;${ID_VIDEO_WIDTH}x${ID_VIDEO_HEIGHT}&amp;quot; -aspect \
&amp;quot;${ID_VIDEO_WIDTH}:${ID_VIDEO_HEIGHT}&amp;quot;   -r 23.98   \
   -vcodec h264  -g 200  -qmin 23  -b 1200k  -level 30 -loop 1 \
-sc_threshold 40 -partp4x4 1 -rc_eq 'blurCplx^(1-qComp)' -refs 2  \
-qmax 51 -maxrate 1500k -bufsize 500k    -async 50  -acodec aac  \
-ar 48000 -ac 2 -ab 128k  &amp;quot;$outfile&amp;quot;

mv &amp;quot;$file&amp;quot; &amp;quot;$donefile&amp;quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;This gets me a nice h.264 compressed mp4.  What it doesn&amp;#8217;t get me is a file tagged in such a way that iTunes will recognize that it&amp;#8217;s a TV Show, the show name, episode name, season, and episode number.  All that information is available on &lt;a href="http://tv.com"&gt;TV.com&lt;/a&gt;.  Tagging video isn&amp;#8217;t quite as easy as tagging audio with id3 tags.  However, &lt;a href="http://atomicparsley.sourceforge.net/"&gt;AtomicParsely&lt;/a&gt; does the job admirably.&lt;/p&gt;


	&lt;p&gt;Using &lt;a href="http://whytheluckystiff.net/"&gt;Why the lucky stiff&amp;#8217;s&lt;/a&gt; Hpricot I was able to fairly easily parse the information out.  I created a &lt;a href="http://rubyforge.org/projects/tv-dot-com/"&gt;TvDotCom&lt;/a&gt; ruby module an put it up on RubyForge.  If you do an svn checkout, there&amp;#8217;s a bin called &lt;code&gt;atomictv.rb&lt;/code&gt; that takes a file, parses its name for the show, season, and episode, then looks up the meta-data from TV.com and finally tags the file with AtomicParsley.&lt;/p&gt;


	&lt;p&gt;The net result is that my transcoding and tagging is all automated now.  This should significantly improve the &lt;span class="caps"&gt;WAF&lt;/span&gt; of our cable-less TV setup.&lt;/p&gt;</description>
      <pubDate>Thu, 05 Apr 2007 08:37:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:12b910ef-536f-42a1-8d81-4e2d9b7d8951</guid>
      <author>Doug</author>
      <link>http://blog.lathi.net/articles/2007/04/05/transcoding-and-tagging-video</link>
      <category>Family</category>
      <category>Programming</category>
      <category>TV</category>
      <category>Software</category>
      <category>Mac OS X</category>
      <category>iTunes</category>
      <category>mplayer</category>
      <category>hpricot</category>
      <category>tv</category>
      <category>rubyforge</category>
      <category>atomicparsley</category>
      <category>ffmpeg</category>
      <category>Ruby</category>
    </item>
  </channel>
</rss>
