Skip to content

Instantly share code, notes, and snippets.

@ronhuang
Created October 20, 2012 17:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ronhuang/3924038 to your computer and use it in GitHub Desktop.
Save ronhuang/3924038 to your computer and use it in GitHub Desktop.
Applescript to bulk transcode video files using Handbrake (creation/modification date preserved)
(*
** TRANSCODE TO APPLE UNIVERSAL
** 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
** http://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"}
(*
** ON RUN HANDLER
** 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
(*
** ON OPEN HANDLER
** 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
(*
** ON PROCESS_ITEM
** 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
else
-- If not a folder then check the extension, file type, etc
try
set this_extension to the name extension of item_info
on error
set this_extension to ""
end try
try
set this_filetype to the file type of item_info
on error
set this_filetype to ""
end try
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
(*
** ON CONVERT_VIDEO
** 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"
try
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
set origFileCreationDate to creation date of this_item
set origFileModificationDate to modification date of this_item
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
else
-- 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=\"Normal\""
-- display dialog shellCommand — uncomment for troubleshooting
log "Executing command: " & shellCommand
tell current application
do shell script shellCommand -- comment out for debugging
do shell script "SetFile -d \"" & (getDateAsString2 of me from (origFileCreationDate)) & "\" " & newFilepathPOSIX
do shell script "SetFile -m \"" & (getDateAsString2 of me from (origFileModificationDate)) & "\" " & newFilepathPOSIX
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
else
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
to getDateAsString2 from t
-- SetFile expect "mm/dd/[yy]yy [hh:mm[:ss] [AM | PM]]"
set str to text -2 thru -1 of ("0" & getMonthNum(t) as string) & "/"
set str to str & text -2 thru -1 of ("0" & day of t as string) & "/"
set str to (str & year of t as string) & " "
set str to str & text -2 thru -1 of ("0" & hours of t as string) & ":"
set str to str & text -2 thru -1 of ("0" & minutes of t as string) & ":"
set str to str & text -2 thru -1 of ("0" & seconds of t as string)
end getDateAsString2
on getMonthNum(theDate)
-- French Vanilla, by Emmanuel Lévy
-- set theDate to the current date --or any other date
-- http://hints.macworld.com/article.php?story=20060703160750583
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
-- http://macscripter.net/viewtopic.php?id=24725
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment