Skip to content

Instantly share code, notes, and snippets.

@loshlee
Last active February 29, 2024 23:22
Show Gist options
  • Save loshlee/32c9aca2fa45e241c652fc810772bca1 to your computer and use it in GitHub Desktop.
Save loshlee/32c9aca2fa45e241c652fc810772bca1 to your computer and use it in GitHub Desktop.
Mac AppleScript sources for passing options to and executing 'two-pass-transcode.rb' by Lisa Melton (https://github.com/lisamelton/more-video-transcoding).
## This AppleScript can be used to create a macOS application to pass options to and execute Lisa Melton's video transcoding Ruby script (two-pass-transcode.rb).
## It is configured with additional constructs for detelecine, aggressive cropping, and converting output to .mp4.
## (See https://github.com/lisamelton/more-video-transcoding)
use AppleScript version "2.5"
use scripting additions
property temppath : "/private/tmp/"
property tmpfile : "/tmp/execme.command"
on open the_items
my handbrake_transcode(the_items)
end open
on handbrake_transcode(the_items)
set theshellscript to ""
repeat with the_item in the_items
set the_item to the_item as alias
try
tell application "Finder"
set sost to (container of the_item) as string
end tell
set pos_filepath to POSIX path of sost
end try
set this_filepath to (the_item as text)
if last character of this_filepath is ":" then
tell me to set it_is_a_folder to true
else
set it_is_a_folder to false
end if
set thesourcename to (name of (info for the_item))
set namepart to (name extension of (info for the_item))
set the_source_file to POSIX path of this_filepath
set newname to replace_chars(thesourcename, namepart, "detelecine.mp4")
set the_output_file to replace_chars(newname, "detelecine.mp4", "mkv")
set endFile to replace_chars(the_output_file, "mkv", "mp4")
try
set theshellscript to the theshellscript & "cd" & space & pos_filepath & ";two-pass-transcode.rb --ac3-surround -x detelecine -x rate=23.976 -x cfr -x crop-mode=auto" & space & (quoted form of the_source_file) & ";ffmpeg -i" & space & (quoted form of (pos_filepath & the_output_file)) & space & "-c copy" & space & (quoted form of (pos_filepath & endFile)) & ";rm -rf" & space & (quoted form of (pos_filepath & the_output_file)) & ";/bin/echo'" & space & "
==========================
" & newname & space & "FINISHED!
==========================
';"
on error onerr
activate
display dialog onerr
end try
end repeat
set theshellscript to theshellscript & "sleep 2;mv" & space & (quoted form of tmpfile) & space & (quoted form of (POSIX path of (path to trash)))
do shell script "echo " & quoted form of theshellscript & " > " & tmpfile
repeat
do shell script "chmod +x " & tmpfile
do shell script "open -a Terminal.app" & space & tmpfile
exit repeat
end repeat
end handbrake_transcode
on replace_chars(this_text, _bad, _good)
set AppleScript's text item delimiters to the _bad
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the _good as text
set this_text to the item_list as text
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
on run
set the_items to ((choose file) as list)
handbrake_transcode(the_items)
end run
## This AppleScript can be used to create a macOS application to pass options to and execute Lisa Melton's video transcoding Ruby script (two-pass-transcode.rb).
## (https://github.com/lisamelton/more-video-transcoding)
use AppleScript version "2.5"
use scripting additions
property temppath : "/private/tmp/"
property tmpfile : "/tmp/execme.command"
on open the_items
my handbrake_transcode(the_items)
end open
on handbrake_transcode(the_items)
set theshellscript to ""
repeat with the_item in the_items
set the_item to the_item as alias
try
tell application "Finder"
set sost to (container of the_item) as string
end tell
set pos_filepath to POSIX path of sost
end try
set this_filepath to (the_item as text)
if last character of this_filepath is ":" then
tell me to set it_is_a_folder to true
else
set it_is_a_folder to false
end if
set thesourcename to (name of (info for the_item))
set namepart to (name extension of (info for the_item))
set the_source_file to POSIX path of this_filepath
set newname to replace_chars(thesourcename, namepart, "mkv")
try
set theshellscript to the theshellscript & "cd" & space & pos_filepath & ";two-pass-transcode.rb" & space & (quoted form of the_source_file) & ";/bin/echo'" & space & "
==========================
" & newname & space & "FINISHED!
==========================
';"
on error onerr
activate
display dialog onerr
end try
end repeat
set theshellscript to theshellscript & "sleep 2;mv" & space & (quoted form of tmpfile) & space & (quoted form of (POSIX path of (path to trash)))
do shell script "echo " & quoted form of theshellscript & " > " & tmpfile
repeat
do shell script "chmod +x " & tmpfile
do shell script "open -a Terminal.app" & space & tmpfile
exit repeat
end repeat
end handbrake_transcode
on replace_chars(this_text, _bad, _good)
set AppleScript's text item delimiters to the _bad
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the _good as text
set this_text to the item_list as text
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
on run
set the_items to ((choose file) as list)
handbrake_transcode(the_items)
end run
@loshlee
Copy link
Author

loshlee commented Apr 18, 2023

This gist (which is only useful on a Mac) consists of two AppleScript text files named "two-pass-transcode.applescript" and "two-pass-detelecine-aggr-crop-mp4-out.applescript".

Each of the files consists of text that can be saved as an AppleScript droplet application that passes options to and executes Lisa Melton's ruby script (two-pass-transcode.rb) to transcode a selection of video files in the Mac Finder. I've dropped up to 5, I wouldn't want to tie up my Mac for days at a time, so it only works for dropped files, not folders. Lisa Melton's Ruby script (two-pass-transcode.rb) in turn executes the handbrakeCLI command line executable to perform the transcoding.

This comment contains information about the shorter of the two files, "two-pass-transcode.applescript". I'll post a subsequent comment pertaining specifically to the longer of the two files, "two-pass-detelecine-aggr-crop-mp4-out.applescript".

When saved as an AppleScript or AppleScript application, the file named "two-pass-transcode.applescript" applies the "out of the box" treatment of two-pass-transcode.rb, completely described at more-video-transcoding. Just create a document from "two-pass-transcode.applescript" in Script Editor, then save as an application. Drop the source video files onto the new app's icon. If two-pass-transcode.rb, handbrakeCLI, and ffprobe are correctly installed and working, the application will create the corresponding transcoded .mkv files in the same folder from which the originals are dropped, providing that folder isn't read-only.

Don't be concerned about "no such file or directory" in the Terminal window, it's just a byproduct of an mv command, which moves the operative command line script from /tmp to the user's trash. My experience is that "no such file or directory" gets added if the script takes a certain minimal amount of time to work, which successful transcoding usually does.

@loshlee
Copy link
Author

loshlee commented Apr 18, 2023

Here's the comment promised in the previous comment pertaining specifically to the file named "two-pass-detelecine-aggr-crop-mp4-out.applescript".

When saved as an AppleScript or AppleScript application, the file named "two-pass-detelecine-aggr-crop-mp4-out.applescript" provides a method to incorporate the suggestions offered by @lisamelton at more-video-transcoding to meet my specific transcoding needs when using two-pass-transcode.rb on my Mac. Most video sources for the content I transcode are generated by an add-on hardware capture device called the Hauppauge HDPVR 2. This device is not compatible with video sources for which copying is prohibited through HDCP.

I find that the highest quality output from the device is normally received in telecine format, so my transcode process requires IVTC (or "detelecine"), as incorporated into this AppleScript.

Please refer to my previous comment for general information about using both AppleScripts.

This AppleScript includes an ffmpeg construct for removing and replacing the default .mkv output of two-pass-transcode.rb with .mpv output. This construct can be removed by deleting

";ffmpeg -i" & space & (quoted form of (pos_filepath & the_output_file)) & space & "-c copy" & space & (quoted form of (pos_filepath & endFile)) & ";rm -rf" & space & (quoted form of (pos_filepath & the_output_file)) &

from the AppleScript.

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