#!/usr/bin/env zsh

# Check if required arguments are provided
if [[ $# -lt 2 ]]; then
    echo "Usage: $0 <input_file> <segment_duration>"
    echo "Example: $0 effects-raw.mp4 2"
    exit 1
fi

# Get arguments
INPUT=$1
SEGMENT_LENGTH=$2

# Verify input file exists
if [[ ! -f $INPUT ]]; then
    echo "Error: Input file '$INPUT' not found"
    exit 1
fi

# should match the order they appear in examples/effect-showcase.rs
effects=(
    fade_from
    fade_to
    fade_from_fg
    fade_to_fg
    coalesce
    coalesce_from
    dissolve
    dissolve_to
    sweep_in
    sweep_out
    slide_in
    slide_out
    hsl_shift
    hsl_shift_fg
    parallel_fx
    sequence_fx
    delay
    never_complete
    ping_pong
    prolong_start
    prolong_end
    effect_fn
    effect_fn_buf
)

# create output directory if it doesn't exist
mkdir -p segments

# split the video into segments
for i in {1..$#effects}; do
    effect=$effects[$i]
    start_time=$(( (i-1) * SEGMENT_LENGTH ))
    
    echo "Processing $effect..."
    
    # Extract segment and create both MP4 and GIF
    ffmpeg -i "$INPUT" -ss $start_time -t $SEGMENT_LENGTH \
        -c:v libx264 -crf 23 \
        "segments/$effect.mp4"
        
#    ffmpeg -i "segments/$effect.mp4" \
#        -vf "fps=30,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" \
#        -loop 0 "segments/$effect.gif"
done