summaryrefslogtreecommitdiff
path: root/scripts/dmenurecord
blob: cc3956727936546169965f8e80b4896433af6450 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/sh
# dmenu for recordings with ffmpeg
# dependencies: pipewire-pulse/pulseaudio, ffmpeg, dmenu, slop
# Original: https://github.com/whoisYoges/record-with-ffmpeg

set -e

videodir="${XDG_VIDEOS_DIR:-$HOME/Videos}/ffmpeg/"
audiodir="$videodir"
title="$(date +ffmpeg-"%Y-%m-%d-%H-%M-%S")"
recordingresolution="$(xrandr | sed -n 's/\s*\([0-9]\+x[0-9]\+\).*\*.*/\1/p')"
outputresolution="$recordingresolution"
display="$DISPLAY"
fps="30"
crf="22" # 18
codecvideo="libx264"
codecaudio="libmp3lame"
preset="veryfast"
videoext="mp4"
audioext="wav"

# use pactl list source to check the infos
monitoraudioinput="alsa_output.pci-0000_00_1b.0.analog-stereo.monitor"
microphoneaudioinput="alsa_input.pci-0000_00_1b.0.analog-stereo"
monitoraudiochannel="2"
microphoneaudiochannel="2"
monitoraudiofrequency="48000"
microphoneaudiofrequency="48000"
outputaudiofrequency="44100"

internalAudioOnly() {
	ffmpeg \
		-f pulse -ac "$monitoraudiochannel" -ar "$monitoraudiofrequency" -i "$monitoraudioinput" \
		-acodec "$codecaudio" -ar "$outputaudiofrequency" -q:a 1 "$title.$audioext"
}

microphoneOnly() {
	ffmpeg \
		-f pulse -ac "$microphoneaudiochannel" -ar "$microphoneaudiofrequency" -i "$microphoneaudioinput" \
		-acodec "$codecaudio" -ar "$outputaudiofrequency" -q:a 1 "$title.$audioext"
}

internalAudioAndMicrophone() {
	ffmpeg \
		-f pulse -ac "$monitoraudiochannel" -ar "$monitoraudiofrequency" -i "$monitoraudioinput" \
		-f pulse -ac "$microphoneaudiochannel" -ar "$microphoneaudiofrequency" -i "$microphoneaudioinput" \
		-filter_complex amix=inputs=2 \
		-acodec "$codecaudio" -ar "$outputaudiofrequency" -q:a 1 "$title.$audioext"
}

videoWithoutAudio() {
	ffmpeg \
		-f x11grab -r "$fps" -s "$recordingresolution" -i "$display" \
		-vcodec "$codecvideo" -preset "$preset" -crf "$crf" \
		-pix_fmt yuv420p -s "$outputresolution" "$title.$videoext"
}

videoWithInternalAudio() {
	ffmpeg \
		-f pulse -ac "$monitoraudiochannel" -ar "$monitoraudiofrequency" -i "$monitoraudioinput" \
		-f x11grab -r "$fps" -s "$recordingresolution" -i "$display" \
		-vcodec "$codecvideo" -preset "$preset" -crf "$crf" \
		-acodec "$codecaudio" -ar "$outputaudiofrequency" -q:a 1 \
		-pix_fmt yuv420p -s "$outputresolution" "$title.$videoext"
}

videoWithMicrophone() {
	ffmpeg \
		-f pulse -ac "$microphoneaudiochannel" -ar "$microphoneaudiofrequency" -i "$microphoneaudioinput" \
		-f x11grab -r "$fps" -s "$recordingresolution" -i "$display" \
		-vcodec "$codecvideo" -preset "$preset" -crf "$crf" \
		-acodec "$codecaudio" -ar "$outputaudiofrequency" -q:a 1 \
		-pix_fmt yuv420p -s "$outputresolution" "$title.$videoext"
}

videoWithMicrophoneAndInternalAudio() {
	ffmpeg \
		-f pulse -ac "$monitoraudiochannel" -ar "$monitoraudiofrequency" -i "$monitoraudioinput" \
		-f pulse -ac "$microphoneaudiochannel" -ar "$microphoneaudiofrequency" -i "$microphoneaudioinput" \
		-filter_complex amix=inputs=2 \
		-f x11grab -r "$fps" -s "$recordingresolution" -i "$display" \
		-vcodec "$codecvideo" -preset "$preset" -crf "$crf" \
		-acodec "$codecaudio" -ar "$outputaudiofrequency" -q:a 1 \
		-pix_fmt yuv420p -s "$outputresolution" "$title.$videoext"
}

audio() {
	choice=${1:-$(printf "Quit
1 Internal audio
2 Microphone
3 Both" | dmenu -i -p "Audio")}

	case $choice in
		Quit) return 0 ;;
		1*) cd "$audiodir" && internalAudioOnly ;;
		2*) cd "$audiodir" && microphoneOnly ;;
		3*) cd "$audiodir" && internalAudioAndMicrophone ;;
		*) return 1 ;;
	esac
}

video() {
	choice=${1:-$(printf "Quit
1 Without audio
2 With internal audio
3 With mic
4 With both" | dmenu -i -p "Video")}

	case $choice in
		Quit) return 0 ;;
		1*) cd "$videodir" && videoWithoutAudio ;;
		2*) cd "$videodir" && videoWithInternalAudio ;;
		3*) cd "$videodir" && videoWithMicrophone ;;
		4*) cd "$videodir" && videoWithMicrophoneAndInternalAudio ;;
		*) return 1 ;;
	esac
}

setSelectionVal() {
	X=$1 Y=$2 W=$3 H=$4
	[ $((W % 2)) -eq 1 ] && W=$((W + 1))
	[ $((H % 2)) -eq 1 ] && H=$((H + 1))
	recordingresolution="$W"x"$H"
	outputresolution="$recordingresolution"
	display="$display"+"$X","$Y"
}

videosel() {
	slop=$(slop -q -f "%x %y %w %h") || return 1
	# shellcheck disable=SC2086
	setSelectionVal $slop
	video "$1"
}

main() {
	[ ! -d "$audiodir" ] && mkdir -p "$audiodir"
	[ ! -d "$videodir" ] && mkdir -p "$videodir"

	choice=${1:-$(printf "Quit
1 Audio
2 Video
3 Video Selection" | dmenu -i -p "Mode")}

	case $choice in
		Quit) return 0 ;;
		1*) audio "$2" ;;
		2*) video "$2" ;;
		3*) videosel "$2" ;;
		*) return 1 ;;
	esac
}

main "$@"