garysimpson.dev
Mobile development with swift and flutter

Fun With HandBrake Automation

May 07, 2024 at 3:12PM

Was finally able to finish a script to handle running HandBrake encodes. My use case is demo/functionality videos for merge requests. The key to making it work was updating the Automator action Pass input selection to As Arguments.

Spoiler

Feel free to cheat and download a previously created quick action built to work with a preset name1080p30 (NO AUDIO) and a folder name ~\Pictures\ScreenShots. Simply unzip and double click. download here

Once it is added right-click on the video in Finder to run the action and skip the creation steps below. You may need to allow the following if this is the first time using HandBrake CLI.




Build You Own

Tools

  • HandBrake (download)
  • HandBrakeCLI (installed w/ brew install handbrake)
  • Automator.app

Steps

  1. HandBrake.app (GUI): Create a preset use with the shortcut.
    • (e.g. 1080p30 (NO AUDIO) )
  1. Automator.app: Create a new quick action.
    • Add single Run Shell Script item.

  1. Automator.app: Update the Run Shell Script with the following script and save.
    • NOTE: Be sure to replace the 1080p30 (NO AUDIO) with your preset name.


#!/bin/bash
source ~/.bash_profile
Requires HandBrakeCLI use brew install homebrew verify with HandBrakeCLI --version
#read -p "Enter prepend name for output file:" fileName
for file in "$@";
do
Define output file name
output_file="${if%.*}".mp4
#echo "Processing file: $file"
#read -p "Enter name for output file you would like to install: " arg1
Run HandBrakeCLI command
HandBrakeCLI --preset-import-gui -Z "1080p30 (NO AUDIO)" -i "$file" -o ~/Pictures/ScreenShots/RESIZED$(date '+%H%M%S').mp4
HandBrakeCLI --preset-import-gui "1080p30 (NO AUDIO)" -i "$file" -o ~/Pictures/ScreenShots/vid-resize/"$fileName"$(date '+%Y-%m-%d_%H%M%S').mp4
done

Once its all setup you can access the Quick Action using the finder right-click menu.


Happy Coding ;-)




Helpful References