Tag Archives: Handbrake

Applescript to bulk transcode video files using Handbrake

Real life has taken over my life recently so my blog is feeling a little neglected.

However, thought I’d share something I’ve been working on for the last week.

After buying a bigger hard drive, I’ve finally got all my M2TS files from my Sony HDR-SR7 video camera consolidated together in one spot.  But so far, I’ve been using iMovie to capture and organise all my other home video footage from Video8 and miniDV tapes.

I know I can get the AVCHD (M2TS and MTS) files into iMovie by converting them with Voltaic first into a QuickTime MOV file (encoded using the Apple Intermediate Codec), or by opening up the MTS files as a camera archive.

However, one thing that the family have been bugging me for so far is to get them into a format that’s easy for the rest of the family to view.  iMovie is not exactly a media player.  Nor is using Finder with Voltaic easy for the family.

My plan was to transcode all the raw footage into something that can be viewed either on the PS3 or iPad.

Luckily Handbrake is able to transcode M2TS files into any other format.  But I had a few hundred files to process, and even though Handbrake has a queue for processing, it doesn’t have the ability to submit a bulk set of files to transcode at once.

So I ventured into AppleScript land!   I’m a professional programmer and familiar with DOS batch files, VBScript and javascript – but all really on a Windows platform.   What is this AppleScript thing?

After some mucking around, I’ve written an AppleScript which can be run, or be saved as a droplet and files or folders drag and dropped to it, which will convert the files to MP4 using Handbrake.

To help everyone else, here it is!

To use, just:

  1. Copy and paste the code below
  2. Open up Applications -> Utilities -> AppleScript Editor
  3. Paste into a new document
  4. Then either
    1. Click Run to run the script immediately    OR
    2. Save somewhere (e.g. the Desktop) with type of Application, then drag and drop the folders you want to transcode onto the icon

Sorry about the formatting – once you paste into AppleScript Editor it should look all nice again 🙂

** This droplet will convert any file/folder dropped onto it or selected to the Apple Universal format using Handbrake.
** It currently will reuse the same filename for all files except MTS files, with which it will rename using the file’s creation timestamp.
** by Jason Chong
** https://jasechong.wordpress.com
** jchong@iinet.net.au
** Revision Description
** 1.0 Initial version

property type_list : {} — e.g.: {“PICT”, “JPEG”, “TIFF”, “GIFf”}
property extension_list : {“m2ts”, “mts”, “mov”, “avi”, “mpg”, “mkv”, “dv”} — e.g.: {“txt”, “text”, “jpg”, “jpeg”}, NOT: {“.txt”, “.text”, “.jpg”, “.jpeg”}
property typeIDs_list : {} — e.g.: {“public.jpeg”, “public.tiff”, “public.png”}

** This function is called if the script is run by executing the script directly.
on run
set these_items to choose folder with prompt “Select a folder to convert:” with multiple selections allowed
set dest_folder to choose folder with prompt “Select a destination folder:”
repeat with one_item in these_items
process_item(one_item, dest_folder)
end repeat
end run

** This function is called if the script is run by dropping files/folders onto the item
on open these_items
set dest_folder to choose folder with prompt “Select a destination folder:”
repeat with one_item in these_items
process_item(one_item, dest_folder)
end repeat
end open

** This function does the high level processing of the items chosen
on process_item(the_item, dest_folder)
set the item_info to info for the_item
if folder of the item_info is true then
— If a folder then call this function recursively for each item within
set these_items to list folder the_item without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((the_item as Unicode text) & (item i of these_items))
process_item(this_item, dest_folder)
end repeat
— If not a folder then check the extension, file type, etc
set this_extension to the name extension of item_info
on error
set this_extension to “”
end try
set this_filetype to the file type of item_info
on error
set this_filetype to “”
end try
set this_typeID to the type identifier of item_info
on error
set this_typeID to “”
end try
— Only process if meets all criteria
if (package folder of the item_info is false) and (alias of the item_info is false) and ((this_filetype is in the type_list) or (this_extension is in the extension_list) or (this_typeID is in typeIDs_list)) then
convert_video(the_item, dest_folder)
end if
end if
end process_item

