Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatic AVI/MKV conversion for iTunes
--Set the location for where your torrents download to
set torrentsLocation to "/Users/Jelly/Torrents"
--Set the location for where HandbrakeCLI lives
set handbrakeLocation to "/Applications/Utilities/HandBrakeCLI"
--Settings up some handlers…
on fileExists(posixFile)
try
if not (exists posixFile as alias) then return false
on error
return false
end try
return true
end fileExists
on replaceText(find, replace, someText)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set someText to text items of someText
set text item delimiters of AppleScript to replace
set someText to "" & someText
set text item delimiters of AppleScript to prevTIDs
return someText
end replaceText
--Let's begin...
--Determine if HandBrakeCLI is already running
set activityLog to do shell script "/bin/ps -ax"
set segmentedActivityLog to (every word of activityLog)
set isRunning to segmentedActivityLog contains "HandBrakeCLI"
--If HandBrakeCLI is not running
if isRunning = false then
--Get all AVI and MKV files that have no label color yet, meaning it hasn't been processed
tell application "Finder"
set allFiles to every file of entire contents of (POSIX file torrentsLocation as alias) whose ((name extension is "avi" or name extension is "mkv") and label index is 0)
end tell
--Repeat for all files in above folder
repeat with i from 1 to number of items in allFiles
set currentFile to (item i of allFiles)
-- Only process files that aren't being or haven't been processed
tell application "Finder"
if label index of currentFile is not 0 then exit repeat
end tell
--Set to gray label to indicate processing
tell application "Finder" to set label index of currentFile to 7
--Assemble original and new file paths
tell application "Finder" to set origFilepath to POSIX path of (currentFile as alias)
set logFilepath to (characters 1 thru -5 of origFilepath as string) & ".log"
-- Try and find a unique filename by appending a number to the end if necessary
set partDuplicate to 1
set partFilepath to (characters 1 thru -5 of origFilepath as string) & ".hb"
repeat while fileExists(POSIX file partFilepath)
set partDuplicate to partDuplicate + 1
set partFilepath to (characters 1 thru -5 of origFilepath as string) & " " & partDuplicate & ".hb"
end repeat
--Start the conversion
set shellCommand to "nice " & handbrakeLocation & " -i '" & origFilepath & "' -o '" & partFilepath & "' --preset='High Profile'" & " // &> '" & logFilepath & "'"
do shell script shellCommand
--Set the label to green in case file deletion fails
tell application "Finder" to set label index of currentFile to 6
--Wait a bit while things settle
delay 1
--Rename the converted file
set partFile to (POSIX file partFilepath as alias)
set newDuplicate to 1
tell application "Finder" to set partFilename to name of partFile
set newFilename to (characters 1 thru -3 of partFilename as string) & "m4v"
set newFilepath to replaceText(partFilename, newFilename, partFilepath)
repeat while fileExists(POSIX file newFilepath)
set newDuplicate to newDuplicate + 1
set newFilename to (characters 1 thru -4 of partFilename as string) & " " & newDuplicate & ".m4v"
set newFilepath to replaceText(partFilename, newFilename, partFilepath)
end repeat
tell application "Finder"
set name of partFile to newFilename
set label index of partFile to 6
end tell
--Remove the old file
set shellCommand to "rm -f " & logFilepath
do shell script shellCommand
end repeat
end if
--Get all M4V files that have been converted
tell application "Finder"
set allFiles to every file of entire contents of (POSIX file torrentsLocation as alias) whose ((name extension is "m4v" or name extension is "mp4") and label index is 6)
end tell
--Repeat for all files in above folder
repeat with i from 1 to number of items in allFiles
set currentFile to (item i of allFiles)
--Clear the label color
tell application "Finder"
set label index of currentFile to 0
end tell
--Import the file with iFlicks
tell application "iFlicks"
import (currentFile as alias) as QuickTime movie with deleting without gui
end tell
end repeat
@jellybeansoup

This comment has been minimized.

Owner

jellybeansoup commented Feb 15, 2012

For explanation on how to use this script, please see the related blog post at http://blog.jellystyle.com/post/5302942680/automating-your-tv-downloads

@jellybeansoup

This comment has been minimized.

Owner

jellybeansoup commented Feb 26, 2012

Made some adjustments so to allow iFlicks to clean up more frequently. Files are processed by Handbrake with a .hb extension, which is changed to .m4v once they are finished converting. This means that iFlicks can run more often, and won't attempt to load files that Handbrake hasn't finished with.

Duplicates are also catered for, so if for some reason a file is converted twice, both will coexist happily. This is to fix a problem I was having where the script would end up running multiple instances in tandem, and end up processing files twice. The second instance would effectively overwrite the (sometimes completed) original, meaning I had to wait twice as long (if i was trying to clean things up manually).

@jellybeansoup

This comment has been minimized.

Owner

jellybeansoup commented Feb 26, 2012

Forgot to take out a section of code that reset labels for debugging purposes.

Also threw in a little section that will determine whether the original file is or has been processed to try to help stop duplicated conversion.

@zkahn94

This comment has been minimized.

zkahn94 commented Dec 11, 2014

Seeing an error with the part about QuickTime at the end. Thoughts?

@jellybeansoup

This comment has been minimized.

Owner

jellybeansoup commented Jan 16, 2015

@zkahn94 You probably don't have iFlicks, or if you do, it doesn't respond to iFlicks (I'm not sure how the new version works, it actually has this functionality built-in).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment