# Author: Theodore Zacharia
# V0.1: 02/10/2023 - Initial Release
# V0.2: 06/10/2023 - Handle spaces in filenames
# V0.3: 19/10/2023 - Added echo of framecount of original file so can see how far its got
# V0.4: 03/11/2023 - Add echo of (current / total) to keep track of how progressing
#
# This script provides simple interface for processing a bunch of videos and
# making them smaller in side but not loosing too much quality
#
#

# *** Globals
TRACE=0
FINDBIGFILES=0
DELETEORIGINAL=0
MINFILESIZE=600
INPUTFILE=""
T1=$$.tmp


# *** Functions
_usage()
{
	echo "usage: $0 [-h] [-t] [-f|-F] [-D] [-s size] [-i inputfile] <positional_params>"
	echo "where"
	cat <<- _EOF_
	  -h        Displays this help message
	  -t        Set trace on
	  -f        Find big files, provide a path to look in
	  -F        Like -f but provides size info as well
	  -D        Delete the original file when done
	  -s sizeMB set min file size to look for (in MB)
	  -i file   takes the files to modify from the input file and not the args
_EOF_
}


# *** Mainline

# process input parameters
while getopts thfFDs:i: AOPT
do
case $AOPT in
	t) TRACE=1 ;; # set TRACE mode
	f) FINDBIGFILES=1 ;;
	F) FINDBIGFILES=2 ;;
	s) MINFILESIZE=$OPTARG ;;
	i) INPUTFILE=$OPTARG ;;
	D) DELETEORIGINAL=1 ;;
	h) _usage
	   exit 1 ;;
	*) # echo "-$OPTARG is an invalid option" >&2 # start getopts string with :
	   exit 2 ;;
esac
done

shift $((OPTIND-1))

if [ $TRACE -gt 0 ]
then
	echo "Starting at $(date)"
	echo "TRACE=$TRACE" >&2
	echo "positional_args=$@" >&2
fi

if [ $# -lt 0 ]
then
	_usage
	exit 1
fi

if [ $FINDBIGFILES -gt 0 ]
then
	# look for big files in the path provided
	if [ $FINDBIGFILES -gt 1 ]
	then
		find $1 -type f -size +${MINFILESIZE}M -ls
	else
		find $1 -type f -size +${MINFILESIZE}M
	fi
	exit 0
fi

# if we get this far, the filelist should be the files to shrink

if [ -n "$INPUTFILE" ]
then
	echo "Processing input file $INPUTFILE"
	cp $INPUTFILE $T1
else
	echo "Processing arguments"
	for AFILE in "$@"
	do
		echo "$AFILE" >> $T1
	done
fi

echo "Will shrink following files:"
cat $T1
TOTTODO=$(wc -l $T1 | awk '{print $1}')
CURRDOING=0

while read AFILE
do
	CURRDOING=$(expr $CURRDOING + 1)
	echo "Processing ($CURRDOING / $TOTTODO) $AFILE"

	# maintain image size but reduce file size
	BFILE=${AFILE%%.mp4}
	BFILE="${BFILE}_sm.mp4"
	FRAMECOUNT=$(ffprobe -select_streams v -show_streams "$AFILE" 2>/dev/null | grep nb_frames | sed -e 's/nb_frames=//')
	echo "shrinking $AFILE ($FRAMECOUNT frames) to $BFILE"
	ffmpeg -i "${AFILE}" -vcodec libx265 -crf 28 "${BFILE}" </dev/null
	EL=$?

	# make image half the size
	#ffmpeg -i $AFILE -vf "scale=trunc(iw/4)*2:trunc(ih/4)*2" -c:v libx265 -crf 28 half_the_frame_size.mkv

	#ffmpeg -i $AFILE -vf "scale=trunc(iw/6)*2:trunc(ih/6)*2" -c:v libx265 -crf 28 a_third_the_frame_size.mkv

	if [ $EL -eq 0 ]
	then
		if [ $DELETEORIGINAL -eq 1 ]
		then
			echo "Deleting Original"
			rm "$AFILE"
		fi
	fi

	echo "done ($CURRDOING / $TOTTODO) at $(date)"

done<$T1

rm -f $T1