Difference between revisions of "Automation"
Simplicity (talk | contribs) (→Notes) |
Simplicity (talk | contribs) (→Automation) |
||
Line 2: | Line 2: | ||
==Notes== | ==Notes== | ||
− | This is a Mac only process and works on Leopard 10.6.4 and with iTunes 9.2. It should be reasonably future proof. Unfortunately, I don't know how to script anything on Windows and I have cobbled together what I have found in other places to get this working. I am not an Applescript expert by any means, so improvements are welcome, although it would be good to keep the original script for reference. Your thanks / comments are gratefully received. | + | This is a Mac only process and works on Leopard 10.6.4 and with iTunes 9.2. It should be reasonably future proof. Unfortunately, I don't know how to script anything on Windows and I have cobbled together what I have found in other places to get this working. I am not an Applescript expert by any means, so improvements are welcome, although it would be good to keep the original script for reference. Your thanks / comments are gratefully received. atv_simplicity at mac dot com. |
==What we're going to do== | ==What we're going to do== | ||
Line 95: | Line 95: | ||
--!!!!YOU MUST CHANGE THE USERNAME HERE TO YOUR OWN USERNAME OR LOCATION OF THE FILE HandBrakeCLI!!!!! | --!!!!YOU MUST CHANGE THE USERNAME HERE TO YOUR OWN USERNAME OR LOCATION OF THE FILE HandBrakeCLI!!!!! | ||
− | do shell script "/Users/ | + | do shell script "/Users/USERNAME/HandBrakeCLI -Z AppleTV -i " & quoted form of inputFile & " -o " & quoted form of outputFile |
--that's created a new file with the ext "tmp" | --that's created a new file with the ext "tmp" | ||
Line 233: | Line 233: | ||
</pre> | </pre> | ||
− | Save the script to the following folder Library > Scripts > Folder Action Scripts. You can now quit Applescript Editor. | + | There are a couple of changes to make to the script for your own system. Firstly, by default avi, mkw and wmv files will be converted. If you want less or more, then you must edit the following lines:- |
+ | |||
+ | <pre> | ||
+ | if (offset of ".avi" in inputFile) - 1 > 0 then set convert to ".avi" | ||
+ | if (offset of ".mkv" in inputFile) - 1 > 0 then set convert to ".mkv" | ||
+ | if (offset of ".wmv" in inputFile) - 1 > 0 then set convert to ".wmv" | ||
+ | </pre> | ||
+ | |||
+ | If you wanted only avi files converted, then the lines would look like this:- | ||
+ | |||
+ | <pre> | ||
+ | if (offset of ".avi" in inputFile) - 1 > 0 then set convert to ".avi" | ||
+ | </pre> | ||
+ | |||
+ | and if you want to add other formats (like mpg, below), do it like this:- | ||
+ | |||
+ | <pre> | ||
+ | do shell script "/Users/'USERNAME'/HandBrakeCLI -Z AppleTV -i " & quoted form of inputFile & " -o " & quoted form of outputFile | ||
+ | </pre> | ||
+ | |||
+ | So that would become something like:- | ||
+ | |||
+ | <pre> | ||
+ | do shell script "/Users/david/HandBrakeCLI -Z AppleTV -i " & quoted form of inputFile & " -o " & quoted form of outputFile | ||
+ | </pre> | ||
+ | |||
+ | Finally, any errors that occur will show messages and stop the script. If you would prefer, you can remove the two lines that display these dialogs, either by deleting them or commenting them out (put two dashes in front of them, like:- | ||
+ | |||
+ | <pre> | ||
+ | -- display dialog "iTunes processing error" & return & epname & ": " & m | ||
+ | </pre> | ||
+ | |||
+ | Save the script as "Converter" to the following folder Library > Scripts > Folder Action Scripts. You can now quit Applescript Editor. | ||
+ | |||
+ | ==Setting Everything Up== | ||
+ | |||
+ | Everything is now in place. We just need to set things running. | ||
+ | |||
+ | Go to your home folder and find the folder called "Automatic" you created earlier. Right click on it and select "Folder Actions Setup". Find the script in the list called "Converter" which we saved in the step above. Click on it once and press "Attach". Make sure the "Enable Folder Actions" checkbox is selected and then you can use the red button to close the window. | ||
==Tidying Up, Extra Touches== | ==Tidying Up, Extra Touches== |
Revision as of 01:38, 22 June 2010
Contents
Automation
Notes
This is a Mac only process and works on Leopard 10.6.4 and with iTunes 9.2. It should be reasonably future proof. Unfortunately, I don't know how to script anything on Windows and I have cobbled together what I have found in other places to get this working. I am not an Applescript expert by any means, so improvements are welcome, although it would be good to keep the original script for reference. Your thanks / comments are gratefully received. atv_simplicity at mac dot com.
What we're going to do
Mac OS X has a standard feature called "Folder Action Scripts". The system will run an Applescript we'll create each time a file is added to the folder. This script will convert the video file to an iTunes friendly, Apple TV format. It will add it to iTunes and do some basic processing of it, too. Although this process uses Applescript, I will explain each part step-by-step, as this is meant to be a beginner's guide.
What we can't do: process HD videos and have them come out as HD videos. This is a limitation of Handbrake. HD videos some out as a middle ground - higher quality than SD, but not full HD. This is correct as of version 0.9.4 of handbrake. Future versions may improve this situation and as long as the preset for Apple TV remains named as "Apple TV" then this will work.
Getting Started
A few little bits to do in advance. If you can't do any of these steps without further instruction, then this guide is a little too advanced for you. :-)
Download the latest Handbrake CLI version from here and place it in your home directory. Everything else you need is already installed.
Create a folder called "Automatic" in your home directory.
Create a playlist called "AutoAdded" in iTunes.
You can change the names and locations of any of the files / playlists above, but if you do, you need to suitably amend the steps / scripts outlined below.
The Script
First and foremost, hardcore Applescript users will simple fall over and die with horror at the mess that is the following script. I am self-taught and cobbled bits from all over the internet. I'm sorry about this. But, I can only say that this works for me and if anyone want to add spit and polish, they should go right ahead!
Open Applescript Editor (Applications --> Utilities). A blank script is in front of you. Add the following code. You can just copy and paste in.
--this tells the mac to run this script on each new item in the folder on adding folder items to theWatchedFolder after receiving theDetectedItems try --we count the new items in the folder set filecount to (count theDetectedItems) --and do this for each item in the folder repeat with i from 1 to filecount set rawfile to item i of theDetectedItems set inputFile to POSIX path of rawfile --by default, set the conversion to not run set convert to "No" --now set it to run, if one of our wanted file types is added --add a line here for each file type you'd like to convert. As is, this will convert all avi, mkv and wmv files and ignore all else if (offset of ".avi" in inputFile) - 1 > 0 then set convert to ".avi" if (offset of ".mkv" in inputFile) - 1 > 0 then set convert to ".mkv" if (offset of ".wmv" in inputFile) - 1 > 0 then set convert to ".wmv" --when files are converted, they will become m4v files, so we need to make sure those are properly handled later if (offset of ".m4v" in inputFile) - 1 > 0 then set convert to ".m4v" --let's check what kind of file we have if convert = "No" then --we want to stop completely, as this isn't a video to convert and it isn't an mp4 to copy to itunes -- this command will stop everything error number -128 else --conversion will proceed, you can have a message pop up here if you like. Remove the two dashes. set extension to convert end if --We need to do something with this file if extension = ".m4v" then --add the file to iTunes, it's been converted tell application "iTunes" add rawfile to playlist "AutoAdded" of source "Library" end tell --this calls the function below, which will process the file in iTunes. See below. ProcessTracks() --we will delete the original file. Obviously you could move it to a folder to keep safe or leave it in place, if you want try tell application "Finder" move rawfile to the trash end tell end try else --we need to convert this file --this sets up the file input name and output name for handbrake CLI set outputFile to text 1 through (offset of extension in inputFile) of inputFile & "tmp" --!!!!YOU MUST CHANGE THE USERNAME HERE TO YOUR OWN USERNAME OR LOCATION OF THE FILE HandBrakeCLI!!!!! do shell script "/Users/USERNAME/HandBrakeCLI -Z AppleTV -i " & quoted form of inputFile & " -o " & quoted form of outputFile --that's created a new file with the ext "tmp" --when done, let's rename it to .m4v, so it can be processed set extensionTmp to (offset of ".tmp" in outputFile) - 1 set doneFile to text 1 through extensionTmp of inputFile & ".m4v" --rename the file do shell script "mv " & quoted form of outputFile & " " & quoted form of doneFile --trash the file --you should only do this if on adding files to iTunes, the file is copied to your library. --Check iTunes Preferences --> Advanced and put a check against "Copy files to iTunes Media folder when adding to library" tell application "Finder" move rawfile to the trash end tell end if end repeat on error m display dialog "Conversion error" & return & inputFile & ": " & m end try end adding folder items to --This is how the tracks are processed in iTunes on ProcessTracks() try tell application "iTunes" set sel to every track of playlist "AutoAdded" of source "Library" repeat with i from 1 to (count sel) tell (item i of sel) set epname to name set startpoint to 1 set endpoint to count (epname) set epname_search to epname -- We're going to look at the file and see if contains SxxExx data, that would indicate a TV Show ripped from a DVD or other source has landed in the folder -- this will find the first instance of .S0, .S1 or .S3 -- this script always assume the format SnnEnn (always two digits!) --anything else won't work. --Applescript gurus, serious apologies in advance. This is a mess and I don't know how to do it better, but it does work. set checkShow to (offset of ".S" in epname_search) - 1 if checkShow = -1 then --this is probably a video of some kind that isn't a TV Show. We'll leave as is. else --process as a TV Show repeat set epname_search to text startpoint through endpoint of epname set theSeasonOffset to offset of ".S" in epname_search set sos to theSeasonOffset + 2 set soe to theSeasonOffset + 2 set checkNo to text sos through soe of epname_search if checkNo is equal to "0" then exit repeat if checkNo is equal to "1" then exit repeat if checkNo is equal to "2" then exit repeat if checkNo is equal to "3" then exit repeat set startpoint to theSeasonOffset + 3 end repeat --This find the season number set theSeasonStart to theSeasonOffset + 2 set theSeasonEnd to theSeasonOffset + 3 set theSeason to text theSeasonStart through theSeasonEnd of epname set theSeasonFull to theSeason if text 1 of theSeason is "0" then set theSeason to text 2 of theSeason --get the ep number set theEpisodeStart to theSeasonOffset + 5 set theEpisodeEnd to theSeasonOffset + 6 set theEpisode to text theEpisodeStart through theEpisodeEnd of epname set theEpisodeFull to theEpisode if text 1 of theEpisode is "0" then set theEpisode to text 2 of theEpisode set eptitle to "Episode " & theEpisode --show name, remove "." and replace with " " set showend to theSeasonOffset - 1 set shownamerough to text 1 through showend of epname set AppleScript's text item delimiters to the "." set the item_list to every text item of shownamerough set AppleScript's text item delimiters to " " set showname to the item_list as string set AppleScript's text item delimiters to {""} --this little line will capitalise the Show Name, because we can't be doing with lower case nopw, can we? ;-) set showname to do shell script "python -c \"import sys; print unicode(sys.argv[1], 'utf8').title().encode('utf8')\" " & quoted form of showname --episide no (episode number) & track number (track number) set episode number to theEpisode set track number to theEpisode --series no (season number) set season number to theSeason --show name set show to showname --episide title set name to eptitle --And it's a TV Show set video kind to TV show end if end tell end repeat --end timeout end tell --Create an error log if something went wrong on error m display dialog "iTunes processing error" & return & epname & ": " & m end try end ProcessTracks
There are a couple of changes to make to the script for your own system. Firstly, by default avi, mkw and wmv files will be converted. If you want less or more, then you must edit the following lines:-
if (offset of ".avi" in inputFile) - 1 > 0 then set convert to ".avi" if (offset of ".mkv" in inputFile) - 1 > 0 then set convert to ".mkv" if (offset of ".wmv" in inputFile) - 1 > 0 then set convert to ".wmv"
If you wanted only avi files converted, then the lines would look like this:-
if (offset of ".avi" in inputFile) - 1 > 0 then set convert to ".avi"
and if you want to add other formats (like mpg, below), do it like this:-
do shell script "/Users/'USERNAME'/HandBrakeCLI -Z AppleTV -i " & quoted form of inputFile & " -o " & quoted form of outputFile
So that would become something like:-
do shell script "/Users/david/HandBrakeCLI -Z AppleTV -i " & quoted form of inputFile & " -o " & quoted form of outputFile
Finally, any errors that occur will show messages and stop the script. If you would prefer, you can remove the two lines that display these dialogs, either by deleting them or commenting them out (put two dashes in front of them, like:-
-- display dialog "iTunes processing error" & return & epname & ": " & m
Save the script as "Converter" to the following folder Library > Scripts > Folder Action Scripts. You can now quit Applescript Editor.
Setting Everything Up
Everything is now in place. We just need to set things running.
Go to your home folder and find the folder called "Automatic" you created earlier. Right click on it and select "Folder Actions Setup". Find the script in the list called "Converter" which we saved in the step above. Click on it once and press "Attach". Make sure the "Enable Folder Actions" checkbox is selected and then you can use the red button to close the window.
Tidying Up, Extra Touches
This is obviously a lengthy process for the computer to run and sometimes things go wrong. You might end up with a file in your "AutoAdded" folder that hasn't been processed.
End (for now)