#!/bin/sh

############
# TODO:
############
# - audio format RAW possible? might be preferrable over WAV
# - remove temporary files after muxing?
# - test how far can push video size
# - allow substituting aucat with custom other audio recording for portability?
# - check if ffmpeg and aucat available in path
# - if -m works, set muxflag=on as default
# - don't set audiostream (-a flag) to snd/0.mon because not enabled by default?
# - check with regex that format of -r (resolution) is correct? (shouldn't this
#   be left to ffmpeg)
# - test if position can be added to resolution (-r) argument? (like 640x480+48+48)
# - add mention of $DISPLAY variable for the recording?
# - AFAIK '-preset ultrafast' is specific to libx264 and needs to be disabled
#   for use of other codecs
# - get default framerate from xrandr? same for resolution - default to desktop
#   resolution?
# - mention need to stop recording with Ctrl-C??
# - don't check for filename if -m (muxflag=off)
# - add flag to skip recording and only mux (to adjust volume ratio, for example)
# - add highpass=f=200,lowpass=f=3000
# - HIGH PRIORITY: replace libfdk_aac!
###########

USAGE="Usage: `basename $0` [-m] [-r resolution] [-a stream] [-c codec] [-p path] [-f framerate] [-v0 volume_adjustment] [-v1 volume_adjustment] file"

# Parse arguments based on http://www.shelldorado.com/goodcoding/cmdargs.html

filename=
resolution=
audiostream=
videocodec=
streampath=
volume1=
volume2=
framerate=
muxflag=on
hilopass=

if [[ $# -eq 0 ]];then
	echo $USAGE; exit 0
fi

while [[ $# -gt 0 ]]
do
	case "$1" in
		-m) muxflag=off;;
		-r) resolution="$2"; shift;;
		-a) audiostream="$2"; shift;;
		-c) videocodec="$2"; shift;;
		-p) streampath="$2"; shift;;
		-v0) volume0="$2"; shift;;
		-v1) volume1="$2"; shift;;
		-f) framerate="$2"; shift;;
		-hl) hilopass=",highpass=f=200,lowpass=f=3000";;
		-h) echo $USAGE; exit 0;;
		-*) echo $USAGE; exit 0;;
		*) filename="$1"; break;;
	esac
	shift
done

# set defaults if not specified in arguments
if [[ -z "$streampath" ]];then
	streampath="/tmp"
fi
if [[ -z "$resolution" ]];then
	resolution=640x480
fi
if [[ -z "$framerate" ]];then
	framerate=60
fi
# FIXME: find a way that -a not needed
if [[ -z "$audiostream" ]];then
	echo "IN DEVELOPMENT: -a argument needed at the moment (e.g. '-a snd/0.mon')"; exit 1
fi
if [[ -z "$videocodec" ]];then
	videocodec=libx264
fi
if [[ -z "$filename" ]];then
	echo $USAGE; exit 0
fi
if [[ -z "$volume0" ]];then
	volume0=1.0
fi
if [[ -z "$volume1" ]];then
	volume1=1.0
fi

# remove temp files in case they exist (from prior attempts)
rm $streampath/fauxrec_{v0.mp4,a{0,1}.wav}

# record the streams
# 'sh -i' makes it so that the aucat and ffmpeg jobs receive SIGINT rather than
# this running script
sh -i -c "aucat -f snd/0 -o \"$streampath/fauxrec_a0.wav\" | \
	aucat -f $audiostream -o \"$streampath/fauxrec_a1.wav\" | \
	ffmpeg -f x11grab -video_size $resolution -framerate $framerate \
	-i $DISPLAY -c:v $videocodec -preset ultrafast \
	$streampath/fauxrec_v0.mp4"

echo ""
echo "*** Recording complete. ***"
echo ""

# multiplex the separate streams
if [[ $muxflag = on ]];then
	echo "Multiplexing streams to $filename..."
	echo $hilopass
	echo ""
	ffmpeg -i "$streampath/fauxrec_v0.mp4" -i "$streampath/fauxrec_a0.wav" \
		-i "$streampath/fauxrec_a1.wav" -filter_complex \
		"[1]volume=$volume0,aformat=channel_layouts=stereo$hilopass[l];[2]volume=$volume1,aformat=channel_layouts=stereo[m];[l][m]amerge=inputs=2[a]" \
		-map '0:v:0' -map '[a]' -c:v copy -c:a aac -q:a 2 -ac 2 \
		$filename
	if [[ $? -eq 0 ]];then
		echo ""
		echo "*** Multiplexing complete. ***"
	else
		echo ""
		echo "*** Multiplexing failed. ***"
	fi
	echo ""
fi
