Fun with Automator

Automator iconThis post also titled “how to automatically download TV shows and convert them for your iPhone.”

Facts:

  • I have a shiny new iPhone in need of content
  • TVRSS is great
  • NetNewsWire is godly
  • Transmission makes a fine BitTorrent client
  • VisualHub is an awesome app
  • AtomicParsley is kind to metadata
  • iTunes is not nice with metadata
  • Automator kinda sucks, but can be made functional
  • AppleScript is great
  • Folder Rules FTW

I needed a way to get stuff  from TVRSS into iTunes and onto my iPhone. After most of a weekend hacking and scraping and debugging, and Lather Rinse Repeat, I have come up with an Automator workflow to (in conjunction with everything listed above) automagically find, download via torrent, transcode, metadata-ify, and copy videos to your iProduct of choice. Note, this was tested with an iPhone on an Intel Mac. I will probably not be re-writing this guide for PC users. Your mileage may vary. This is based pretty heavily on LRR’s guide, with a lot of my own modifications.

You will need the following:

Ok, so you’ve got everything installed. Good. Now first make sure you downloaded and installed that script pack for automation from TechSpansion. Download and decompress, then find VisualHub. Right-click, show package contents to see what’s inside the bundle. Copy the script to the Contents/Resources/Scripts folder. Open VisualHub and the title bar should now read “VisualHub 1.x Automation Tech Preview.” If it doesn’t, you screwed up, so start over.

Easy, huh? Just you wait…

In order to use the new automation features of VH correctly now, we must create a file that contains the settings we’re going to use when run automated. When we get to running and building the script, we will load in this settings file and tell VH to go crazy. Fire up VH. Set it to iTunes, optimize for your iProduct of choice (in this example I’m using iPhone, but I’m guessing it’d work fine for the AppleTV or video iPods), and check the H.264 encoding box. Leave the Quality slider at High and make sure the Send to iTunes box is unchecked. Set your Save To path, and make sure you remember it. This will be important later on. Once you have everything set like you’re going to want it, hit the Advanced button and then the Save Settings button. Pick a path (I chose ~/Documents) and name it iphone.vhub.

Now that VisualHub’s setup, let’s go find some shows we want. Go over to TVRSS.net and find a show you want. Switch  groups to either EZTV or VTV to avoid duplicates, and then right click and copy the search-based RSS feed. From there, create a new subscription in NetNewsWire with Cmd-N and paste in the feed URL (NNW should auto-paste it.) Once you’ve got the feed subscribed, go into your NNW prefs and into the Downloading section. Set your downloads folder to your Desktop, and create a sub-folder for each feed. Once this is setup, select the feed in your list and hit Cmd-I. Expand the Enclosures section, check the box for Use Custom Setting, and check the box for Other enclosures. Close this and you’re good.

NNW feed settings

Since the .torrent files are enclosed in the feed, NNW will automatically download them and put them in ~/Desktop/NetNewsWire Enclosures/tvRSS – search based. Now we need to tell Transmission to keep an eye on this folder, and automatically add any .torrents that are created.

Fire up Transmission and rip open your prefs. Create a new folder on your desktop, I named it “torrents.” Set NNW to Download to this folder. Check the box for Keep Incomplete File In: and choose the tvRSS – search based folder (~/Desktop/NetNewsWire Enclosures/tvRSS – search based). Check the box to trash original .torrent files. Now the important part: set the tvRSS – search based as the folder to watch for auto-add.

transmissionsettings

From here, your Transmission configuration is complete. NNW will refresh the search feed every so often, auto-save the attached torrents to a folder that Transmission will keep an eye on. When it sees new torrents, it’ll start downloading them, and once they’re complete it will move the files to ~/Desktop/torrents. This is the important part, because now we’re going to bust out Automator and create a workflow then folder action that will run whenever files are put in the torrents folder.

First, create another folder on your desktop named iphonemovies. Create a subfolder in there called temp. VisualHub is going to get the original files from the torrents folder, transcode them, and dump them in ~/Desktop/iphonemovies/temp.

