#!/bin/bash

# generates screenshots per X frame from all the videos in the selected directory

# usage: sas [scan_dir=$pwd] [interval=10] [output_dir=snaps]

source "/usr/local/bin/colors"

COUNT=0
INTERVAL=10
OUTPUT="snaps"
CURRENT="$PWD"

if [[ $# -gt 0 ]] ; then
	CURRENT="$1"
fi

if [[ $# -gt 1 ]] ; then
	INTERVAL=$2
fi

if [[ $# -gt 2 ]] ; then
	OUTPUT=$3
fi

echo "Selected: '$(echoyellow "$CURRENT")' .."

sleep 5

snap () {
	if [ ! -d "$OUTPUT" ]; then
		mkdir -p "$OUTPUT"
	fi

	((COUNT++))

	FILENAME=$(basename -- "$1")

	ffmpeg -i "$1" -vf fps=1/"$INTERVAL" "$OUTPUT/$COUNT-$FILENAME-%03d.jpg"
}

for FILE in "$CURRENT/"*; do 
	if [ -f "$FILE" ]; then
		EXTENSION="${FILE##*.}"

		case "${EXTENSION,,}" in
			"mp4"|"avi"|"mkv"|"webm")
				snap "$FILE"
				;;
		esac
	fi 
done