This article was written on October 20, 2010 by CyberNet.

Mac only 
I’ve been working with some AppleScript lately to help with my video conversions, and I’ve come up with something that saves me quite a bit of hassle. It uses the free command-line interface of Handbrake called HandbrakeCLI, and is able to convert all videos from specified extensions (ex. avi and mkv) to something that is a bit more widely supported (ex. MP4). All you have to do is point it to a directory and it will start converting all matching videos one-by-one.
How does it work? This is (kind of) a step-by-step walkthrough of how the code processes files if you’re using it without any reconfiguration. You can, of course, tailor it a bit more to your needs.
- The script looks at a drive called “SecondaryHD” in a directory called “Movies” for any video files that have an extension of AVI or MKV (it even looks recursively through all subfolders), and also makes sure that the video has no label color assigned yet. You’ll find out why the label color thing is important in the next step.
- It loops through all of the files it found, and before it begins processing it sets the label color of the file to gray. That way if you run multiple instances of this at the same time it will not process the same file twice. If you do run this multiple times, however, it only runs one instance of Handbrake at a time.
- It now runs the HandbrakeCLI using a set of parameters that I’ve found to work well on my Xbox 360 (I know the bitrate is unnecessarily high at 4000Kbps, but it helps make sure I don’t lose quality). You can configure the parameters to your liking using the information on this page. Also, it’s important to note that the command is run using “nice”, which will run the conversion process using low priority. That way it shouldn’t affect the overall performance of your system.
- It sets the label color of the original file to green, which assuming the next step works will be worthless. It’s just good measure.
- The original file is deleted so that all you have left over is the MP4 version of the original video.
- That’s it. If any error occurs during the conversion process or on any of the other steps the label color of the original file will be set to red. That way you’ll know something didn’t go as expected. Plus if the label color is set to red the video will not be reprocessed if you decide to run the script again, unless you manually remove the label color by right-clicking on the file.
Here is the code (download the script):
--on adding folder items to this_folder after receiving these_items
with timeout of (720 * 60) seconds
tell application "Finder"
--Get all AVI and MKV files that have no label color yet, meaning it hasn’t been processed
set allFiles to every file of entire contents of ("SecondaryHD:Movies" as alias) whose ((name extension is "avi" or name extension is "mkv") and label index is 0)
--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)
try
--Set to gray label to indicate processing
set label index of currentFile to 7
--Assemble original and new file paths
set origFilepath to quoted form of POSIX path of (currentFile as alias)
set newFilepath to (characters 1 thru -5 of origFilepath as string) & "mp4'"
--Start the conversion
set shellCommand to "nice /Applications/HandBrakeCLI -i " & origFilepath & " -o " & newFilepath & " -e x264 -b 4000 -a 1 -E faac -B 160 -R 48 -6 dpl2 -f mp4 –crop 0:0:0:0 -x level=40:ref=2:mixed-refs:bframes=3:weightb:subme=9:direct=auto:b-pyramid:me=umh:analyse=all:no-fast-pskip:filter=-2,-1 ;"
do shell script shellCommand
--Set the label to green in case file deletion fails
set label index of currentFile to 6
--Remove the old file
set shellCommand to "rm -f " & origFilepath
do shell script shellCommand
on error errmsg
--Set the label to red to indicate failure
set label index of currentFile to 2
end try
end repeat
end tell
end timeout
--end adding folder items to
Notes about the code:
- You can specify any extensions you want to include in the conversion process, but it really only works with extensions that are 3 characters based on the way it generates the filename of the new path. I’m sure this can be improved, but I only wanted AVI and MKV files converted.
- You’ll likely need to update the folder location that is searched. The way the path is specified is in an AppleScript format, and this may help you if you’ve never dealt with them before.
- This is is assuming you’ve downloaded HandbrakeCLI and put it in the Applications folder.
- You can use this with folder actions if you uncomment the first and last lines. Keep in mind that this enables it to run when a file is added to a particular folder, but will still process every matching file in that folder. It doesn’t actually use the items it is passed.
- You can schedule this to run at certain times using iCal.
- Try downloading the script directly if you copied and pasted the script and are having troubles. There may be some characters that got incorrectly encoded when being posted here, and won’t translate well in the code.
If you’ve got any updates to the code please feel free to send them our way. There are probably much more elegant ways of doing this, but this works well for me. Hopefully this will at least point some of you in the right direction for creating your own scripts.
Copyright © 2011 CyberNetNews.com
Related Posts:


