#!/bin/bash

# Debug script to test HEVC alpha conversion step by step
# Usage: ./test_hevc.sh input.webm

if [ $# -eq 0 ]; then
    echo "Usage: $0 input.webm"
    exit 1
fi

INPUT="$1"
BASE=$(basename "$INPUT" .webm)

echo "=== Testing HEVC Alpha Conversion ==="
echo "Input: $INPUT"
echo ""

# Test 1: Basic HEVC (your original command format)
echo "Test 1: Basic HEVC with alpha..."
ffmpeg -y -c:v libvpx-vp9 -i "$INPUT" -c:v hevc_videotoolbox -q:v 60 -allow_sw 1 -alpha_quality 0.7 -vtag hvc1 -movflags +faststart "${BASE}_test1.mp4"
if [ $? -eq 0 ]; then
    echo "✓ Test 1 successful"
    ls -lh "${BASE}_test1.mp4"
else
    echo "✗ Test 1 failed"
fi
echo ""

# Test 2: Simplified approach (no audio)
echo "Test 2: HEVC without audio..."
ffmpeg -y -c:v libvpx-vp9 -i "$INPUT" -c:v hevc_videotoolbox -q:v 60 -allow_sw 1 -alpha_quality 0.7 -vtag hvc1 -an "${BASE}_test2.mp4"
if [ $? -eq 0 ]; then
    echo "✓ Test 2 successful"
    ls -lh "${BASE}_test2.mp4"
else
    echo "✗ Test 2 failed"
fi
echo ""

# Test 3: MOV container instead of MP4
echo "Test 3: HEVC with MOV container..."
ffmpeg -y -c:v libvpx-vp9 -i "$INPUT" -c:v hevc_videotoolbox -q:v 60 -allow_sw 1 -alpha_quality 0.7 -vtag hvc1 "${BASE}_test3.mov"
if [ $? -eq 0 ]; then
    echo "✓ Test 3 successful"
    ls -lh "${BASE}_test3.mov"
else
    echo "✗ Test 3 failed"
fi
echo ""

# Test 4: Check if any files actually have alpha
echo "=== Checking results with ffprobe ==="
for file in "${BASE}_test"*.{mp4,mov}; do
    if [ -f "$file" ]; then
        echo "--- $file ---"
        ffprobe -v quiet -select_streams v:0 -show_entries stream=codec_name,pix_fmt -of csv=p=0 "$file"
    fi
done

echo ""
echo "=== Manual Test ==="
echo "Try opening the test files in Safari/QuickTime to check for transparency"
