#!/bin/sh
#
# RJP 30.9.2025 Desktop recorder with audio for FreeBSD
# Packages sox, libsoxr, virtual_oss, yad, xterm, espeak-ng and ffmpeg must be installed
# 2.10.2025 espeak-ng added for testing virtual_os
# 19.7.2026 New dialogs and buttons
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" --button="RECORD VIDEO":0 --button=ABORT:1 --text="INSTRUCTION:
	
When recording video, minimize these messages and Terminal output if you do not want them to see into video

An altenative way to abort recording to stop it via Terminal using command 

pkill ffmpeg
pkill sox

"
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 | sox -r 48k -b 16 -v 0.99 -t oss /dev/dsp -t wav /tmp/video-$time.mp3 " & \
#
############################################################################################################
#### DEBUG SECTION: uncomment the next 4 lines below if wanting to see what happens with ffmpeg and sox ####
############################################################################################################
# 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 --button="FINISH RECORDING":0 --title="Stop Recording" --text="Stop video recording pressing FINISH 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
