Welcome to our forums! Please take a few moments to read through our Community Guidelines (also conveniently linked in the header at the top of each page). There, you'll find guidelines on conduct, tips on getting the help you may be searching for, and more!

Eyetv export with Season and Episode applescript

hrcolb0hrcolb0 Posts: 1,239Members ✭✭
Here is a RecordingDone.scpt that appends the export file with the Season and Episode number if it is available in the EPG info that Eyetv downloads.

-You do not need to worry about special characters
-You don't have to install any other programs or extra scripts. Just copy this file to \Library\Application Support\EyeTV\Scripts\TriggeredScripts folder
-Script can easily be modified to include much more metadata than EyeTV provides in its dictionary
-No internet scraping! Uses data already captured by EyeTV!
-Combine this script with folder actions to sort exported shows into your Plex Tv Shows source share
-Can be used as an example script to mine epg info for importing into iTunes library

I looked for easy solutions to do this and did not find any on the net. I hate manually editing the file names, and then sorting them into the correct folders, so much so that most of my recorded shows aren't even in my library.

That's the old way though. Welcome to a new tighter EyeTV recordings integration with Plex. Recorded shows populating the Plex library in 30 -60 minutes after the recording is done, and you didn't have to log into your computer!
«13456

Comments

  • jadaiberjadaiber Posts: 1Members
    This is almost exactly what I am looking for. The only addition I'd like is for the original recording to be deleted to save disk space. I am an extreme novice with AppleScripts. Can you be so kind to explain how I could change the script to accomplish this. Thanks.
  • adriandbadriandb Posts: 946Members, Plex Pass Plex Pass
    What's your solution to exporting without commercials? Comskipper?
  • hrcolb0hrcolb0 Posts: 1,239Members ✭✭
    adriandb wrote on 10 August 2010 - 12:36 PM:

    What's your solution to exporting without commercials? Comskipper?



    Sorry it took so long, and don,t know if anyone is still reading this, but...

    I can't take out commercials until compacting becomes apple scriptable.
  • numbnu7snumbnu7s Posts: 27Members, Plex Pass Plex Pass
    I "really" wanted this script as I watch my shows using Plex and like having the season and episode number as part of the file name for Plex parsing. However, it didn't work for me :(

    So... i've spent some time debugging this and documenting it for my purposes. It works fine for my purposes but there is not a lot of error handling (i.e. if directories are missing, duplicate files exist, etc). I'm also gonna spend some time getting this to export in different formats.

    Here's my updated version, i hope it works for you.


    property fname : "Macintosh HD:" as string
    
    
    on RecordingDone(recordingID)
    	
    	-- Customize this script for yourself. The next three variables are locations on your system where we will read and export to
    	
    	-- Set this value to the location where EyeTV records to
    	set EyeTvArchiveLocation to "Macintosh HD:Users:dad:Documents:EyeTV Archive:" as string
    	
    	-- Set this location to where you want your exports to write to
    	set ExportLocation to "Macintosh HD:Users:dad:Documents:EyeTV Archive:" as string
    	
    	-- Set this to a temporary location. A file "temp.plist" is written and then deleted from here
    	set TempLocation to "Macintosh HD:Users:dad:Desktop:" as string
    	
    	
    	-- Grab some basics from EyeTV. RecoringID, Show name, Episode name. It'll be used later.
    	tell application "EyeTV"
    		
    		set rec to recordingID as integer
    		set theRec to recording id rec
    		set thisTitle to title of theRec
    		set thisEpisode to episode of theRec
    		
    	end tell
    	
    	
    	-- Now we're gonna go to the finder, find the show and grab the file out of the package that has the meta data. It'll be written to a temp file and we'll grab the season and episode # from it.
    	tell application "Finder"
    		
    		-- Set the alias for the show we just got done recording
    		if thisEpisode is "" then
    			set fpath to EyeTvArchiveLocation & thisTitle & ".eyetv" as alias
    		else
    			set fpath to EyeTvArchiveLocation & thisTitle & " - " & thisEpisode & ".eyetv" as alias
    		end if
    		
    		-- The .eyetv file is a package. We're going to iterate through it's contents and find the .eyetvp file. It'll be stored in the fname variable
    		try
    			set eyetv_archive to every file of the file fpath
    			repeat with thisFile in eyetv_archive
    				if the name of thisFile ends with "eyetvp" then
    					set fname to thisFile as string
    					exit repeat
    				end if
    			end repeat
    		on error error_message number error_number
    			display alert ("YIKES! Something's wrong!") message error_message & (" Error number ") & error_number & "."
    		end try
    	end tell
    	
    	-- Now we're gonna write the contents of the .eyetvp file to a temp file. the "readFile" sub is referenced at the bottom of this script
    	set eyetvp to readFile(fname)
    	set filepath2 to TempLocation & "test.plist"
    	set theFileReference to open for access (filepath2) as string with write permission
    	
    	write eyetvp to theFileReference
    	close access theFileReference
    	
    	-- Great! no grab Season id and episode num out of this temp file and create the sedat variable. This will be used in the file export later.
    	-- You can grab more meta data in this routine and also tweak the way the file name meta data is created.
    	tell application "System Events"
    		set the plistfile_path to ((filepath2))
    		filepath2 as text
    		set fileRecord to (value of property list file plistfile_path)
    		set sid to value of property list item "SEASONID" of property list item "epg info" of property list file plistfile_path
    		set eid to value of property list item "EPISODENUM" of property list item "epg info" of property list file plistfile_path
    		
    		if sid is not "" and eid is not "" then
    			set sedat to "S" & sid & "E" & eid
    		else
    			set sedat to "S00E00"
    		end if
    	end tell
    	
    	-- Now delete the temp file we created above
    	set filetodelete to filepath2 as alias
    	tell application "Finder"
    		delete filetodelete
    	end tell
    	
    	-- Now, tell EyeTV to set the file name and location and export the file.
    	-- TODO: I'd like to have an option to just rename the file in place here and/or export using Turbo.264 and/or export in HD720p format.
    	tell application "EyeTV"
    		set theDateStamp to (do shell script "date +%Y%m%d.%H%M%S")
    		set fileName to ExportLocation & thisTitle & " - " & sedat & ".avi"
    		export from theRec to file fileName as iPad
    	end tell
    	
    	--Voila!
    end RecordingDone
    
    -- This subroutine is used in reading the eyetvp file in the package.
    on readFile(unixPath)
    	set foo to (open for access (unixPath))
    	set txt to (read foo for (get eof foo))
    	close access foo
    	return txt
    end readFile
    
  • hrcolb0hrcolb0 Posts: 1,239Members ✭✭
    Also, for some reason, compacting has now become part of the export process automatically. As I run my batch script at the end of every day, commercial markings are honored and clipped out.
  • numbnu7snumbnu7s Posts: 27Members, Plex Pass Plex Pass

    Also, for some reason, compacting has now become part of the export process automatically. As I run my batch script at the end of every day, commercial markings are honored and clipped out.


    Oooh.. daily batch. Never thought of that, but will now.

    I'm installing and testing ETVcomskip right now. So far so good. I have another question for ya... My desired workflow is as follows:

    1. Record (duh, done)
    2. Rename with Season and Episode #'s (done)
    3. Mark commercials (Working on it)
    4. Export to HD720p to drive:show name:season number folder (doing manually now)
    5. Move .eyetv to backup location (need help here)

    I have an ExportDone script to move the .eyetv file to a backup location but it remains in the eyetv recording pane until I restart the application. Any thoughts on how to remove it from the recording pane? (guess, i could restart nightly). I want to move it rather than delete it until I'm 100% confident my workflow is working right. Then I'll change it to delete.
  • hrcolb0hrcolb0 Posts: 1,239Members ✭✭
    Here's my batch script. It is saved as an app so it runs when double clicked. It works thusly:

    1. Record
    3. Comskip Recording done script runs once the recording is done automagically.
    2. At the end of the night, because it is still a resource hog ( I need my facebook bejewelled blitz y'all) I select the recordings I want to convert.
    4. Then I go to the EyeTV scripts menu and select it.
    5. In the morning I run a folder script to put the converted files in the correct folders in my Plex Library, which scans automatically based on the Season and Ep numbers.

    To modify the script attached, right click and show package contents. Navigate to main.scpt to make changes.
  • thiloroethiloroe Posts: 6Members
    @ numbnu7s

    I customized the file paths and installed the script. However it only works if I manually choose export to iPad. Am I doing something wrong?

    Also, do you know how to put the published year info into the file name? I couldn't find this in the eyeTV Apple script function list.

    t.lo
  • hrcolb0hrcolb0 Posts: 1,239Members ✭✭
    This export applescript uses the plist file yo grab info for the export file. "Show contents" of the .eyetv file and you should find it.
  • Michael4824Michael4824 Posts: 34Members, Plex Pass ✭✭
    I was wondering if anyone could help me with my code. I use Comskipper, so I had to combine it with this code. (numbnu7s's version, as the comments helped me understand what was going on.)

    Anyways, I want my TV show recordings copied or moved to my external drive, with the following path: My Book:TV Shows:(TV Series):Season (X):(Episode Number) (Episode Name)
    (Anything in parenthesis should be replaced with the appropriate information.)

    Here is my hacked together code. Basically, the folders are created if they don't exist by the code (So, if I record "Everybody Loves Ramond" for the first time, the series folder, as well as season folder will be generated). However, the episode file never appears. When I pull up EyeTV, it's stuck on Waiting. (Between Recording and Exporting). So, for whatever reason, the code is breaking. Any help? (Sorry, I've never used Applescript before, I've only really had high school classes on JAVA, and a few college courses on RESOLVE. I'm trying to find my way with this by relying on Google.)

    How this code is setup: The very first bit is for commskipper, then I move into numbnu7's code. You can see my small edit about creating folders, which does work. Here's the exporting line:
    set fileName to ExportLocation & thisTitle & ":Season " & sid & ":" & eid & " " & thisEpisode & ".avi"
    


    I did make a point of putting a comment where all the information below it is basically useless, as it only involves commskipper.



    -- Run the python MarkCommercials script for the given recording
    -- this must be run with the RecordingStarted script
    -- it will check if there were multiple PIDs for the recording and runs MarkCommercials for each pid
    -- requires updated MarkCommercials which allows specifying the pid
    -- by Ben Blake, September 2009
    
    global LogMsg
    
    on RecordingDone(recordingID)
    	set LogMsg to ""
    	CheckMultiplePIDs(recordingID)
    	
    	--disable this if you do not want a logfile written
    	if (count of LogMsg) > 0 then
    		write_to_file((short date string of (current date) & " " & time string of (current date)) & LogMsg & (ASCII character 13), (path to "logs" as string) & "EyeTV scripts.log", true)
    	end if
    	
    	
    	
    	--Export to Plex! 	
    	
    	-- Grab some basics from EyeTV. RecoringID, Show name, Episode name. It'll be used later.
    	tell application "EyeTV"
    		
    		set rec to recordingID as integer
    		set theRec to recording id rec
    		set thisTitle to title of theRec
    		set thisEpisode to episode of theRec
    		
    	end tell
    	
    	-- Customize this script for yourself. The next three variables are locations on your system where we will read and export to
    	
    	-- Set this value to the location where EyeTV records to
    	set EyeTvArchiveLocation to "My Book:EyeTV:EyeTV Archive:" as string
    	
    	-- Set this location to where you want your exports to write to
    	set ExportLocation to "My Book:TV Shows:" as string
    	
    	-- Set this to a temporary location. A file "temp.plist" is written and then deleted from here
    	set TempLocation to "Macintosh HD:Users:Michael:Documents:" as string
    	
    	-- Now we're gonna go to the finder, find the show and grab the file out of the package that has the meta data. It'll be written to a temp file and we'll grab the season and episode # from it.
    	tell application "Finder"
    		
    		-- Set the alias for the show we just got done recording
    		if thisEpisode is "" then
    			set fpath to EyeTvArchiveLocation & thisTitle & ".eyetv" as alias
    		else
    			set fpath to EyeTvArchiveLocation & thisTitle & " - " & thisEpisode & ".eyetv" as alias
    		end if
    		
    		-- The .eyetv file is a package. We're going to iterate through it's contents and find the .eyetvp file. It'll be stored in the fname variable
    		try
    			set eyetv_archive to every file of the file fpath
    			repeat with thisFile in eyetv_archive
    				if the name of thisFile ends with "eyetvp" then
    					set fname to thisFile as string
    					exit repeat
    				end if
    			end repeat
    		on error error_message number error_number
    			display alert ("YIKES! Something's wrong!") message error_message & (" Error number ") & error_number & "."
    		end try
    	end tell
    	
    	-- Now we're gonna write the contents of the .eyetvp file to a temp file. the "readFile" sub is referenced at the bottom of this script
    	set eyetvp to readFile(fname)
    	set filepath2 to TempLocation & "test.plist"
    	set theFileReference to open for access (filepath2) as string with write permission
    	
    	write eyetvp to theFileReference
    	close access theFileReference
    	
    	-- Great! no grab Season id and episode num out of this temp file and create the sedat variable. This will be used in the file export later.
    	-- You can grab more meta data in this routine and also tweak the way the file name meta data is created.
    	tell application "System Events"
    		set the plistfile_path to ((filepath2))
    		filepath2 as text
    		set fileRecord to (value of property list file plistfile_path)
    		set sid to value of property list item "SEASONID" of property list item "epg info" of property list file plistfile_path
    		set eid to value of property list item "EPISODENUM" of property list item "epg info" of property list file plistfile_path
    		
    		if sid is not "" and eid is not "" then
    			set sedat to "S" & sid & "E" & eid
    		else
    			set sedat to "S00E00"
    		end if
    	end tell
    	
    	-- Now delete the temp file we created above
    	set filetodelete to filepath2 as alias
    	tell application "Finder"
    		delete filetodelete
    		
    		set fileLocation to ExportLocation & thisTitle
    		if (exists folder fileLocation) is false then
    			make new folder at alias ExportLocation with properties {name:thisTitle}
    		end if
    		
    		set fileLocation to ExportLocation & thisTitle & ":" & "Season " & sid
    		set tempDir to ExportLocation & thisTitle
    		set tempFolder to "Season " & sid
    		if (exists folder fileLocation) is false then
    			make new folder at alias tempDir with properties {name:tempFolder}
    		end if
    	end tell
    	
    	-- Now, tell EyeTV to set the file name and location and export the file.
    	-- TODO: I'd like to have an option to just rename the file in place here and/or export using Turbo.264 and/or export in HD720p format.
    	tell application "EyeTV"
    		set theDateStamp to (do shell script "date +%Y%m%d.%H%M%S")
    		--set fileName to ExportLocation & thisTitle & " - " & sedat & ".avi"
    		set fileName to ExportLocation & thisTitle & ":Season " & sid & ":" & eid & " " & thisEpisode & ".avi"
    		export from theRec to file fileName as iPad
    	end tell
    	
    	--Voila!
    end RecordingDone
    
    -- one more edit for plex export, only section below.
    on readFile(unixPath)
    	set foo to (open for access (unixPath))
    	set txt to (read foo for (get eof foo))
    	close access foo
    	return txt
    end readFile
    
    
    
    
    
    
    --Everything below is for commskipper only!
    
    
    
    -- testing code: this will not be called when triggered from EyeTV, but only when the script is run as a stand-alone script
    on run
    	tell application "EyeTV"
    		set rec to unique ID of item 1 of every recording
    		
    		my RecordingDone(rec)
    	end tell
    end run
    
    on CheckMultiplePIDs(recordingID)
    	--check if there are multiple Video PIDs in the file
    	
    	tell application "EyeTV"
    		set input_text to my read_from_file((path to "logs" as string) & "ETVComskip" & ":" & recordingID & "_comskip.log")
    		if (count of (input_text as string)) > 0 then
    			set logdata to every paragraph of input_text
    			set logdata_lastrow to (item ((count of logdata) - 1) of logdata) as string
    			
    			if (items 1 thru 19 of logdata_lastrow) as string = "Video PID not found" then
    				--multiple Video PIDs, rerun MarkCommercials until successful
    				
    				set recrdingIDInteger to recordingID as integer
    				set rec to recording id recrdingIDInteger
    				set LogMsg to "RecordingDone found multiple PIDs for recording ID: " & recordingID & ", Channel " & (channel number of rec) & " - " & (title of rec)
    				
    				set PIDs to (items 44 thru ((count of logdata_lastrow) - 2) of logdata_lastrow) as string
    				set delims to AppleScript's text item delimiters
    				set AppleScript's text item delimiters to ", "
    				set PID_List to {}
    				set PID_List to every word of PIDs
    				set AppleScript's text item delimiters to delims
    				
    				repeat with pid in PID_List
    					my launchComSkip(recordingID, pid)
    					repeat while (my mcIsRunning())
    						delay 5
    					end repeat
    				end repeat
    				
    			end if
    		end if
    	end tell
    end CheckMultiplePIDs
    
    on read_from_file(target_file)
    	--return the contents of the given file
    	set fileRef to (open for access (target_file))
    	set txt to (read fileRef for (get eof fileRef) as «class utf8»)
    	close access fileRef
    	return txt
    end read_from_file
    
    on write_to_file(this_data, target_file, append_data)
    	--from http://www.apple.com/applescript/sbrt/sbrt-09.html
    	try
    		set the target_file to the target_file as string
    		set the open_target_file to open for access file target_file with write permission
    		if append_data is false then set eof of the open_target_file to 0
    		write this_data to the open_target_file starting at eof
    		close access the open_target_file
    		return true
    	on error
    		try
    			close access file target_file
    		end try
    		return false
    	end try
    end write_to_file
    
    on launchComSkip(recID, pid)
    	if pid = "" then
    		set cmd to "'/Library/Application Support/ETVComskip/MarkCommercials.app/Contents/MacOS/MarkCommercials' --force --log " & recID & " &> /dev/null &"
    	else
    		set cmd to "'/Library/Application Support/ETVComskip/MarkCommercials.app/Contents/MacOS/MarkCommercials' --force --log " & recID & " --pid=" & pid & " &> /dev/null &"
    	end if
    	
    	do shell script cmd
    end launchComSkip
    
    on mcIsRunning()
    	set processPaths to do shell script "ps -xww | awk -F/ 'NF >2' | awk -F/ '{print $NF}' | awk -F '-' '{print $1}' "
    	return (processPaths contains "MarkCommercials")
    end mcIsRunning
    



    Thanks for any help!
    -Michael
  • numbnu7snumbnu7s Posts: 27Members, Plex Pass Plex Pass
    t.lo wrote:

    @ numbnu7s

    I customized the file paths and installed the script. However it only works if I manually choose export to iPad. Am I doing something wrong?

    Also, do you know how to put the published year info into the file name? I couldn't find this in the eyeTV Apple script function list.

    t.lo


    Sorry, I commented out the export code. I'm doing export manually after I check to make sure ETVcomskip was accurate. I'll probable fine tune it later for my needs.

    As for Year, Harley's right. Grab it from the test.plist.. specifically create a variable for it (yearpublished) and grab the property... something like this:

    set yearpublished to value of property list item "YEAR" of property list item "epg info" of property list file plistfile_path
    
  • numbnu7snumbnu7s Posts: 27Members, Plex Pass Plex Pass
    Hendrick wrote:

    I was wondering if anyone could help me with my code. I use Comskipper, so I had to combine it with this code. (numbnu7s's version, as the comments helped me understand what was going on.)

    Anyways, I want my TV show recordings copied or moved to my external drive, with the following path: My Book:TV Shows:(TV Series):Season (X):(Episode Number) (Episode Name)
    (Anything in parenthesis should be replaced with the appropriate information.)

    Here is my hacked together code. Basically, the folders are created if they don't exist by the code (So, if I record "Everybody Loves Ramond" for the first time, the series folder, as well as season folder will be generated). However, the episode file never appears. When I pull up EyeTV, it's stuck on Waiting. (Between Recording and Exporting). So, for whatever reason, the code is breaking. Any help? (Sorry, I've never used Applescript before, I've only really had high school classes on JAVA, and a few college courses on RESOLVE. I'm trying to find my way with this by relying on Google.)

    How this code is setup: The very first bit is for commskipper, then I move into numbnu7's code. You can see my small edit about creating folders, which does work. Here's the exporting line:
    set fileName to ExportLocation & thisTitle & ":Season " & sid & ":" & eid & " " & thisEpisode & ".avi"
    


    I did make a point of putting a comment where all the information below it is basically useless, as it only involves commskipper.




    Well, I too am new to Applescript but I've found that it's pretty finicky when it comes to building strings of different type variables. Try casting thisEpisode to string. Better yet, build the string and do a display dialog command with the string so you can debug it.

    Good luck and let me know what you find as I'll be doing the same thing soon.
  • Michael4824Michael4824 Posts: 34Members, Plex Pass ✭✭
    edited January 2011
    I think you are on to something about thisEpisode. I recorded the Colbert Report last night, and it actually saved. But with one kicker... the file name was ".avi". No Episode name. While looking at EyeTV, there wasn't an episode name listed with it. So, for whatever reason, the thisEpisode is breaking it. I'll try casting it as a string, and post back once I record something. (Give me a half hour.)

    Here's the line of code I added, and it's placement. I'm recording a show now, I'll let you know how it exports. (half hour show.)

    tell application "EyeTV"
    		set theDateStamp to (do shell script "date +%Y%m%d.%H%M%S")
    		--set fileName to ExportLocation & thisTitle & " - " & sedat & ".avi"
    		set tempEp to thisEpisode as string
    		if eid is not "" then
    			set tempEid to eid as integer
    		else
    			set tempEid to 0 as integer
    		end if
    		set fileName to ExportLocation & thisTitle & ":Season " & sid & ":" & tempEid & " " & tempEp & ".avi"
    		export from theRec to file fileName as iPad
    	end tell
    
  • Michael4824Michael4824 Posts: 34Members, Plex Pass ✭✭
    edited January 2011
    Well, the folders appear, and the file appeared with the proper naming. However, it's only 21 megs, and it won't play. I'm going to try recording another show, and see what happens.

    EDIT: Well, I lied. I checked back on the file after a couple of minutes, and saw the file size had grown significantly, but still wasn't playable. That's when I realized the file was still exporting! So, it now works! I let the whole file copy, and it played! Thanks so much for the help!

    I'm doing one more edit (to correct The Colbert Report, where they don't have a season name), so it'll export with the date. If this works, I'll post the script here, so anyone in my situation can use it.
  • Michael4824Michael4824 Posts: 34Members, Plex Pass ✭✭
    Alright, I think I have a final solution. Creates an series folder, creates a season folder if needed (as long as there is a season id). Episode will be names in the format with the episode id first, followed by episode name, in mp4 format. If there isn't an episode name (Colbert Report/SportsCenter), it'll be named with the date. Uses commskipper.

    Thanks for the help, and the initial script to modify!

    (If you do use this, make sure you edit the folder names up top.)

    -- Run the python MarkCommercials script for the given recording
    -- this must be run with the RecordingStarted script
    -- it will check if there were multiple PIDs for the recording and runs MarkCommercials for each pid
    -- requires updated MarkCommercials which allows specifying the pid
    -- by Ben Blake, September 2009
    
    global LogMsg
    
    on RecordingDone(recordingID)
    	set LogMsg to ""
    	CheckMultiplePIDs(recordingID)
    	
    	--disable this if you do not want a logfile written
    	if (count of LogMsg) > 0 then
    		write_to_file((short date string of (current date) & " " & time string of (current date)) & LogMsg & (ASCII character 13), (path to "logs" as string) & "EyeTV scripts.log", true)
    	end if
    	
    	
    	
    	--Export to Plex! 	
    	
    	-- Grab some basics from EyeTV. RecoringID, Show name, Episode name. It'll be used later.
    	tell application "EyeTV"
    		
    		set rec to recordingID as integer
    		set theRec to recording id rec
    		set thisTitle to title of theRec
    		set thisEpisode to episode of theRec
    		
    	end tell
    	
    	-- Customize this script for yourself. The next three variables are locations on your system where we will read and export to
    	
    	-- Set this value to the location where EyeTV records to
    	set EyeTvArchiveLocation to "My Book:EyeTV:EyeTV Archive:" as string
    	
    	-- Set this location to where you want your exports to write to
    	set ExportLocation to "My Book:TV Shows:" as string
    	
    	-- Set this to a temporary location. A file "temp.plist" is written and then deleted from here
    	set TempLocation to "Macintosh HD:Users:Michael:Documents:" as string
    	
    	-- Now we're gonna go to the finder, find the show and grab the file out of the package that has the meta data. It'll be written to a temp file and we'll grab the season and episode # from it.
    	tell application "Finder"
    		
    		-- Set the alias for the show we just got done recording
    		if thisEpisode is "" then
    			set fpath to EyeTvArchiveLocation & thisTitle & ".eyetv" as alias
    		else
    			set fpath to EyeTvArchiveLocation & thisTitle & " - " & thisEpisode & ".eyetv" as alias
    		end if
    		
    		-- The .eyetv file is a package. We're going to iterate through it's contents and find the .eyetvp file. It'll be stored in the fname variable
    		try
    			set eyetv_archive to every file of the file fpath
    			repeat with thisFile in eyetv_archive
    				if the name of thisFile ends with "eyetvp" then
    					set fname to thisFile as string
    					exit repeat
    				end if
    			end repeat
    		on error error_message number error_number
    			display alert ("YIKES! Something's wrong!") message error_message & (" Error number ") & error_number & "."
    		end try
    	end tell
    	
    	-- Now we're gonna write the contents of the .eyetvp file to a temp file. the "readFile" sub is referenced at the bottom of this script
    	set eyetvp to readFile(fname)
    	set filepath2 to TempLocation & "test.plist"
    	set theFileReference to open for access (filepath2) as string with write permission
    	
    	write eyetvp to theFileReference
    	close access theFileReference
    	
    	-- Great! no grab Season id and episode num out of this temp file and create the sedat variable. This will be used in the file export later.
    	-- You can grab more meta data in this routine and also tweak the way the file name meta data is created.
    	tell application "System Events"
    		set the plistfile_path to ((filepath2))
    		filepath2 as text
    		set fileRecord to (value of property list file plistfile_path)
    		set sid to value of property list item "SEASONID" of property list item "epg info" of property list file plistfile_path
    		set eid to value of property list item "EPISODENUM" of property list item "epg info" of property list file plistfile_path
    		
    		if sid is not "" and eid is not "" then
    			set sedat to "S" & sid & "E" & eid
    		else
    			set sedat to "S00E00"
    		end if
    	end tell
    	
    	-- Now delete the temp file we created above
    	set filetodelete to filepath2 as alias
    	tell application "Finder"
    		delete filetodelete
    		
    		set fileLocation to ExportLocation & thisTitle
    		if (exists folder fileLocation) is false then
    			make new folder at alias ExportLocation with properties {name:thisTitle}
    		end if
    		
    		set fileLocation to ExportLocation & thisTitle & ":" & "Season " & sid
    		set tempDir to ExportLocation & thisTitle
    		set tempFolder to "Season " & sid
    		set tempSid to sid as string
    		if (exists folder fileLocation) is false and tempSid is not "" and tempSid is not "0" then
    			make new folder at alias tempDir with properties {name:tempFolder}
    		end if
    	end tell
    	
    	-- Now, tell EyeTV to set the file name and location and export the file.
    	-- TODO: I'd like to have an option to just rename the file in place here and/or export using Turbo.264 and/or export in HD720p format.
    	tell application "EyeTV"
    		set theDateStamp to (do shell script "date +%Y%m%d.%H%M%S")
    		--set fileName to ExportLocation & thisTitle & " - " & sedat & ".avi"
    		set tempEp to thisEpisode as string
    		if eid is not "" then
    			set tempEid to eid as integer
    		else
    			set tempEid to 0 as integer
    		end if
    		
    		set tempSeason to sid as string
    		
    		if tempEp is not "" and tempSeason is not "" and tempSeason is not "0" and tempEid is not "0" and tempEid is not "" then
    			set fileName to ExportLocation & thisTitle & ":Season " & sid & ":" & tempEid & " " & tempEp & ".mp4"
    		else if tempEp is not "" then
    			set fileName to ExportLocation & thisTitle & ":" & tempEid & " " & tempEp & ".mp4"
    		else if tempSeason is not "" and tempSeason is not "0" then
    			--have season number, use Date
    			set tempEpDate to (do shell script "date +%m%d%Y") as string
    			set fileName to ExportLocation & thisTitle & ":Season " & sid & ":" & tempEpDate & ".mp4"
    		else
    			--just use date
    			set tempEpDate to (do shell script "date +%m%d%Y") as string
    			set fileName to ExportLocation & thisTitle & ":" & tempEpDate & ".mp4"
    		end if
    		
    		--display dialog fileName
    		export from theRec to file fileName as iPad
    	end tell
    	
    	--Voila!
    end RecordingDone
    
    -- one more edit for plex export, only section below.
    on readFile(unixPath)
    	set foo to (open for access (unixPath))
    	set txt to (read foo for (get eof foo))
    	close access foo
    	return txt
    end readFile
    
    -- testing code: this will not be called when triggered from EyeTV, but only when the script is run as a stand-alone script
    on run
    	tell application "EyeTV"
    		set rec to unique ID of item 1 of every recording
    		
    		my RecordingDone(rec)
    	end tell
    end run
    
    on CheckMultiplePIDs(recordingID)
    	--check if there are multiple Video PIDs in the file
    	
    	tell application "EyeTV"
    		set input_text to my read_from_file((path to "logs" as string) & "ETVComskip" & ":" & recordingID & "_comskip.log")
    		if (count of (input_text as string)) > 0 then
    			set logdata to every paragraph of input_text
    			set logdata_lastrow to (item ((count of logdata) - 1) of logdata) as string
    			
    			if (items 1 thru 19 of logdata_lastrow) as string = "Video PID not found" then
    				--multiple Video PIDs, rerun MarkCommercials until successful
    				
    				set recrdingIDInteger to recordingID as integer
    				set rec to recording id recrdingIDInteger
    				set LogMsg to "RecordingDone found multiple PIDs for recording ID: " & recordingID & ", Channel " & (channel number of rec) & " - " & (title of rec)
    				
    				set PIDs to (items 44 thru ((count of logdata_lastrow) - 2) of logdata_lastrow) as string
    				set delims to AppleScript's text item delimiters
    				set AppleScript's text item delimiters to ", "
    				set PID_List to {}
    				set PID_List to every word of PIDs
    				set AppleScript's text item delimiters to delims
    				
    				repeat with pid in PID_List
    					my launchComSkip(recordingID, pid)
    					repeat while (my mcIsRunning())
    						delay 5
    					end repeat
    				end repeat
    				
    			end if
    		end if
    	end tell
    end CheckMultiplePIDs
    
    on read_from_file(target_file)
    	--return the contents of the given file
    	set fileRef to (open for access (target_file))
    	set txt to (read fileRef for (get eof fileRef) as «class utf8»)
    	close access fileRef
    	return txt
    end read_from_file
    
    on write_to_file(this_data, target_file, append_data)
    	--from http://www.apple.com/applescript/sbrt/sbrt-09.html
    	try
    		set the target_file to the target_file as string
    		set the open_target_file to open for access file target_file with write permission
    		if append_data is false then set eof of the open_target_file to 0
    		write this_data to the open_target_file starting at eof
    		close access the open_target_file
    		return true
    	on error
    		try
    			close access file target_file
    		end try
    		return false
    	end try
    end write_to_file
    
    on launchComSkip(recID, pid)
    	if pid = "" then
    		set cmd to "'/Library/Application Support/ETVComskip/MarkCommercials.app/Contents/MacOS/MarkCommercials' --force --log " & recID & " &> /dev/null &"
    	else
    		set cmd to "'/Library/Application Support/ETVComskip/MarkCommercials.app/Contents/MacOS/MarkCommercials' --force --log " & recID & " --pid=" & pid & " &> /dev/null &"
    	end if
    	
    	do shell script cmd
    end launchComSkip
    
    on mcIsRunning()
    	set processPaths to do shell script "ps -xww | awk -F/ 'NF >2' | awk -F/ '{print $NF}' | awk -F '-' '{print $1}' "
    	return (processPaths contains "MarkCommercials")
    end mcIsRunning
    
  • hrcolb0hrcolb0 Posts: 1,239Members ✭✭
    Somewhat off topic, but what hardware do you use to to get cable to your mac?
  • Michael4824Michael4824 Posts: 34Members, Plex Pass ✭✭
    Me? Elgato's Hybrid, 2010 version.
  • numbnu7snumbnu7s Posts: 27Members, Plex Pass Plex Pass

    Somewhat off topic, but what hardware do you use to to get cable to your mac?


    @Harley... me? I use a home made UHF antenna to get HD OTA through a HdHomeRun device to my network.

    @Hendrick... Cool! Now I shall copy your folder creation code and paste in to my ExportDone trigger ;)
  • hrcolb0hrcolb0 Posts: 1,239Members ✭✭
    You live in a very lucky area that your cable isn't encrypted. Out here the only non encrypted cable channel is TNT
  • 1Bope1Bope Posts: 55Members
    I am hoping someone is still looking at this thread. I can't wait to try this script. EyeTV is nice but I like how Plex displays TV episodes much better. So my question, which is a little off topic, is how are you getting comskip to work? I am setting up a new HTPC so I have a 2010 MacPro running the latest EyeTV, MarkCommercials but nothing gets marked either during recording or when I run Markcommercials separately. The odd thing is I keep getting the message that Comskipper is an app I downloaded from the internet and do I want to run it every time I boot the computer. These are shows from Prime Time on major network so the commercial detecting should work well. I have asked this question on the Elgato and AVS forums but no answer. I am hoping some one here might have a suggestion.
«13456
Sign In or Register to comment.