Convert Sage recordings to iPhone
I have posted an updated method. I STRONGLY SUGGEST YOU USE IT INSTEAD! I leave the below for posterity…
NOTE: mencoder is not cutting commercials properly on my system. Everything else is working fine. I’ll post an update when I get it working.
Here is how I’m converting SageTV recordings to iPhone-compatible (and Touch/iPod-compatible) MP4 videos. This will also work for the Zune!
At a high level, the steps (and applications used) are:
- DVRMSToolbox: monitor the recordings directories, identify which shows to process, grab the metadata that is later written to the MP4, and control program flow
- comskip or ShowAnalyzer: Scan for commercials
- mencoder: Cut commercials
- ffmpeg: Convert to MP4 (note: I had problems with the latest Windows build of ffmpeg, but the one from babgvant.com worked fine)
- Option 2 is to use mencoder to cut out commercials and convert to MP4 in single pass, and then use NicMP4Box to fix the MP4 container to make it iPod-compatible
- Apparently mencoder’s current implementation (2008-09-17) of lavf correctly handles the MP4 container, but I haven’t verified this. If true, you can skip this step.
- Also (apparently), the latest SVN of AtomicParsley can fix this, but you have to build it yourself.
- Option 2 is to use mencoder to cut out commercials and convert to MP4 in single pass, and then use NicMP4Box to fix the MP4 container to make it iPod-compatible
- AtomicParsley: Write metadata to identify as TV Shows (plus any other info we have)
I am assuming basic familiarity with DTb, so I’ll talk about how to setup the Profile to process a Sage recording, but I am not addressing installing DTb or setting up the processing condition.
More after the break…
DTb: Setup the Profiles
Note you will need to create one profile for SD and one for HD. The only difference will be profile indicated in the the call to mencoder/ffmpeg (step 6).
- Throttle to prevent unlimited workers from running
- Set a timeout (trust me – the first time mencoder hangs and you don’t have “Lost” waiting for you in the morning like you were expecting, you’ll be glad)
- Update OutputFile context to allow for MP4 extension:
FIND IN OutputFile: ^((.*\)(.*).(.*))z
REPLACE IN OutputFile: $2$3.mp4 - Create a new context to specify the Sage commercial file location:
FIND IN InputFile: ^((.*\)(.*).(.*))z
REPLACE IN SageEDLFile: $2$3.edl - Find commercials
- Option 1 – ffmpeg
Note: this process is tried and true, but you suffer a performance hit since mencoder has to rewrite the entire file to cut the commercials.- Cut commercials with mencoder
mencoder “%InputFile%” -edl “%SageEDLFile%” -oac copy -ovc copy -of mpeg -o “%OutputFile%.mpg” - Convert with ffmpeg
Profile: X264 with AAC
- Cut commercials with mencoder
- Option 2 – mencoder
Note: mencoder does not have the best implementation of the mp4 container, and I have had problems getting the conversion to work properly. However, if you can get it to work, the overall process is faster)- Cut and convert with mencoder
mencoder “%InputFile%” -edl “%SageEDLFile%” -profile x264IPod-SD -o “%OutputFile%.tmp.mp4″ - Remux MP4 with NicMP4Box
NicMP4Box -add “%OutputFile%.TMP.mp4″ “%OutputFile%”
- Cut and convert with mencoder
- Add basic metadata to MP4:
AtomicParsley “%OutputFile%” –genre “TV Shows” –stik “TV Show” –TVNetwork %channelName% –TVShowName “%Title%” –TVEpisode “%episode%” –description “%description%” –category=”%category%” - Rename the output file to something meaningful:
CONTEXT: OutputFile
PATTERN: %Title% – %originalAirDate% – %episode%
Setup the Profiles
I use two profiles, one for HD sources and one for SD. I resize to 640×480 or 640×360, since that’s the max resolution the iPhone can handle (Zune can handle 720×480).
A couple of other notes:
- You must use H.264 or MPEG-4 with AAC audio – the iPhone supports nothing else.
- You can mess with the bitrates (bitrate=x), but keep in mind the bitrate maximums (vbv_maxrate=x) that the iPhone supports (currently 1500).
- The maximum IDC is currently 30 (level_idc=30)
- Cabac is not supported (nocabac)
Option 1: ffmpeg
Here’s the ffmpeg profile that’s working for me right now (note the “-ac 2″ parameter for the audio encoding – this was the only way I could get the sound to play on the iPhone):
<profile>
<name>MPEG-4 with AAC HD</name>
<description>MPEG-4 with AAC HD (iPhone-compatible Archive)</description>
<extension>mp4</extension>
<video>-vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2</video>
<audio>-acodec libfaac -ac 2 -ab 128000</audio>
</profile>
Here are some other command lines that I’ve found, mostly from here, in case you want to tinker:
- ffmpeg -y -benchmark -threads 2 -i “input.dvr-ms” -async 1 -deinterlace -s qvga -vcodec libx264 -b 384000 -bufsize 384 -coder 0 -level 13 -acodec libfaac “output.mp4″
- 4:3 and 16:9:
- ffmpeg -y -benchmark -threads 2 -i “%CutDvrmsFile%” -async 1 -deinterlace -croptop 0 -cropbottom 4 -cropleft 8 -cropright 6 -s 704:480 -vcodec libx264 -b 832k -maxrate 960k -bufsize 384k -coder 1 -level 13 -flags +loop -cmp +chroma -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 -flags2 +wpred+mixed_refs+8x8dct+brdo -me umh -trellis 2 -bidir_refine 1 -refs 3 -b_strategy 1 -directpred 2 -subq 5 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -aspect 4:3 -acodec libfaac -ab 128k “%OutputFile%”
- ffmpeg -y -benchmark -threads 2 -i “%CutDvrmsFile%” -async 1 -deinterlace -croptop 56 -cropbottom 60 -cropleft 8 -cropright 6 -s 704:368 -vcodec libx264 -b 832k -maxrate 960k -bufsize 384k -coder 1 -level 13 -flags +loop -cmp +chroma -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 -flags2 +wpred+mixed_refs+8x8dct+brdo -me umh -trellis 2 -bidir_refine 1 -refs 3 -b_strategy 1 -directpred 2 -subq 5 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -aspect 16:9 -acodec libfaac -ab 128k “%OutputFile%”
- ffmpeg -i input -acodec libfaac -ab 128kb -vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 640×360 -title X output.mp4
(from http://ffmpeg.mplayerhq.hu/faq.html#SEC25)
Option 2: mencoder
[x264IPod-SD]
profile-desc=”H.264/AAC MP4 SD for iPhone –After this, use NicMP4Box to remux–”
lavdopts=threads=2
vf=pp=md,scale=640:-10,harddup
ovc=x264=yes
x264encopts=turbo=1:bitrate=768:vbv_maxrate=1500:vbv_bufsize=244:nocabac:level_idc=30:threads=2
oac=faac=yes
faacopts=br=128:object=2:mpeg=4:raw=yes
of=lavf=yes
lavfopts=format=mp4
#DtbExtension=mp4
#dtbhres=”-vf pp=md,scale={0}:-10,harddup”
[x264IPod-HD]
profile-desc=”H.264/AAC MP4 SD for iPhone –After this, use NicMP4Box to remux–”
lavdopts=threads=2
vf=pp=md,scale=640:-10,harddup
ovc=x264=yes
x264encopts=turbo=1:bitrate=1200:vbv_maxrate=1500:vbv_bufsize=244:nocabac:level_idc=30:threads=2
oac=faac=yes
faacopts=br=128:object=2:mpeg=4:raw=yes
of=lavf=yes
lavfopts=format=mp4
#DtbExtension=mp4
#dtbhres=”-vf pp=md,scale={0}:-10,harddup”
Resources:
- http://lists.mplayerhq.hu/pipermail/mencoder-users/2007-December/007692.html
- http://rob.opendot.cl/index.php/useful-stuff/ipod-video-guide/
- Getting mencoder to cut commercials with muxing errors:

If you can live with commercials it can be done in one easy step, complete with iTunes integration.
f you want to muck about with Mencoder you can also get it to work by tweaking the watch dirs.
Best of all (Drum roll please!) It’s bloody well free…
http://www.ipodifier.com/
No I’m not affilliated with the dev, apart from having helped beta test it, and I’m not affilliated with Frey apart from having wrestled with SageTv since version 2POINTpain.
V6 is a thing of beauty
Enjoy
Thanks for the suggestion! I did look into this, along with the in-built conversion tools in SageMC. For me, I prefer a little more control over the process, and, of course, I want the commercials gone (I’m not quite there yet, but I’m still working on it!). iPodifier is a great suggestion for those who don’t want to muck around with it, though.
Great conf !
I’ts works for me when I add the video into iTunes and synchronise. But when I enter directly my video url through safari, Safari can’t play it.
Do you know why ?
What do you mean “enter directly…through safari”? Does it play in Quicktime (I assume so, if iTunes can handle it)? Are you trying to stream it?