#!/bin/bash


# Where screenshots are saved
SC_ROOT="model_data/current"

# WILL BE DELETED
OUTPUT_DIR="model_data/video_output"


# To make with fade in and out:
# ffmpeg -framerate 30 -i %03d.png -vf "scale=1024x1024:flags=neighbor,fade=in:0:45,fade=out:1040:45" out.webm

render_dir () {
	ffmpeg \
		-y \
		-loglevel quiet \
		-framerate 30 \
		-i $1/hackcel_%003d.png \
		-c:v libx264 \
		-pix_fmt yuv420p \
		$OUTPUT_DIR/${1##*/}.mp4
}



# Todo: error out if exists
mkdir -p $OUTPUT_DIR


echo "Making episode files..."
for D in $SC_ROOT/screenshots/*; do
	if [ -d "${D}" ]; then
		render_dir $D
	fi
done

echo "Done."


# Generate video for each run
for f in $OUTPUT_DIR/*.mp4; do
	echo file \'$f\' >> video_merge_list
done

# Merge videos
ffmpeg \
	-loglevel error -stats -y \
	-f concat \
	-safe 0 \
	-i video_merge_list \
	-vf "scale=1024x1024:flags=neighbor" \
	$OUTPUT_DIR/00-all.mp4

rm video_merge_list

# Make accelerated video
ffmpeg \
	-loglevel error -stats -y \
	-i $OUTPUT_DIR/00-all.mp4 \
	-framerate 60 \
	-filter:v "setpts=0.125*PTS" \
	$SC_ROOT/8x.mp4

echo "Cleaning up..."
rm -dr $OUTPUT_DIR
