Transcoding and Tagging Video

Posted by Doug Thu, 05 Apr 2007 13:37:00 GMT

A while back I posted I was Officially Cable TV Free. We’re getting our TV content pretty much only from bittorrent. I personally think it’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 ffmpeg and some fun use of Hpricot to automatically parse TV.com.

Let me recap my setup. I have a Linux file server. I have a 12” G4 Apple PowerBook hacked to run FrontRow. I use Torrentflux 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’m able to still load up content even while I’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.

Here’s a shell script I wrote that uses midentify (part of the mplayer package that uses the -identify flag of mplayer) to figure out the parameters of the input file (like picture dimensions) and then run ffmpeg with what I’ve found are reasonable settings. I ripped these command-line options off from iSquint by using it to transcode then checking the process list to see what ffmpeg command it was running.

#!/bin/sh
#set -x

file=$1
outfile=`echo $file | sed -e 's/\.avi/.mp4/'`
donefile=`echo $file | sed -e 's/\.avi/.avi.done/'`
midentify "$file" | sed -e 's/^/export /' > /tmp/midentify.sh
. /tmp/midentify.sh
# from visual hub:
echo "****************************"
echo "$outfile - $ID_LENGTH seconds"
echo "****************************"
time ffmpeg -y  -i "$file"  -threads 4 -s \
"${ID_VIDEO_WIDTH}x${ID_VIDEO_HEIGHT}" -aspect \
"${ID_VIDEO_WIDTH}:${ID_VIDEO_HEIGHT}"   -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  "$outfile"

mv "$file" "$donefile"

This gets me a nice h.264 compressed mp4. What it doesn’t get me is a file tagged in such a way that iTunes will recognize that it’s a TV Show, the show name, episode name, season, and episode number. All that information is available on TV.com. Tagging video isn’t quite as easy as tagging audio with id3 tags. However, AtomicParsely does the job admirably.

Using Why the lucky stiff’s Hpricot I was able to fairly easily parse the information out. I created a TvDotCom ruby module an put it up on RubyForge. If you do an svn checkout, there’s a bin called atomictv.rb 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.

The net result is that my transcoding and tagging is all automated now. This should significantly improve the WAF of our cable-less TV setup.

Posted in , , , ,  | Tags , , , , , , ,  | no comments

Comments

Comments are disabled

Copyright 2001 - 2005 by Lathi.net and Doug Alcorn

Creative Commons, Some Rights Reserved Ruby on Rails Developer Powered by Debian GNU/Linux Powered by Typo