#!/bin/bash

# Find directories that contain video.mkv and process each
find . -type d -exec sh -c '
  dir="$1"
  video="$dir/video.mkv"
  if [ -f "$video" ]; then
    echo "Processing $video in $dir"
    
    # Get duration in seconds
    duration=$(ffprobe -i "$video" -show_entries format=duration -v quiet -of csv="p=0")
    if [ $? -ne 0 ] || [ -z "$duration" ]; then
      echo "Error getting duration for $video"
      exit 1
    fi
    
    # Check if duration > 10 seconds
    if awk "BEGIN {exit !($duration > 10)}"; then
      ss=10
    else
      ss=0
    fi
    
    ffmpeg -i "$video" -ss $ss -vframes 1 "$dir/poster.jpg"
    if [ $? -eq 0 ]; then
      echo "Poster created for $dir"
    else
      echo "Error creating poster for $dir"
    fi
  fi
' _ {} \;
