#!/bin/sh
#
# RJP 30.9.2025 Desktop recorder with audio
# Packages pulseaudio, yad, xterm, pipewire-pulse, wireplumber and ffmpeg must be installed
# 16.6.2026 New version with pipewire-pulse
time=$(date "+%F-%H-%M-%S")
AUDIO="$(pacmd list-sources | grep -PB 1 "analog.*monitor>" | head -n 1 | perl -pe 's/.* //g')"
pulseaudio -D &

yad --center --width=300 --height=100 --text-align=center --button=OK:0 --title="Record video" --text="Record video"
if [ $? = 0 ];
then
xterm -e "ffmpeg -threads 1 -f x11grab -video_size $(xdpyinfo | grep 'dimensions:' | awk '{print $2;}') -i $DISPLAY -f pulse -ac 2 -i "$AUDIO" -c:v libx264rgb -crf 0 -preset ultrafast video$time.avi " & \
#
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 --button=OK:0 --text="INFO: $(cat /tmp/message.txt)"
#
# Stop recording via yad dialog
#
yad --center --width=300 --height=100 --text-align=center --button=OK:0 --title="Stop Recording" --text="Stop Recording"
if [ $? = 0 ];
then
killall --user $USER --ignore-case --signal INT ffmpeg
else exit 0
fi
fi
sleep 30
cd /tmp && rm message.txt 
