summaryrefslogtreecommitdiff
path: root/bin/guiscripts/vrec
blob: e16679d4362bab03387cf553bdc82abcc835963a (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/sh

# Quick video capture with dmenu
#
# dependencies: 
#   - ffmpeg
# 	- hacksaw: part
# 	- xdotool: active
# 	- xdg-user-dir
#   - dmenu
#   - clipp
#
# Usage: 
# vrec [command] [dir]
# - if you do not provide a command it will launch dmenu.
# - if you provide command and dir it will save the recording in dir
# Commands:
# - active: capture the active window
# - part: select a part of the screen to record
# - stop: stop an active recording
# - full: record your full screen
# - last: copy the latest recording's path to the clipboard
# - audio: one of the prior recording commands but with sound
# Recording:
# there is a lock file

# $1: x
# $2: y
# $3: width
# $4: height
# $5: output dir
# $6: output name
record_cmd()
{
	if [ -f "$lock" ] 
	then
		>&2 printf 'already recording, please stop recording first\n'
		exit 1
	else
		touch "$lock"
	fi
	printf '%s\n' "$5/$6.mp4"

	herbe "vrec" "started recording." # use notification to know when recording started
	w=$(($3 + $3 % 2))
	h=$(($4 + $4 % 2))
	ffmpeg $audio        \
            -hide_banner     \
	    -r 30            \
	    -f x11grab       \
	    -s "${w}x${h}"   \
	    -i ":0.0+$1,$2"  \
	    -preset ultrafast\
	    -c:v libx264     \
	    -pix_fmt yuv420p \
	    "$5/$6.mp4"
	rm -f "$lock"
	herbe "vrec" "stopped recording." & 
}

lock="/tmp/record.lock"
# Get recording directory
if [ -d "$1" ]
then
	dir="$1" 
	shift
else 
	dir="$(xdg-user-dir VIDEOS)"
	[ -d "$dir" ] && dir="$dir/records" || dir="$HOME"
fi
mkdir -p "$dir"

# Set audio variable
if [ "$1" = "-a" ]
then
    if [ "$2" = "-m" ]
    then
        ## Mic
        audio="-f pulse -ac 2 -i default"
        shift
    else
        ## Desktop
        audio="-f pulse -i alsa_output.pci-0000_00_1f.3.analog-stereo.monitor -ac 1"
    fi
    shift
fi
output="$(date +%F_%H-%M-%S)"

if [ -z "${option:="$1"}" ]; then
    option="$(printf 'active\npart\nstop\nfull\naudio\nlast\n' | dmenu -c)"
fi
[ "$option" ] || exit 1

case "$option" in
    active)
        record_cmd $(xwininfo -id "$(xdotool getactivewindow)" |
            sed '/Absolute\|Width:\|Height:/!d;s/.*:\s*//' | tr '\n' ' ') "$dir" "$output"
        ;;
    part) 
        hacksaw | {
            IFS=+x read -r w h x y
            [ "$x" ] || exit 1 # quit on ESC
            record_cmd $x $y $w $h "$dir" "$output"
        }
        ;;
    stop)
        pid="$(pgrep ffmpeg | xargs ps | grep 'x11grab' | awk '{print $1}')"
        [ "$pid" ] && kill -TERM $pid
        rm -f "$lock" 
        herbe "vrec" "stopped recording." &
        ;;
    last) 
        file="$(find "$dir" -type f -iname '*.mp4' -printf '%Ts %p\n' | sort -n -r | head -n 1 | cut -f 2- -d' ')"
        printf '%s' "$file" | clipp
        ;;
    full) record_cmd 0 0 1920 1080 "$dir" "$output" ;;
    audio) 
    	choice="$(printf 'mic\ndesktop\n' | dmenu -c)"
    	[ "$choice" ] || exit 1
    	if [ "$choice" = "desktop" ]
    	then
	    $0 -a
	else
            $0 -a -m
	fi
        exit
        ;;
    help|*) >&2 printf 'record [dir] (active|part|stop|full|last|audio)\n' ;;
esac