Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/mrgnw/scripts.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115#!/usr/bin/env bashset -euo pipefail
# Defaults tuned for screen recordingsENCODER="vt_h264" # vt_h264, vt_h265, vt_h265_10bitQUALITY=20 # Lower = better quality (and larger file)FRAMERATE="" # Empty -> same as source; e.g. 30 to force 30 fps
usage() { cat <<EOFUsage: $(basename "$0") [options]
Compress video screen recordings in the CURRENT DIRECTORY using HandBrakeCLI + VideoToolbox.
Behavior:- Processes only video files directly in the current directory (no subdirectories).- Writes compressed .mp4 files alongside the originals, named: basename.cps.mp4- After a successful encode, moves the original file to the macOS Trash using 'trash'.
Options: -e encoder Video encoder (default: $ENCODER) Examples: vt_h264, vt_h265, vt_h265_10bit, x264, x265 -q quality Constant quality (float, lower = better). Default: $QUALITY -r fps Output frame rate (default: same as source) -h Show this help
Examples: $(basename "$0") $(basename "$0") -e vt_h265 -q 22EOF}
while getopts "e:q:r:h" opt; do case "$opt" in e) ENCODER="$OPTARG" ;; q) QUALITY="$OPTARG" ;; r) FRAMERATE="$OPTARG" ;; h) usage; exit 0 ;; *) usage; exit 1 ;; esacdoneshift $((OPTIND - 1))
# No positional arguments expectedif [ "$#" -ne 0 ]; then usage exit 1fi
INPUT_DIR="."OUTPUT_DIR="."
EXTENSIONS=("mov" "mp4" "m4v" "mkv" "avi")
process_file() { local in="$1" local rel rel="$(basename "$in")" local base="${rel%.*}" # Add .cps before extension to avoid collisions with originals local out="$OUTPUT_DIR/$base.cps.mp4"
if [ -f "$out" ]; then echo "Output already exists, skipping: $out" return fi
echo "Processing:" echo " Input: $in" echo " Output: $out"
# Build frame rate options local rate_opts=() if [ -n "$FRAMERATE" ]; then rate_opts=(-r "$FRAMERATE" --cfr) else rate_opts=(--vfr) fi
if HandBrakeCLI \ -i "$in" \ -o "$out" \ -e "$ENCODER" \ -q "$QUALITY" \ "${rate_opts[@]}" \ -E av_aac -B 128 -R 48 \ -f av_mp4 \ --enable-hw-decoding videotoolbox then echo "Encode succeeded, moving original to Trash: $in" trash "$in" else echo "Encode FAILED for $in, leaving original in place." >&2 [ -f "$out" ] && rm -f "$out" fi}
have_files=false
for ext in "${EXTENSIONS[@]}"; do for file in "$INPUT_DIR"/*."$ext"; do if [ ! -f "$file" ]; then continue fi have_files=true process_file "$file" donedone
if [ "$have_files" = false ]; then echo "No matching video files found in current directory."fi
echo "Done."