#!/bin/sh
set

echo "[INFO] Recording to /storage/capture/${EVENT}/${ROOM} started…"

ffmpeg -y -nostdin -hide_banner -re \
    -thread_queue_size 512 -i tcp://localhost:11000?timeout=3000000 \
    -map 0:v -c:v:0 mpeg2video -pix_fmt:v:0 yuv420p -qscale:v:0 4 -qmin:v:0 4 -qmax:v:0 4 -keyint_min:v:0 5 -bf:v:0 0 -g:v:0 5 -me_method:v:0 dia -metadata:s:v:0 title="HD" \
    -map 0:a:0 -c:a s302m -metadata:s:a:0 title="Native" \
    -strict -2 \
    -flags +global_header \
    -f segment \
    -segment_time 180 \
    -strftime 1 \
    -segment_format mpegts \
    "/storage/capture/${EVENT}/${ROOM}-%Y-%m-%d_%H-%M-%S-$$.ts"

ffmpeg_error_code=$?
if [ "0" -ne "$ffmpeg_error_code" ]; then
   echo "[ERROR] Recording failed, ffmpeg exited $ffmpeg_error_code"
else
   echo "[WARN] Recording stopped!"
fi

exit $ffmpeg_error_code
