#!/bin/sh
#
# RJP 2.10.2025 Desktop recorder with audio for FreeBSD
# Packages virtual_oss, yad, xterm, espeak-ng and ffmpeg must be installed
# version 2 where sox is not required
# 2.10.2025 espeak-ng added for testing virtual_os
time=$(date "+%F-%H-%M")
yad --form --center --columns=1 --rows=3 --width=350 --title="Enable virtual_oss for audio recording" --text="         Enable virtual_oss for audio recording" \
\
--field="Load virtual os for recording (if not loaded)":fbtn "xterm -e sh -c 'sudo virtual_oss -Q 0 -C 2 -c 2 -r 48000 -b 16 -s 1024 -P /dev/dsp0 -R /dev/null -w vdsp.wav -l dsp  ; exec sh' & " \
--field="Test virtual oss":fbtn "xterm -e sh -c 'espeak-ng 'virtual oss is loaded succeesfully, you can start recording' ; exec sh' & " \
--button=Exit:1

yad --center --width=300 --height=100 --text-align=center --title="Record video" --text="Record video"
if [ $? = 0 ];
then
xterm -e "ffmpeg -f x11grab -video_size $(xdpyinfo | grep 'dimensions:' | awk '{print $2;}') -i $DISPLAY -c:v libx264 -preset ultrafast /tmp/video-$time.avi | ffmpeg -f oss -i /dev/dsp -f mp3 /tmp/video-$time.mp3 " & \
#
sleep 2 && (ps -ef|grep -v grep|grep ffmpeg >/dev/null && echo "ffmpeg is running ... stop recording closing the Terminal. You can close this message" || echo "ffmpeg is not running, you probably has the wrong audio or video device. You can close this message") > /tmp/message.txt
yad --center --width=700 --height=200 --text-align=center --text="INFO: $(cat /tmp/message.txt)"
#
# Stop recording via yad dialog
#
yad --center --width=300 --height=100 --text-align=center --title="Stop Recording" --text="Stop Recording"
if [ $? = 0 ];
then
killall xterm
ffmpeg -i /tmp/video-$time.avi -i /tmp/video-$time.mp3 -c copy video-$time.mkv
else exit 0
fi
fi
sleep 30
cd /tmp && rm message.txt video-$time.avi video-$time.mp3
