nova-bavisbox-assets / encode_to_hapq.sh
NilsJohannesson's picture
skip files with _hap as suffix
5f35c98
#!/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}"