Scripted use of HandBrake to re-encode video

My workflow for importing media from my cameras reflects the fact that video is just as important to me as still images, and beginning to dominate in terms of storage needs. Which is saying a lot considering that I have a Canon 5D Mk2 and take a lot of RAW images which are over 30MB each.
Video originates mainly from my family's 2 point & shoot cameras - a Canon Powershot TX1, and my new (awesome summer camera) Canon D10, and to a lesser degree from the 5D Mk2.

Generally, the video is generally not particularly high quality - it's family orientated stuff that we really want to keep but doesn't need to be, for example, a 1080p 40Mb/s stream. So, I want to re-encode the footage to reduce my storage needs. In the case of video from the point & shoot cameras - I am well served by the "Universal" preset available in HandBrake (yes, I have an AppleTV), i.e. there' no noticeable quality loss but often storage is only 1/3 of the original. For footage from the 5D Mk2, I may use the "Universal" preset - which creates a 720p version that is still lovely and clear but uses only 10% of the storage (!!) or I handle the file using a different workflow (it will probably get used directly in Final Cut Express).

So, my workflow is something like this:

  1. Import from camera or card, using Image Capture, into an import folder - the same folder each time.
  2. Run my "encode" script from a Terminal window - see below (I could call this from Automator but haven't done so yet).
  3. From Finder, check the encoded video is ok. I have seen issues with Handbrake not creating usable output from very short input files. If all ok, delete the original video files.
  4. Import content into Aperture 3 (Yippee, Aperture 3 supports video !!)
  5. Kick off a Time Machine backup.
  6. Erase import folder.


My "encode" script.
This script does the following:

  1. Enumerates files of type avi & mov in the current folder and generates H264 mp4 files that can play on my AppleTV.
  2. Sets the timestamp of the mp4 files to be the same as the input file - since there is no EXIF-type data in the mp4 files you would otherwise lose the original time & date.

#!/bin/zsh
# Author: balrob. Date: Feb 2010. Copyright: public domain.


set -G


for EXT in 'MOV' 'mov' 'AVI' 'avi'; do
for FILE in *.$EXT; do
BASE=`echo $FILE | sed s/.$EXT//`
HandBrakeCLI -Z Universal -i $FILE -o $BASE.mp4
touch -c -t `stat -f '%Sm' -t '%Y%m%d%H%M.%S' $FILE` $BASE.mp4 
done
done

Yes, I know I could have used case-insensitive globbing, but that doesn't help me here:
sed s/.$EXT//
because sed is case sensitive.  

Comments

So your camera goes .MOV files? My cam (Panasonic HDCSD9) does 1080 but MTS files. NOTHING on the Mac reads those so I need to start going through and converting them to some other format. I'd really like a camera I could just record in a readily editable format on the mac with Final Cut Express. Unfortunately iMovie and FCE have been completely gimped in the format department.
Hi Rob, just wanted to thank you a tonne for this script! I made a couple of changes to use my own encoding settings and now it's happily chugging away converting all my hundreds of .MTS files into mp4 which is infinitely more usable by mac software.

Cheers!
Unknown said…
VLC will gladly play those MTS files.

Popular posts from this blog

Creamoata

Windows FILETIME to Javascript Date

Using an Async Iterator in Typescript