In Automator, create a new workflow. Add the “Get Specified Finder Items” action in position 1. Leave it blank, since it’ll fill in when we attach it to a folder.

Add a Run AppleScript in position 2. Jam this code in there, replacing with the name of your hard drive and your short username:

on run {input, parameters}
--we need to unset the default timeout length of two minutes, we'll make it 45
with timeout of (45 * 60) seconds

tell application "VisualHub" to set VisualHub to load script (scripts path of main bundle & "/automation.scpt" as POSIX file)

tell VisualHub
LoadSettings("YOURHARDDRIVE:Users:YOURNAME:Documents:iphone.vhub")
SetSaveLocation("YOURHARDDRIVE:Users:YOURNAME:Desktop:iphonemovies:temp")
AddFiles(input)
StartConversion()
QuitApp()
end tell
end timeout
return input
delay 15
end run

This will do all the VisualHub fun. From here, we’re doing good, except for one thing. We need to add some metadata so iTunes knows what to sync. In this case, we’re setting the STIK atom to 10, which iTunes recogizes as a TV show. If we don’t set this atom, iTunes will add in this file as a movie, and that caused issues for me. While we’re at it, we’ll set the TV show name to DailyShow, for easier iPhone syncing later.

Add a third action, Run Shell Script in position 3. Set it to ignore results from previous action, and paste this in:

