Introduction: MythTV / AppleTV2 Integration- Non Jailbreak Version

I've been running MythTV in my home since late 2006, and have had great joy working with it over the years (started running it on Fedora Core 6- that's how long it's been!).  

So that's why when my wife got me an AppleTV2 for my birthday last year, I just had to integrate it into my MythTV environment.  The first thing I did was jailbreak the device, and installed XBMC.  Using the built-in DLNA server that MythTV includes, I was able to stream my recordings directly to my Apple TV2 with quite a bit of success.

While XBMC is a great product, using it on an Apple TV2 is not quite ideal- it requires a jailbreak, and it's a little odd moving back and forth between the Apple UI and the XBMC UI- which our family found ourselves doing quite a bit.  Also, Apple products seem to work best when you buy into their ecosystem- I know that sounds rather "sheep"-ish, but it's true.  

This instructable outlines how I am able to watch all of my MythTV shows on my AppleTV2, while staying "genius" friendly...

Step 1: My Environment- Overview

This is my environment.  A brief description of how the setup works:

1) MythTV records a TV show
2) A custom job executes at the end of the recording which uses HandBrakeCLI to encode the video into AppleTV2 format.
3) Every 5 minutes, a powershell job executes on the Win7 VM.  This job does the following:
     a)  Uses the iTunes SDK to acquire an instance of the active iTunes "TV Shows" library.
     b)  Performs a query on MythConverg looking for all transcoded shows, and finds any not in iTunes yet.
     c)  Adds any missing shows from mythconverg into the TV Shows library
     d)  Looks through the TV Shows library, and removes any that are no longer in mythconverg

4) Wife enjoys Big Brother on our Apple TV2!

Note: All scheduling of new shows, and deletion of old shows happens through mythweb.

Step 2: Software Required

Nothing too crazy here- all software should be easily installed via the website of the application developer, or via your favorite Linux package manager.

1) A machine running Mythbackend
2) A Windows machine running iTunes (I only wrote the script in powershell)

Additional Software- Mythbackend Machine:
1) HandbrakeCLI
2) smbd

Additional Software- Windows Machine:
1) Windows powershell (if running WinXP only- included with Vista and above)
2) MySQL .NET Connector

Step 3: MythTV Setup

The main task at hand here is to setup the custom job and the cifs/samba file share for Win7 to use:

1) Get my source code here (*UPDATE* please download the 11 version for mythtv .26!)- read it, understand how it works- save it as: /home/mythtv/jobs/mythtv-transcode-atv2.sh
NOTE- you must put your mythconverg database username and password in here!

2) Make it executable: chmod a+x /home/mythtv/jobs/mythtv-transcode-atv2.sh

3) Configure the new job in mythtv-setup, in the "general" menu item- as shown in figure 1:
  Here is the full text shown in the picture: /home/mythtv/jobs/mythtv-transcode-atv2.sh "%DIR%/%FILE%" "%CHANID%" "%STARTTIMEISO%"
(UPDATE: For mythtv .26, use %STARTTIMEISOUTC% instead of %STARTTIMEISO%!!)

4) Save your changes in mythtv-setup, and exit.

5) Run mythfrontend, and set our new job to run automatically after new recordings.  This is done in the Setup->Video->General menu area (as shown in figure 2).  Save and exit mythfrontend

6) Create a new cifs share using your favorite configuration tool.  My share in smb.conf looks like (make it read/write):
   [recordings]
   comment = recordings
   path = /video
   valid users = mythtv
   public = no
   writable = yes
   printable = no

Note- this share needs to be read/write because iTunes will modify the video files to put metadata tags on them!

7) Make sure your mysql database is exposed to your internal network. A quick google search will help with that.

Step 4: Win7 Setup

Assuming you have all the prerequisite software installed, this should be simply setting up the scheduled task:

1)   Setup a new mapped network drive, as shown.  Doesn't matter which drive letter- I picked Y:\.

2)  Get my powershell script here- understand how it works.  Save it somewhere public on your Win7 machine.

2) In scheduled tasks, create a new task, making sure you execute powershell.exe as the action (I have posted pictures of the relevant parts of my configuration).  My powershell line is as such:
powershell.exe -command "c:\users\currentuserhere\desktop\SynciTunes.ps1 -recdir Y:\ -dbhost mythhost -dbuser dbuser -dbpass dbpassword"

3) In iTunes, turn on Home Sharing, as shown.

4) On your Apple TV2, also turn on Home Sharing, using the same Apple ID as in step 3.

5) Enjoy!

Step 5: Results/Final Comments

Overall I'm quite happy with how this project turned out- I have all my mythtv shows accessible from my Apple TV2, complete with metadata (show title/subtitle/synopsis, etc). I also coded the recording date as part of the show's title in iTunes so I always know which order to watch the shows in.

*Update- I just got around to posting a picture of the results.  The date in parentheses is the original record date, which could be different than the "added" date, but normally are the same.

Potential Improvements:
1) I'd really like to not rely on a batch job on the Windows side to pull in shows.  Perhaps some sort of service could be written to be called on demand.

2) I'm not a fan of COM+, which is what the iTunes SDK is exposed as- i wish iTunes had a better API, or better yet, a DAEMONIZED version of iTunes!!  At least I could use powershell, which I AM a big fan of.

3) For a while, I put in a major hack in the form of a second powershell job that would parse out the handbrake logs and expose a status complete on the ATV2.  It did this by manipulating the name of a TV playlist in iTunes. Very hokey, but it would allow us to check the status of a transcode on demand from the ATV2.  I didn't include that here because it just didn't work all that well.

4) Sometimes shows do not delete immediately from iTunes, even after being removed via the SDK.  I'm not sure what that's about.  It eventually goes, but sometimes it takes a few runs of the sync job before they are gone.  Items do appear pretty much right away though, and that's what's important anyway.

5) Sometimes the sync script will not "find" a recording in iTunes (even though it's there), resulting it being added again.  It doesn't hurt anything, the show only shows up once in iTunes, but it slows the script down a bit.