Created
September 18, 2012 16:38
-
-
Save cwardzala/3744179 to your computer and use it in GitHub Desktop.
EyeTV applescripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
property HANDBRAKE_CLI : "HandBrakeCLI" | |
property HANDBRAKE_PARAMETERS : " -f mp4 --preset=\"AppleTV 2\" " | |
property TARGET_PATH : "/Shared Media/TV Shows/" | |
property TARGET_TYPE : ".mp4" | |
property SOURCE_TYPE : ".mpg" | |
property SHELL_SCRIPT_SUFFIX : " > /Shared\\ Media/TV\\ Shows/EyeTV\\ Archive/HandBrakeCLI-export.log 2>&1 " | |
-- Properties for clean_filename() routine: | |
-- Set the list of characters you want to replace | |
-- DisallowedChars will be replaced with the replacementChar | |
property CLEAN_FILENAME_DISALLOWED_CHARS : ";/|!@#$%^&*()+" | |
-- Anything in disallowedChars2 will be removed altogether | |
property CLEAN_FILENAME_DISALLOWED_CHARS2 : ":,'" | |
-- Set the character you'd like to use to replace the invalid | |
-- characters specified in disallowedChars | |
property CLEAN_FILENAME_REPLACEMENT : "_" | |
on run argv | |
set recordingID to item 1 of argv | |
-- Obtain some show information from EyeTV | |
-- Transcode recorded video to conform to desired format | |
-- Delete original EyeTV recording | |
with timeout of (480 * 60) seconds | |
tell application "EyeTV" | |
set myid to recordingID as integer | |
set show_title to title of recording id myid as text | |
set show_description to description of recording id myid as text | |
-- create the show's folder | |
set show_folder to (TARGET_PATH & show_title) as string | |
my create_show_folder(show_folder) | |
-- create the show's log file | |
set log_file to (my create_show_log(show_folder)) | |
-- format episode as mmddyyyy if no episode name is available | |
set show_episode to (my format_episode(episode of recording id myid as text, start time of recording id myid)) --episode of recording id myid as text | |
-- get the original file for transcoding | |
set input_file to my recording_path((location of recording id myid as text)) | |
set show_filename to (my clean_filename(show_title & " - " & show_episode) & TARGET_TYPE) | |
set transcoded_file to (show_folder & "/" & show_filename) as string | |
-- build Handbrake CLI command | |
set cmd to my format_hbcli(input_file, transcoded_file, log_file) | |
--return cmd | |
do shell script "echo \"" & cmd & "\" > /Shared\\ Media/TV\\ Shows/EyeTV\\ Archive/HandBrakeCLI-command.txt " | |
do shell script cmd | |
delete recording id myid | |
end tell | |
end timeout | |
tell application "iTunes" | |
set the_file to (":Shared Media:TV Shows:" & show_folder & ":" & show_filename) | |
set newShow to (add file the_file) | |
set video kind of newShow to TV show | |
set show of newShow to show_title | |
set episode ID of newShow to show_episode | |
set description of newShow to show_description | |
end tell | |
end run | |
on format_hbcli(in_file, out_file, log_file) | |
return HANDBRAKE_CLI & " -i " & quoted form of in_file & " -o " & quoted form of out_file & HANDBRAKE_PARAMETERS & " > " & quoted form of log_file & " 2>&1" | |
end format_hbcli | |
on recording_path(loc) | |
set AppleScript's text item delimiters to "." | |
set rp to text items 1 through -2 of loc as string | |
set AppleScript's text item delimiters to "" | |
return (POSIX path of rp) & SOURCE_TYPE as string | |
end recording_path | |
on format_episode(ep, dat) | |
if ep = "" then | |
set rdate to dat | |
set rday to my format_num((day of rdate as string)) | |
set rmonth to my format_num((month of rdate as integer as string)) | |
set ryear to year of rdate as string | |
set ep to ((rmonth as string) & rday & ryear) as text | |
end if | |
return ep | |
end format_episode | |
on format_num(num) | |
if length of num is 1 then set num to "0" & num | |
return num | |
end format_num | |
on create_show_log(fldr) | |
set lf to fldr & "/HandBrakeCLI-export.log" | |
do shell script "touch " & quoted form of lf | |
return lf | |
end create_show_log | |
on create_show_folder(fldr) | |
tell application "System Events" | |
try | |
do shell script "mkdir " & quoted form of fldr | |
end try | |
end tell | |
end create_show_folder | |
on clean_filename(theName) | |
set newName to "" | |
repeat with i from 1 to length of theName | |
-- check if the character is in CLEAN_FILENAME_DISALLOWED_CHARS | |
-- replace it with the CLEAN_FILENAME_REPLACEMENT if it is | |
if ((character i of theName) is in CLEAN_FILENAME_DISALLOWED_CHARS) then | |
set newName to newName & CLEAN_FILENAME_REPLACEMENT | |
-- check if the character is in CLEAN_FILENAME_DISALLOWED_CHARS2 | |
-- remove it completely if it is | |
else if ((character i of theName) is in CLEAN_FILENAME_DISALLOWED_CHARS2) then | |
set newName to newName & "" | |
-- if the character is not in either CLEAN_FILENAME_DISALLOWED_CHARS or | |
-- CLEAN_FILENAME_DISALLOWED_CHARS2, keep it in the file name | |
else | |
set newName to newName & character i of theName | |
end if | |
end repeat | |
return newName | |
end clean_filename |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on RecordingDone(recordingID) | |
do shell script "echo \"recordingID = " & recordingID & "\" > /Shared\\ Media/TV\\ Shows/EyeTV\\ Archive/osascript.log" | |
do shell script "nohup osascript /Library/Application\\ Support/EyeTV/Scripts/TriggeredScripts/RecordingDone-called.scpt " & recordingID & " >> /Shared\\ Media/TV\\ Shows/EyeTV\\ Archive/osascript.log 2>&1 &" | |
end RecordingDone | |
on run argv | |
set myItem to item 1 of argv as integer | |
tell application "EyeTV" | |
set rec to unique ID of item myItem of recordings | |
end tell | |
my RecordingDone(rec) | |
end run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment