On October 20, 2013 In Tutorials

Batch Encoding Videos HandBrake CLI & growlnotify (Mac OS X)

I ran into a slight problem where growlnotify command was not found while running a Shell Script in Automator. Took me sometime to find out the problem which was actually quite easy (if I had a lil bit more experience with unix)

Noob with Unix commands and all, thought I will write a short tutorial and solutions to the average user with none or little experience with unix commands in Mac

I can’t cover all topics in a single post, it would result in a tutorial for using Growl Notifications , HandBrake CLI and Automator. Anyone with some experience will understand what is going on, and I will try to describe with some detail on their actions.

Case – Batch Encoding Videos with HandBrake CLI (Service)

2013-10-20 09.30.11 am

Description:

A service I created some time ago, which batch encode videos which I have shot into a lower quality video file (mp4) using HandBrake CLI (a command line version of video converting tool HandBrake) for backup. It also labels the selected files as Grey (I can filter out the labelled items and delete them). A general useful service, you can also encode other video formats like avi, mkv etc into mp4

 /Applications/HandBrakeCLI -i "$if" -o "$if".mp4 --preset="Normal"

The code for standard encoding. Encodes current selected file(s) and output to same folder with .mp4 extension

/Applications/HandBrakeCLI -i "$if" -o "$if".mp4 --preset="Normal" > HandBrake-log.txt

( > HandBrake-log.txt ) outputs encoding process into a text file, which is then read by GeekTool onto my Desktop.

2013-10-20 09.52.36 am

Case: Adding notifications using Growl

Encoding videos is a time consuming process and I thought it would be really nice if I could use a Growl notification to notify that the video encoding has been finished or which file it is actually encoding currently.

Problem: growlnotify command not found (error 127)

Installed GrowlNotify 2.1 (under General downloads of site) and tried running the command in Terminal and Growl works, but when I put in the code with HandBrake shell script, a command not found error occurred.

2013-10-20 10.08.05 am


Solution (Updated! 10.11 El Capitan)

 


After updating to El Capitan, copying growlnotify to /usr/bin no longer work as Terminal will return the following: Operation not permitted

Operation not permitted

Operation not permitted

Newest solution here: Update the automator code to call the growlnotify directly! (Weird I didn’t think about this before)

Handbrake CLI automator 10.11

Handbrake CLI automator 10.11

for if in "[email protected]"
do
 /usr/local/bin/growlnotify -a "HandBrake" -m "Encoding ${if##*/}" "HandBrake Encode - Normal"
 /Applications/HandBrakeCLI -i "$if" -o "$if".mp4 --preset="Normal" > HandBrake-log.txt
 /usr/local/bin/growlnotify -s -a "HandBrake" -m "Encoding Done" "${if##*/}"
done

Solution Notes: (Before 10.11)

It appears that my growlnotify command is installed in a different location other than /usr/bin , and running the shell script in Automator will result in not being able to find the command.

Knowing your $PATH in Automator & finding growlnotify

Explaining on what $PATH is, is beyond the scope of this post. Summary, $PATH is a variable in Unix, and in this case, tells Automator where to look for commands. If your growlnotify is not in the list of folders provided in $PATH, it will not be able to find your growlnotify command , thus the command not found error. Simple.

So what/where is the $PATH in Automator?

2013-10-20 10.20.43 am

Run a simple echo $PATH and you see the results in Automator. Now we know that Automator is looking for the growlnotify command in /usr/bin:/bin:/usr/sbin:/sbin

That looks complicated?! ” : ” the colon is used to separate folders. Just means $PATH consists of folder /usr/bin & /bin & /usr/sbin & /sbin

So where is my growlnotify ?! Go back to Terminal, type in:

which growlnotify

2013-10-20 10.33.36 am

 

My growlnotify is installed in /usr/local/bin/ (red arrow) , now we know, where our growlnotify is located, and why the command was not found, obviously, its in a different place. Your growlnotify might be installed in a different location.

Why running growlnotify in Terminal works

As we do an echo $PATH in Terminal, my  $PATH consists of /usr/local/bin (red arrow), which is where growlnotify is located, so it works.  (This varies, your $PATH might be different with mine, but it doesn’t matter)

Not sure on this, as I said, not a unix veteran, so this might actually be the default location whenever a new command is installed. Anyway:

My Solution

Copy the growlnotify command into /usr/bin

To do that you can use the following command:

sudo cp /usr/local/bin/growlnotify /usr/bin/growlnotify

Please replace the code above with your own growlnotify directory. Your growlnotify location might be different to mine. 

After you have run the command, you can double check again by running ” which growlnotify ” in Terminal to see where it is located now. It should show ” /usr/bin/growlnotify ”

Now go back to Automator, run a test shell script, and it should work. Mine did 🙂

Updated Shell Script (10.11 Version click here!)

2013-10-20 11.10.58 am

for if in "[email protected]"
do
 growlnotify -a "HandBrake" -m "Encoding ${if##*/}" "HandBrake Encode - Normal"
 /Applications/HandBrakeCLI -i "$if" -o "$if".mp4 --preset="Normal" > HandBrake-log.txt
 growlnotify -s -a "HandBrake" -m "Encoding Done" "${if##*/}"
done

Note – Pass input: as arguments

Breakdown

growlnotify -a "HandBrake" -m "Encoding ${if##*/}" "HandBrake Encode - Normal"

2013-10-20 11.09.46 am

growlnotify -s -a "HandBrake" -m "Encoding Done" "${if##*/}"

2013-10-20 11.12.49 am

 

Adding a ” -s ” will sticky the Growl notification.

The code without output to text file

for if in "[email protected]"
do
 growlnotify -a "HandBrake" -m "Encoding ${if##*/}" "HandBrake Encode - Normal"
 /Applications/HandBrakeCLI -i "$if" -o "$if".mp4 --preset="Normal"
 growlnotify -s -a "HandBrake" -m "Encoding Done" "${if##*/}"
done

Phew! A little nice addition to a useful shell script. There are other and even better code out there which includes better file manipulation, but it is out of the scope of this post.

Thank you for reading and please feel free to use this shell script sample in your Automator actions or comments below 😉

Scroll Up