** This procedure does the actual conversion of video files using Handbrake
on convert_video(this_item, dest_folder)
log “Processing file: ” & this_item as string
with timeout of (720 * 60) seconds
tell application “Finder”
if (label index of this_item is 0) then

— Set to gray label to indicate processing
set label index of this_item to 7

— Set original file name
set origFilepath to this_item as string
set origFilepathPOSIX to quoted form of POSIX path of origFilepath

if name extension of this_item is “MTS” then
— If MTS then make destination filename equal to YYYYMMDDHHMMSS.MP4
set origFileCreationDate to creation date of this_item
set newFilepathPOSIX to POSIX path of dest_folder
set newFilepathPOSIX to (newFilepathPOSIX & (getDateAsString of me from (origFileCreationDate))) & “.mp4”
set newFilepathPOSIX to quoted form of newFilepathPOSIX

— For all other files, keep destination filename the same (assume it’s already in YYYYMMDDHHMMSS format)
set nameWithExtension to name of this_item
set nameWithoutExtension to getBaseName of me from (nameWithExtension)
set newFilepathPOSIX to POSIX path of dest_folder
set newFilepathPOSIX to newFilepathPOSIX & nameWithoutExtension & “.mp4”
set newFilepathPOSIX to quoted form of newFilepathPOSIX

end if

— Start the conversion
— Refer to https://trac.handbrake.fr/wiki/CLIGuide and https://trac.handbrake.fr/wiki/BuiltInPresets
— for HandBrakeCLI presets
set shellCommand to “nice /Applications/HandBrakeCLI -i ” & origFilepathPOSIX & ” -o ” & newFilepathPOSIX & ” –preset=\”Universal\””

— display dialog shellCommand — uncomment for troubleshooting
log “Executing command: ” & shellCommand
tell current application
do shell script shellCommand — comment out for debugging
end tell

— Set the label to green in case file deletion fails
set label index of this_item to 6
log “Successfully converted item: ” & this_item as string

— Remove the old file
— set shellCommand to “rm -f ” & origFilepath
— do shell script shellCommand

log “Skipping already processed item: ” & this_item as string
end if

on error errmsg
— Set the label to red to indicate failure
set label index of this_item to 2
log “Failed to convert item: ” & this_item as string

end try
end tell
end timeout
end convert_video

to getDateAsString from t
set creationDateString to year of t as string
set creationDateString to creationDateString & text -2 thru -1 of (“0” & getMonthNum(t) as string)
set creationDateString to creationDateString & text -2 thru -1 of (“0” & day of t as string)
set creationDateString to creationDateString & text -2 thru -1 of (“0” & hours of t as string)
set creationDateString to creationDateString & text -2 thru -1 of (“0” & minutes of t as string)
set creationDateString to creationDateString & text -2 thru -1 of (“0” & seconds of t as string)
return creationDateString
end getDateAsString

on getMonthNum(theDate)
— French Vanilla, by Emmanuel Lévy
— set theDate to the current date –or any other date
copy theDate to b
set the month of b to January
set monthNum to (1 + (theDate – b + 1314864) div 2629728)
return monthNum
end getMonthNum

to getBaseName from t
— by Kai Edwards
set d to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to “.” — separated at periods
if (count t’s text items) > 1 then set t to t’s text 1 thru text item -2
set t to t’s text items — splits t into a list again at the periods
tell t to set t to beginning & ({“”} & rest) — puts it back together to
set AppleScript’s text item delimiters to d — always set them back again!
return t
end getBaseName


Equivalent Mac software

I spent a lot of time last week finding equivalent software on the Mac for the ones I used to use on Windows.

DVD Ripping

Having children means having a lot of kids DVDs that get played very often.  We’ve unfortunately had a few DVDs get scratched by the kids and so I started backing them up, to be played through my PS3.

In Windows, I used a combination of DVD Decrypter, Ripit4Me and then AVS or DVDShrink to backup my home DVDs.  I wanted something simple to use that could encode in xvid with reasonable quality.  Some of my DVDs were purchased from the US, so ability to rip DVDs of other regions is important too.

Unfortunately, it seems that the range of DVD ripping utilities is less on the Mac.  I’m currently experimenting with Rip It and Handbrake.   Handbrake seems pretty good and one test run I did with the PS3 template worked without any problems.

MP3 Ripping