for i in ~/Desktop/iphonemovies/temp/*.mp4; do /Applications/MetaX.app/Contents/Resources/AtomicParsley $i --stik value=10 --TVShowName DailyShow -o $i >> /dev/null;rm $i;done

Add a fourth action, Find Finder Items. Set the Where to ~/Desktop/iphonemovies/temp and the Kind Is Movie.

Add a fifth action, Move Finder Items. Set the To to ~/Desktop/iphonemovies. Make sure this is using the input from the previous action.

Finally, add a sixth action, Run AppleScript.

on run {input, parameters}
tell application "VisualHub" to quit
tell application "iTunes" to add input
end run

Save this workflow somewhere in case you have to edit it later. We’re almost done. With the workflow open, File -> Save As Plug-In. Save it for Folder Actions, name it something like “VH to iPhone,” and attach it to the download folder on your desktop.

Try it out. Drop an avi file into your torrents folder on your desktop. VisualHub should automatically spring in to action, load up the settings for the iPhone, and start transcoding to H.264. Once it’s done, the shell script’ll add the TVShow STIK atom, set the name to DailyShow.

From here, all you have to is set iTunes to sync X most recent episodes of DailyShow and you’re set.

iTunesSettings

You’re done. There are a couple bugs I’ve found though:

  • If VH is transcoding one file and another one falls into the torrents folder, the folder action won’t run because VH is already open.
  • If you subscribe to multiple shows, make sure the name of the feeds in is NNW the same. Transmission can only watch one folder at a time.
  • Not a bug, but this could be easily expanded to transcode to AppleTV or iPod/Touch, just change the settings in the .vhub file.

Enjoy, and leave comments with issues/etc.

Advertisements

16 Responses to “Fun with Automator”

  1. Joe Phillips Says:

    Great job on putting this together. I tried to do this a while back, but I just don’t understand some of the scripting required. The biggest issue I kept running in to was the script timing-out, which you addressed by extending the time-out to 45 minutes. That was a huge piece of the puzzle for me.

    I did have an issue with copy/pasting your scripts, as it would not compile until I manually replaced all of your quotation marks. I don’t know the reason, but FYI.

    I am testing a conversion right now and I will report back after the last half of the script completes.

    Thanks!

  2. professafresh Says:

    Not sure on the quotation mark issue, maybe it’s wordpress being stupid and using the “shifted” inward/outward quotes instead of regular vertical ones…

    Glad you liked it, can you think of any way around the “can’t add more files while VH is running” issue? If you dump more than 1 file into the folder with the action at once, VH seems to grab everything…

  3. Joe Phillips Says:

    I am working on a couple ideas. The easiest way I can think of is to tie the automator action to a ical event or a cron job so it runs once an hour or once a day. Would that work?

    I still had some trouble getting the scripts to run all the way through, but I am testing to see what the issue is.

    I know that MetaX has a preset function. Is there a way to apply those presets from the command line in a script? I haven’t found much documentation on atomic parsley command line options, but I just started looking.

    I would like to have a couple shows set up to DL automatically, convert to an appletv friendly format, mark each with show name and album art, mark as “TV Show”, and import to iTunes. I am using T.E.D. (which seems easier that individually putting feeds in NNW), DL with Transmission, but I am still working out the bugs with VH and MetaX. I am thinking I can have folder actions move the shows by name in to folders, and then have a folder action on the sub folder like yours to convert, name, and import them.

  4. professafresh Says:

    you could tie it to iCal/cron, but I like having fresh content on my iPhone when I undock it as I leave the house. That and I never know when exactly 1. TVRSS will update with the new torrents, or 2. when said torrents will be complete.

    Were I dumping stuff right on my AppleTV, I’d probably definitely kickoff with cron/iCal… I’m definitely going to give T.E.D. a check, looks interesting.

    At some point, I want to somehow figure out how to auto-expire episodes (a-la Miro/Democracy or TiVo) out of iTunes if they’re date-sensitive (i.e. I don’t want to watch week-old Daily Show episodes).

  5. josephbphillips Says:

    -o $i >> /dev/null;rm $i;done

    What does this part do? Can you break it down by each piece of syntax? I do not understand the syntax for scripting and am having a hard time finding a basic enough resource. I did find some good info on all of the AP variables that may be assigned:

    http://todd.dailey.info/archives/2007/04/02/more-apple-tv-metadata-fun/

    Thanks

  6. professafresh Says:

    The chunk
    for i in ~/Desktop/iphonemovies/temp/*.mp4; do /Applications/MetaX.app/Contents/Resources/AtomicParsley $i –stik value=10 –TVShowName DailyShow -o $i >> /dev/null;rm $i;done
    basically does the following:

    -Iterates through all the MP4 files in ~/Desktop/iphonemovies/temp
    -Adds the STIK value of 10 (which iTunes understands as video type = TV Show)
    -Adds the TV Show name
    -Outputs to the same file it found while iterating
    -Redirects all output from stdout to /dev/null
    -Ends

    There’s got to be a more elegant way of doing this…

  7. josephbphillips Says:

    Ok, I am trying to go another direction to simplify things. Rather than converting, I am trying to use “apple tv fooler” (http://wiki.awkwardtv.org/wiki/Talk:How_to_get_iTunes_to_sync_other_video_formats_to_Apple_TV).

    If you installed Perian on your apple tv through the Patchstick or through the ATVLoader, your apple tv can play other formats, but it won’t sync them. The fooler works by appending a little clip to the front of a movie that is in the format Apple TV expects. That way, even though the bulk of the file is an unconverted AVI, it has a MOV extension and is imported to iTunes and synced to apple tv the same as a file that has been converted with VH.

    This simplifies the entire process, reduces the processing time dramatically, and preserves the picture quality of the original file. The only downside is that the new file is not able to be manipulated with atomic parsley as this is not a true mp4 file.

    Doug’s Applescript for Set Video of Kind does, however, allow you to set metadata on these files. That means that I need to figure out how to use applescript to add metadata in the same way he does. The end result will be a smaller, higher quality file with the same meta data- and it is handled in exactly the same way by iTunes and Apple TV. This also eliminates the issue of trying to add a new file to the batch while VH is running, as the conversion time is seconds and can, I believe, run with concurrent sessions.

    So as it stands, my new workflow is 1. Run as folder script on all .avi files in my “downloads” folder 2. run script “apple tv fooler” 3. ???? 4. Run Script “add to itunes”. I just need to figure out that step 3 syntax to add meta data. The answer may be that it is actually done after the import to itunes- I’m not sure yet.

    The syntax for adding the meta data seems to be something like
    on open fileList
    tell application “iTunes”
    set video kind to TV show
    end tell
    end open

    Any ideas?

    Hope this is helpful to you!!

  8. professafresh Says:

    Oooh, awesome, I’ll have to play with that and see what happens. Thanks for the heads-up.

    I know already that since there’s no M68K version of Perian for the iPhone, it might get interesting from there…

  9. josephbphillips Says:

    I think I am on the right track now! I cobbled together a script that will add metadata to all of the files in a user-created smart playlist. I set up folder rules to monitor my download folder and open video files with the “fooler” script, and then use a separate script to add the resulting file to iTunes. Lastly, i run an instance of this script for each show I regularly download. I know there is a better way to roll this in to a single script with if statements, but baby steps. I hope it is helpful:

    –Before running, create a smart playlist using the “name contains” variable to select the appropriate files. This example presumes you have set a smart folder named “Sample Show Name”. It will edit all of the metadata for the shows in this smart playlist as set below. Thanks to allenhuffman on macosxhints.com for the script that I modified to achieve this.
    tell application “iTunes”

    set smartPlaylists to (get every user playlist whose smart is true and name contains “Sample Show Name”)
    repeat with thisPlaylist in smartPlaylists
    set allTracks to every track of thisPlaylist
    repeat with this_track in allTracks
    try
    set video kind of this_track to TV show
    set show of this_track to “Sample Show Name”
    set season number of this_track to “1”
    on error
    display dialog “Failed to add Metadata”
    end try
    end repeat
    end repeat

    end tell
    —-end—

  10. Nic Says:

    It appears that the problem (at least for me) is in the Shell Script part. It won’t actually change the tags for me – Just adds it as a Movie file with original name.

    Great work so far though!

    Any ideas?
    Cheers.

  11. Nathan Says:

    Hey, have you looked at maybe using TVShows (http://tvshows.sourceforge.net/) in this setup? You could probably throw out the NetNewsWire and TVRss bits all together!

    My setup at the moment uses TVShows and Transmission, so my shows automatically download to my Downloads folder. But, seeing as i’m probably going to be getting a iPhone pretty soon, i’m gonna add the VisualHub transcoding stuff to my setup.

    Also, is there not a better way to do the meta-data stuff? Maybe setup a smart playlist with shows added in the last 24 hours that syncs to your iPod?

  12. InfoMofo Says:

    Grr, I’m so close to getting this to work, but I’m getting an error in the applescript with the line:

    tell application “VisualHub” to set VisualHub to load script (scripts path of main bundle & “/automation.scpt” as POSIX file)

    The AppleScript error occurs at “bundle”, and I get “Syntax error: Expected “,” but found identifier”.

    Any ideas? Also, is there a way to make this watch a higher level folder and automatically set the TV show metadata name with the folder name?

  13. professafresh Says:

    http://www.techspansion.com/visualhub/vh-automationpreview.pdf has all the info you need on getting VisualHub automated.

  14. drnkusv Says:

    This was a real time-saver. Thanks. One thing that is driving me a little crazy, though. Why does VisualHub restart itself at the end of the workflow? (In your workflow, you instruct VisualHub to quit and then you have another instruction later on to quit VisualHub. I have also noticed when running a slightly modified version of this, that VisualHub restarts itself right before the workflow ends. It seems that the workflow loops back up to the top when it finishes, but I cannot figure out why. I notice this only happens when it is called via folder actions — it does not happen if you run the workflow as a test directly through automator.

    Not a major problem — but it would be nice to understand.

    Thanks again.

  15. Chris Says:

    I just spent several hours trying to get your automation scripts to run. The comments about the “” issue is valid – a copy and paste from the above text will insert the wrong quotes. The part that I struggled with (since I am new to Automator) was that after AtomicParsley64 ran everything came to a dead stop. I stepped through the workflow and everything worked correctly. I ended up having to insert a “Pause” for 5 seconds right after the Shell Script but before the Find Finder. This is on a OS X 10.5.3 2.66GHz iMac 2GB RAM. Works fine now. Thanks!

  16. InfoMofo Says:

    I know it’s almost a year later, but this page is still the best source for this information!

    I just got this working perfectly, using josephbphillips’s metadata script above. This is great for getting shows onto my iPhone, and also for streaming movies to my mac mini’s frontrow (my network seems to be too slow to stream the full hd downloaded eps).


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: