3/20/13

Workflow Processing Multiple Files - TiVo Edition



It seems that there are a lot of people that have been struggling with their TiVo. Like me they want to watch shows when the want, where they want and on the device they want. The problem is getting the recorded show off the TiVo, decrypted, edited then transcoded into a file type that their personal device can play. 

I originally wrote about an application called iTiVo that made this process a lot easier.

TiVo & the Mac - Downloading TiVo files

Then an OS update broke iTiVo so I wrote up a work around. You can read about it here:

TiVo to Mac - Mountain Lion Edition

After a few months, when iTiVo finally became usable again, I wrote up a quick way to update the beta version to get full functionality back. 

Continuing Adventures with iTiVo

I don't know when iTiVo will be updated to work without major surgery but hopefully it will be soon. So while we wait I want to answer a question that has been asked a lot during this whole process; How do I create a Apple Workflow that will process multiple files? This question doesn't just apply to processing TiVo files of course but I'll use that as my example.

Apple provides a great tool for speeding up and automating your work tasks. It's called Automator. It's an easy tool to get to know but getting it to do just what you want, in the way you want can be a trial. 

Let's assume for a minute that iTiVo is broken again and you want to watch the entire season of Supernatural that currently sits on your TiVo. Downloading the files is easy. You can use the web version I wrote up here, or if you have Roxio's Toast you can use the included "TiVo Transfer" application to download your files. Either way you end up with a file on your Mac that is encrypted. So let's decrypt them. 

Assuming you have followed all my directions you should have a copy of tivodecode loaded on your Mac. Now all you need is an Automator Workflow to convert all the files. Not one at a time, but in a batch so you can set it running before you go to bed. 

Open up the Automator application and create a new Workflow


In the column at the left you will need to select "Files & Folders" icon on the left then drag the "Ask for Finder Items" into the window pane on the right.  In the "Prompt" section just type in "Choose a Tivo file(s)" and fill the folder location you normally use when you download your Tivo files.  To handle multiple files make sure that the Type is set to "Files" and "Allow Multiple Selection" box is checked. 




In order to deal with multiple files we need to assign them as variables. Select "Utilities" from the Library and drag "Set Value of Variable" to the right window pane. In the Variable box create a new variable. I called mine "Tivo_File". 




Just as an explanation, the variable "Tivo_File" will be set to the file or files you select manually when you run the action. The value of the variable will be the full name of the file including path.

example:

.../TiVo Files/Supernatural_Hunteri_Heroici_111 KSTWDT_1363233538.TiVo

This is a very important point that we will have to deal with later in the shell script. The tivodecode application needs both the path to the file and the file name.  Now that the variable is set we need to use it.

Again select "Utilities" from the Library and drag "Get Value of Variable" to your script. It seems odd, and you would think the computer should remember the variable because you just set it, but that's the way scripting works. Especially when dealing with multiple files. Make sure that the Variable is set to "Tivo_File" or whatever name you choose in the set variable value step.



Another important point is to make sure the "Ignore this action's input" check-box is checked in the options tab since our input is the variable itself. 

Now that we have our variable we need to run a shell script to decode it. Grab the "Run Shell Script" action from utilities and move it to the Window pane on the right.  Make sure the "Shell" is set to "/bin/bash". 

The first thing we do is start a loop to process each of the files you selected. I have titled my loop counter as the letter "i".  

      for i in "$@"
       do

After we start our loop we need to decode our variable and just grab the file name without the full path information in front of it. To do that I choose to use the UNIX awk command. There are a lot of ways to do this but this one works. The assumption here is that your file name extension has .TiVo at the end. Or to be more exact, it has a dot "." and an extension at the end of the file name.  I've set the file name to a script variable called "fname". 

fname=`echo $i | awk -F"/" '{print $NF}'| awk -F"." '{print $1}'`

After we have the file name we need to run tivodecode. 

tivodecode -m 1234567890 -o "/MacHD/Users/Username/Movies/$fname.mpg" "$i"

Remember to insert your real MAK address into the command where I have put "1234567890". Also insert the real path to your output file. The output file will be the same as your downloaded TiVo file name but with an ".mpg" extension.  The "$i" at the end of of the statement is the full path & filename. Make sure to inlude the quotes as I have shown above.

Since that's all the processing we need to close the loop by adding "done" at the end of the script.   This is how your script should look.



Since that's the last step here's a copy of the entire automator action


You can check your script at this point by hitting the run button at the top. If it all works as planned you are ready to save your Automator Workflow as an executable app. Make sure it's saved as an application in the "File Format" drop down menu.

Happy converting and I hope this answers the question of how to process multiple input files in a single script. 



7 comments:

  1. Wow this is going to be great when I get it going! Thanks. So far I'm getting an error on the Run Shell Script step "opening output file: No such file or directory." My step reads like this, which reflects my own HD name and user folder name I obscured my MAK address with x's for this post:

    tivodecode -m xxxxxxxxxx -o "/Macintosh HD/Users/john/Movies/$fname.mpg" "$i"

    I did set the Pass input: popup to "as argument" which is what shows in your screen shot, but you didn't mention it specifically. Also in the original Automator app you prefaced the tivodecode command with /usr/sbin/. Is that not needed in this one?

    ReplyDelete
  2. You are correct about the /usr/sbin for tivodecode. To be more exact you can always list the full location to the app. It's probably not required as /usr/sbin should be in your search path.

    To check your script and see where your error might be you can put in some echo statements then look at the results from the script. Adding a "#" sign will comment the line out so it is skipped. That way you don't need to delete a line. Here's the same script that echo's the variables back to you so you can see what's going on.

    Just to make sure you have the context correct the fname variable starts and ends with the backtick " ` " That character is located right below the esc key on most keyboards.


    for i in "$@"
    do
    fname=`echo $i | awk -F"/" '{print $NF}'| awk -F"." '{print $1}'`
    #tivodecode -m 1234567890 -o "/MacHD/Users/Username/Movies/$fname.mpg" "$i"
    echo $i
    echo $fname
    done


    Let me know how it goes.

    ReplyDelete
  3. Just to be clear, test your script in Workflow mode by clicking the run button. Each step will be executed separately and at the bottom of each section the results button will be green for successful or red for unsuccessful.

    The results of the echo will show up in the results section.

    ReplyDelete
  4. Bruce, I tried the # and echo elements as you described. There is a red X next to Results. Nothing shows up in the Results section, but in the Log there is a red x and "Run Shell Script failed - 1 error" followed by another red x and "-: line 6: `1': not a valid identifier (1)"

    ReplyDelete
  5. *sigh* This is why I don't relish publishing this sort of tutorial. UNIX can fail for the lack of one character. To diagnose and fix a problem long distance is almost impossible without direct access to someones machine. Let me give you a more general trouble shooting approach.

    To figure out where a script is failing check each variable before you move to the next step. For instance, comment out the "echo $fname" line and only echo $i. That should be a slam dunk and only show your list of files you manually selected. Only select 2 or3 files to process so you don't end up with a huge list. Once you see that $i is echoing correctly you know that your variable is being set and is working.

    The next step is to make sure the fname command is working. In this case, if you have verified that $i is correct then cut out everything else and set fname to $i.

    fname=`echo $i`
    echo $fname


    Make sure that works then add more of the command.

    fname=`echo $i | awk -F"/" '{print $NF}'`
    echo $fname

    etc....

    Break things down in chunks and see if you can locate where the issue is.

    ReplyDelete
  6. Bruce, I think it's working now. After trying all of your last suggestions and getting no improvement, it seemed to be the tivodecode line that was causing the problem. At one point I got a message saying there was no output directory of that name. I tried removing the "/Macintosh HD" from the front of the path for output and it worked. It just finished processing 2 files. I'm not sure why it didn't want the drive name at the beginning, which you had in yours, but it's working now.

    This is going to be great for me! Thanks for your patience and work to help others!

    ReplyDelete
    Replies
    1. Excellent!! I'm so glad it's working for you.

      Delete