File size: 4,600 Bytes
7c84b21 5f35c98 7c84b21 5f35c98 7c84b21 5f35c98 7c84b21 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | #!/bin/bash
# Batch-encode videos to the Hap family (hap / hap_q / hap_alpha / hap_q_alpha).
# Outputs are written next to each source file with a Hap suffix in the filename.
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Check if ffmpeg is available
if ! command -v ffmpeg &> /dev/null; then
echo -e "${RED}Error: ffmpeg is not installed or not in PATH${NC}"
exit 1
fi
# ---------------------------
# Settings (override via env)
# ---------------------------
# HAP_FORMAT:
# - hap : smallest + fastest (DXT1-ish), no alpha
# - hap_q : higher quality (better gradients), larger + slower than hap
# - hap_alpha : alpha support, larger than hap
# - hap_q_alpha : best quality + alpha, largest of the Hap variants
HAP_FORMAT="${HAP_FORMAT:-hap}"
# HAP_COMPRESSOR: "snappy" or "none" (snappy usually smaller, none usually faster)
HAP_COMPRESSOR="${HAP_COMPRESSOR:-snappy}"
# FFMPEG_THREADS: 0 = let ffmpeg decide / use all cores
FFMPEG_THREADS="${FFMPEG_THREADS:-0}"
case "$HAP_FORMAT" in
hap|hap_q|hap_alpha|hap_q_alpha) ;;
*)
echo -e "${RED}Error: Unsupported HAP_FORMAT='$HAP_FORMAT' (use hap|hap_q|hap_alpha|hap_q_alpha)${NC}"
exit 1
;;
esac
case "$HAP_COMPRESSOR" in
snappy|none) ;;
*)
echo -e "${RED}Error: Unsupported HAP_COMPRESSOR='$HAP_COMPRESSOR' (use snappy|none)${NC}"
exit 1
;;
esac
# Function to encode a single video file
encode_video() {
local input_file="$1"
local input_dir=$(dirname "$input_file")
local input_basename=$(basename "$input_file")
local input_name="${input_basename%.*}"
local input_ext="${input_basename##*.}"
local suffix="hap"
case "$HAP_FORMAT" in
hap) suffix="hap" ;;
hap_q) suffix="hapq" ;;
hap_alpha) suffix="hapalpha" ;;
hap_q_alpha) suffix="hapqalpha" ;;
esac
# Write output next to input file with a Hap suffix in the filename.
# Example: ./Projection_Lab/Scene_001/file.mp4 -> ./Projection_Lab/Scene_001/file_${suffix}.mov
# Example: ./file.mp4 -> ./file_${suffix}.mov
local output_file="${input_dir}/${input_name}_${suffix}.mov"
# Skip if output already exists
if [ -f "$output_file" ]; then
echo -e "${YELLOW}Skipping ${input_file} - output already exists${NC}"
return 0
fi
echo -e "${GREEN}Encoding: ${input_file}${NC}"
echo -e " Output: ${output_file}"
# Run ffmpeg with Hap settings
# -nostdin: prevents ffmpeg from reading from stdin (important for batch processing)
# -threads: use all available CPU cores by default (FFMPEG_THREADS=0)
ffmpeg -nostdin -threads "$FFMPEG_THREADS" -i "$input_file" \
-vf "pad=ceil(iw/4)*4:ceil(ih/4)*4" \
-c:v hap -format "$HAP_FORMAT" \
-compressor "$HAP_COMPRESSOR" \
-an \
-y \
"$output_file" 2>&1
if [ $? -eq 0 ]; then
echo -e "${GREEN} ✓ Success${NC}"
else
echo -e "${RED} ✗ Failed${NC}"
return 1
fi
}
# Find all video files (mp4, mov, avi, mkv, etc.)
# Starting from current directory, recursively search
echo -e "${GREEN}Searching for video files...${NC}"
echo ""
# Counter for processed files
total=0
success=0
failed=0
skipped=0
# Find and process video files
# Store files in an array to avoid subshell issues with pipe
mapfile -t video_files < <(find . -type f \( -iname "*.mp4" -o -iname "*.mov" -o -iname "*.avi" -o -iname "*.mkv" -o -iname "*.m4v" -o -iname "*.flv" -o -iname "*.wmv" \))
# Process each file
for file in "${video_files[@]}"; do
# Skip empty entries
[ -z "$file" ] && continue
# Skip files that are already hap-encoded outputs
local_basename=$(basename "$file")
local_name="${local_basename%.*}"
if [[ "$local_name" =~ _(hap|hapq|hapalpha|hapqalpha)$ ]]; then
echo -e "${YELLOW}Skipping ${file} - already a Hap-encoded file${NC}"
skipped=$((skipped + 1))
continue
fi
total=$((total + 1))
if encode_video "$file"; then
success=$((success + 1))
else
failed=$((failed + 1))
fi
echo ""
done
# Summary
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Encoding complete!${NC}"
echo -e "Total files found: ${total}"
echo -e "${GREEN}Successfully encoded: ${success}${NC}"
if [ $skipped -gt 0 ]; then
echo -e "${YELLOW}Skipped (already Hap): ${skipped}${NC}"
fi
if [ $failed -gt 0 ]; then
echo -e "${RED}Failed: ${failed}${NC}"
fi
echo -e "${GREEN}========================================${NC}"
|