Music plays a huge part of my life, and being a Swing DJ, easy and fast access to any track part of my collection is essential.

In Windows, I used the tried and tested EAC + LAME combination.   This combination would be able to grab track names off the internet, securely rip my CDs and then encode them in the best MP3 settings.   Since I playback my music on my iPhone and iPod, and most of my Swing music is scratchy old 1930s-50s stuff, I am not too concerned about lossless.

So what do people on the Mac use?   iTunes seems the “standard”, but unfortunately that doesn’t have secure ripping or use LAME.   After some searching,  I’ve settled on using XLD which uses the LAME engine for MP3 encoding.   It seems to still have actively development, it has advanced ripping and can encode in a variety of formats.

Video Codecs and Playback

If you watch any movies or TV shows on your Windows computer, you probably have heard of the K-Lite Codec Pack.  It’s the best way to make sure your computer has all the necessary files to play back any video files.  It also includes the advanced Media Player Classic.

Is there an equivalent on the Mac?  The Quicktime Player seems to be the standard Mac video player.   So if we stick with that, how about the codecs?   The general consensus is that all you need is Perian and the Windows Media Components for QuickTime.   This should let you playback most videos.


I recently switched from Outlook Express to Thunderbird on Windows.  Fortunately, this made the switch to Mac really easy, as there is Thunderbird on the Mac too.

I’m still toying with switching to use Apple’s OSX Mail program as it has seemless integration with the OSX Address Book.   Unfortunately, I can’t get Google Contact syncing working at the moment with it.

Web Browser

Macs come with Safari out of the box of course.  But Firefox is my browser of choice.   Being available on both Windows and Mac made my switch very easy.

Downloading Photos

I’ve recently been experimenting with BreezeSys Downloader Pro.  I love being able to control the exact naming scheme for the folders and photo files that I’m downloading from my camera.

Unfortunately, this software isn’t available on the Mac, but I’ve since found that many of the commercial photo editing products out there already have this functionality built in!  I’ve been living in the dark ages!

MP3 Library and Playback Software

To manage all my MP3s on Windows, I’ve been using MediaMonkey.  It’s an excellent piece of software and can handle and manage large MP3 collections with ease.

When DJ’ing at Swing Dancing functions, I use 2 instances of Foobar2000 with their outputs set to different sound devices.  This lets me monitor/audition on one, whilst playing back on another.

The bad news for me is that neither of these 2 MP3 programs are available on the Mac.  Yes there is iTunes – but it’s fairly limited in terms of MP3 file management, and on the Mac, you can’t even have 2 instances with 2 different outputs!

I’m trying to resist VERY hard the temptation to install Windows and run these programs, either through BootCamp or on a virtual machine.   Surely there has to be something else on the Mac?   I’ve started reading up about Traktor but it seems overkill for what I need to do.

On a good note, I have my Airport Express working again!  After all the problems I was having on my Dell laptop, iTunes is streaming music wirelessly from the MacBook Pro to my stereo!

Other Software

Office Suite – When I bought my MacBook Pro, I also purchased the AppleCare Protection Plan for extended warranty.  Luckily there was a deal to get Microsoft Office 2008 for free with this!   This has solved my Office suite problems!   Although, I might retry OpenOffice on the Mac as I had been using this on my Windows laptop for a while.

Skype – It’s available on the Mac!

TweetDeck – Also available on the Mac!


DVD Ripping – RipIt and Handbrake
MP3 Ripping – XLD
Video Codecs – Perian and Windows Media Components for Quicktime
Email – Thunderbird
Web Browser – Firefox
MP3 Library Management – iTunes??  or run MediaMonkey in Windows (nooo!!)
Office Suite – Microsoft Office or OpenOffice
Video and Voice Calls – Skype
Twitter – TweetDeck

I know there are other alternatives, but I hope this list might help others who make the switch.

Things still left to do

  • Games (I want to continue playing Guild Wars!)
  • Sync’ing my contacts and calendar from Google and my iPhone to the Mac
  • Sharing music and photos from my MacBook Pro to my wife’s MacBook
  • Getting my Canon MP500 scanner to scan documents into the MacBook Pro
  • Connecting up with the Tivo, downloading videos and converting them to DVD or storing them to be played back through the Tivo or PS3