summaryrefslogtreecommitdiff
path: root/xrec
blob: 2008c6ff1cdc7f062f3aa93a35d8e353b530ad19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash

set -e

recordingPATH="$HOME/media/screenrecords"
PID_FILE="/tmp/ffmpeg_screenrecord.pid"

mkdir -p "$recordingPATH"

# Detect NVENC support; fallback to fast CPU encoding if no NVIDIA GPU is detected
if command -v nvidia-smi &> /dev/null && nvidia-smi &> /dev/null; then
    ENCODER_FLAGS="-c:v h264_nvenc -preset p6 -tune hq -rc constqp -qp 17"
else
    ENCODER_FLAGS="-c:v libx264 -preset ultrafast -crf 17" # or medium/slow to be on par with p6 but ehh
fi

case $1 in

    start)
        if [ -f "$PID_FILE" ] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null; then
            dunstify -t 3000 -i /dev/null "⚠️ Recording is already running!"
            exit 1
        fi

        dunstify -t 3000 -i /dev/null " Recording Starts In 3 Seconds"
        sleep 3

        TIMESTAMP=$(date +'%Y-%m-%d_%H-%M-%S')
        OUTPUT_FILE="${recordingPATH}/screenrecording_${TIMESTAMP}.mp4"

        # Standard yuv420p conversion allows players to auto-expand blacks properly
        ffmpeg -video_size 1920x1080 -framerate 60 -f x11grab -i :0.0+0,0 \
            $ENCODER_FLAGS -profile:v high -pix_fmt yuv420p -movflags +faststart \
            "$OUTPUT_FILE" >/dev/null 2>&1 &

        echo $! > "$PID_FILE"
        ;;

    stop)
        if [ -f "$PID_FILE" ]; then
            PID=$(cat "$PID_FILE")
            if kill -0 "$PID" 2>/dev/null; then
                kill -SIGINT "$PID"
                rm -f "$PID_FILE"
                dunstify -t 3000 -i /dev/null " Recording Finished"
            else
                dunstify -t 3000 -i /dev/null "⚠️ Process not running."
                rm -f "$PID_FILE"
            fi
        else
            dunstify -t 3000 -i /dev/null "⚠️ No active recording."
        fi
        ;;

    *)
        echo "Usage: $0 {start|stop}"
        exit 1
        ;;

esac