processed_intro_path=$PWD/out/intro-processed.mp4
processed_outro_path=$PWD/out/outro-processed.mp4

echo łączenie video z dźwiękiem:
ffmpeg -vaapi_device /dev/dri/renderD128 \
	-i out/video.mp4 -i $input_mono \
	-video_track_timescale $timescale \
	-tune stillimage \
	-c:a aac \
	-ac 1 \
	-strict experimental \
	-vf 'format=nv12,hwupload' \
	-c:v h264_vaapi \
	out/video-and-audio.mp4

echo reencoding intro to enable fast concat...

function reencode_for_demux_compatibility() {
	ffmpeg -i "$1" \
		-pix_fmt yuv420p \
		-r $framerate \
		-video_track_timescale $timescale \
		-ac 1 \
		-b:a 320k \
		"$2"

}

reencode_for_demux_compatibility "$intro" "$processed_intro_path"

if [ "$intro" = "$outro" ]; then
	processed_outro_path="$processed_intro_path"
else
	reencode_for_demux_compatibility "$outro" "$processed_outro_path"
fi

echo "" >out/demuxer-branding.txt
echo "file $processed_intro_path" >>out/demuxer-branding.txt
echo "file video-and-audio.mp4" >>out/demuxer-branding.txt
echo "file $processed_outro_path" >>out/demuxer-branding.txt

cat out/demuxer-branding.txt

ffmpeg -f concat -safe 0 -i out/demuxer-branding.txt -c:v copy -b:a 320k "$final_output"
