Skip to content

Instantly share code, notes, and snippets.

@jellybeansoup
Created February 15, 2012 23:47
Show Gist options
  • Save jellybeansoup/1840156 to your computer and use it in GitHub Desktop.
Save jellybeansoup/1840156 to your computer and use it in GitHub Desktop.
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
Copy link
Author

@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