#!/bin/bash

# Source folder for videos
VIDEO_DIR="$HOME/gifsite/videos"
# Output folder for GIFs
GIF_DIR="$HOME/gifsite"

mkdir -p "$VIDEO_DIR"

# Loop over all mp4/webm/mov files in VIDEO_DIR
for vid in "$VIDEO_DIR"/*.{mp4,webm,mov}; do
    [ -e "$vid" ] || continue  # skip if no files
    filename=$(basename "$vid")
    name="${filename%.*}"
    gif_path="$GIF_DIR/$name.gif"

    # Skip if GIF already exists
    [ -f "$gif_path" ] && continue

    echo "Converting $vid → $gif_path ..."

    # Convert to GIF
    ffmpeg -y -i "$vid" -vf "fps=15,scale=480:-1:flags=lanczos" -loop 0 "$gif_path"

done

# Regenerate the items.js for the gallery
cd "$GIF_DIR"
./generate_items.sh

echo "Done!"
