Below is a “minimum-fuss / maximum-safety” recipe that will:
- Use HandBrakeCLI (not the GUI) so we can script everything.
- Work through the library one file at a time (no crashes, no 100 % CPU for hours).
- Replace each original only after a successful encode.
- Remember what it has already done (via an extended file attribute) so you can stop/restart whenever you like.
Feel free to skim; you can copy-paste the snippets almost verbatim.
🛠️ = you do it once.
🤖 = the script does it forever.
────────────────────────────────────────
- Install the tools (🛠️)
────────────────────────────────────────
# Homebrew (if you haven't already)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# HandBrake CLI
brew install handbrake
Check it works:
HandBrakeCLI --version # Should print something like "HandBrake 1.7.x"
────────────────────────────────────────
2. Create / export your HandBrake preset (🛠️)
────────────────────────────────────────
- Open the HandBrake GUI once.
- Load one of your source files, configure everything exactly the way you like (codec, bitrate / quality, audio-track handling, subtitles, etc.).
- Save it as a new preset, e.g. “MyTV”.
- File ➝ Export ➝ Preset ➝ save it as
~/my_handbrake_preset.json.
HandBrakeCLI can later import that JSON and use it 1:1.
────────────────────────────────────────
3. Write the batch script (🛠️, takes 2 minutes)
────────────────────────────────────────
Save the following as ~/bin/batch-handbrake.sh and make it executable:
#!/usr/bin/env bash
########################################################################
# batch-handbrake.sh – slow & safe re-encode of an entire library
# • Marks completed files with an xattr (user.handbrake_done=1)
# • Replaces original ONLY after successful encode
# • Runs with lowered priority so your Mac stays usable
########################################################################
# ‼️ EDIT THESE THREE LINES ‼️
LIBRARY="/Users/YOU/TV Shows" # folder that holds all episodes
PRESET_JSON="$HOME/my_handbrake_preset.json"
PRESET_NAME="MyTV"
# Optional: lower thread count for “slow & cool” encodes
THREADS=4
LOG="$HOME/handbrake_batch.log"
TMPDIR="${TMPDIR:-/tmp}"
export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH" # ensure HandBrakeCLI is found
# ————————————————————————— iteration —————————————————————————
find "$LIBRARY" \( -iname '*.mkv' -o -iname '*.avi' -o -iname '*.ts' -o \
-iname '*.mp4' -o -iname '*.mov' \) -print0 |
while IFS= read -r -d '' SRC; do
# Skip if we've marked it already
if xattr -p user.handbrake_done "$SRC" &>/dev/null; then
echo "$(date): SKIP $SRC (already done)" >> "$LOG"
continue
fi
echo "$(date): START $SRC" >> "$LOG"
BASENAME="${SRC%.*}"
DEST="$TMPDIR/$(basename "$BASENAME").tmp.mp4"
# Nice = lower CPU priority; caffeinate keeps Mac from sleeping if you like
nice -n 15 HandBrakeCLI \
--preset-import-file "$PRESET_JSON" \
--preset "$PRESET_NAME" \
--threads $THREADS \
-i "$SRC" -o "$DEST"
if [[ $? -eq 0 && -s "$DEST" ]]; then
# success 🎉 : swap files atomically
mv "$SRC" "$SRC.bak"
mv "$DEST" "${BASENAME}.mp4"
xattr -w user.handbrake_done 1 "${BASENAME}.mp4"
rm -f "$SRC.bak"
echo "$(date): DONE $SRC" >> "$LOG"
else
echo "$(date): FAIL $SRC" >> "$LOG"
rm -f "$DEST"
fi
done
Then:
chmod +x ~/bin/batch-handbrake.sh
What it does
• One file at a time
• Marks the final .mp4 with user.handbrake_done
• nice -n 15 keeps CPU usage low; adjust THREADS if you want even cooler temps.
• Keeps a plain-text log.
────────────────────────────────────────
4. Test with ONE episode (🛠️)
────────────────────────────────────────
~/bin/batch-handbrake.sh
Watch the log (tail -f ~/handbrake_batch.log) and verify:
• Output quality is good
• File got replaced correctly
• Extended attribute is set (xattr -p user.handbrake_done yourEpisode.mp4 → 1)
If all good, move on.
────────────────────────────────────────
5. Put it on autopilot with launchd (🛠️ once)
────────────────────────────────────────
Create ~/Library/LaunchAgents/com.you.batch-handbrake.plist with:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key> <string>com.you.batch-handbrake</string>
<key>ProgramArguments</key> <array>
<string>/Users/YOU/bin/batch-handbrake.sh</string>
</array>
<key>StartInterval</key> <integer>1800</integer> <!-- every 30 min -->
<key>StandardErrorPath</key><string>/Users/YOU/handbrake_launchd.err</string>
<key>StandardOutPath</key> <string>/Users/YOU/handbrake_launchd.out</string>
<key>Nice</key> <integer>15</integer> <!-- extra caution -->
</dict>
</plist>
Load it:
launchctl load ~/Library/LaunchAgents/com.you.batch-handbrake.plist
Now, every 30 minutes macOS wakes the script; it processes the next un-done file, then exits.
This “drip-feed” style keeps temps low and lets Spotlight / Photos / whatever else run in between.
────────────────────────────────────────
6. Monitoring & tweaks
────────────────────────────────────────
• See what launchd is doing
launchctl list | grep batch-handbrake
• View logs
tail -f ~/handbrake_batch.log
• Change CPU aggression
‑ Lower THREADS, raise nice value, or add caffeinate -i if you don’t want sleep.
• Undo / re-encode a file
xattr -d user.handbrake_done someEpisode.mp4 → script will pick it up again.
────────────────────────────────────────
7. Pause / stop
────────────────────────────────────────
launchctl unload ~/Library/LaunchAgents/com.you.batch-handbrake.plist
…then reload when you’re ready.
────────────────────────────────────────
8. (Optional) Trash originals instead of delete
────────────────────────────────────────
Replace rm -f "$SRC.bak" with:
osascript -e 'tell application "Finder" to delete POSIX file "'"$SRC.bak"'"'
so they land in the macOS Trash.
────────────────────────────────────────
That’s it! 🥳
You now have a hands-off pipeline that will very gently chew through your 2014 Mac mini’s entire TV library, remember its progress, and leave you with neatly re-encoded files in place.
Questions? Want to add hardware acceleration, notifications, or CSV reports? Just let me know!