Files
Notarius/reference/call-recorder.sh
T

54 lines
1.6 KiB
Bash

#!/usr/bin/env bash
# ~/bin/call-recorder.sh
set -euo pipefail
REC_DIR="$HOME"
PIDFILE="/tmp/call-recorder.pid"
LOGFILE="/tmp/call-recorder.log"
start_recording() {
if [[ -f "$PIDFILE" ]] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then
notify-send "Call Recorder" "Läuft bereits (PID $(cat "$PIDFILE"))"
exit 1
fi
local sink source outfile
sink="$(pactl get-default-sink).monitor"
source="$(pactl get-default-source)"
outfile="${REC_DIR}/call_$(date +%Y-%m-%d_%H-%M-%S).mp3"
ffmpeg -nostdin -y \
-f pulse -i "$sink" \
-f pulse -i "$source" \
-filter_complex "[0:a]pan=mono|c0=0.5*c0+0.5*c1[browser];[1:a]pan=mono|c0=c0[mic];[browser][mic]amerge=inputs=2[aout]" \
-map "[aout]" -ac 2 -c:a libmp3lame -q:a 2 \
"$outfile" \
> "$LOGFILE" 2>&1 &
echo $! > "$PIDFILE"
notify-send "Call Recorder" "Aufnahme gestartet: $(basename "$outfile")"
}
stop_recording() {
if [[ ! -f "$PIDFILE" ]]; then
notify-send "Call Recorder" "Keine laufende Aufnahme."
exit 1
fi
local pid
pid="$(cat "$PIDFILE")"
kill -INT "$pid" 2>/dev/null || true
# ffmpeg braucht einen Moment, um den MP3-Frame sauber abzuschließen
for _ in {1..20}; do
kill -0 "$pid" 2>/dev/null || break
sleep 0.2
done
rm -f "$PIDFILE"
notify-send "Call Recorder" "Aufnahme gestoppt."
}
case "${1:-toggle}" in
start) start_recording ;;
stop) stop_recording ;;
toggle) if [[ -f "$PIDFILE" ]]; then stop_recording; else start_recording; fi ;;
*) echo "Usage: $0 {start|stop|toggle}"; exit 1 ;;
esac