-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobs-virtual-camera-toggle.sh
More file actions
executable file
·185 lines (147 loc) · 4.38 KB
/
obs-virtual-camera-toggle.sh
File metadata and controls
executable file
·185 lines (147 loc) · 4.38 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/bin/bash
# Virtual Camera Toggle with RVM Background Removal
# Press Super+O to start/stop OBS virtual camera with AI background removal
#
# Flow: Webcam → RVM (background removal) → OBS (virtual cam output)
set -e
LOCK_FILE="/tmp/obs-virtual-camera.lock"
RVM_LOCK_FILE="/tmp/rvm-camera.lock"
NOTIFICATION_ICON="camera-web"
# RVM Camera settings (set RVM_CAMERA_SCRIPT env var to override)
RVM_SCRIPT="${RVM_CAMERA_SCRIPT:-rvm_camera.py}"
RVM_BACKGROUND="0,255,0" # Chroma key green for OBS
# Function to show notification
notify() {
local title="$1"
local message="$2"
local icon="${3:-$NOTIFICATION_ICON}"
notify-send -i "$icon" "$title" "$message" 2>/dev/null || true
}
# Check if OBS is running
is_obs_running() {
pgrep -x "obs" > /dev/null 2>&1
}
# Check if RVM camera is running
is_rvm_running() {
pgrep -f "rvm_camera.py" > /dev/null 2>&1
}
# Check if virtual camera is active
is_virtual_camera_active() {
fuser /dev/video10 2>/dev/null | grep -q "." 2>/dev/null
}
# Get OBS PID from lock file or pgrep
get_obs_pid() {
if [ -f "$LOCK_FILE" ]; then
cat "$LOCK_FILE"
else
pgrep -x "obs" | head -1
fi
}
# Get RVM PID from lock file or pgrep
get_rvm_pid() {
if [ -f "$RVM_LOCK_FILE" ]; then
cat "$RVM_LOCK_FILE"
else
pgrep -f "rvm_camera.py" | head -1
fi
}
# Start RVM camera (background removal)
start_rvm_camera() {
echo "Starting RVM background removal..."
# Start RVM camera in background
python3 "$RVM_SCRIPT" --background "$RVM_BACKGROUND" > /tmp/rvm-camera.log 2>&1 &
RVM_PID=$!
# Save PID to lock file
echo "$RVM_PID" > "$RVM_LOCK_FILE"
# Wait for RVM to initialize (model loading takes ~2-3 seconds)
echo "Waiting for RVM to initialize..."
for i in {1..10}; do
if is_virtual_camera_active; then
echo "RVM camera ready (PID: $RVM_PID)"
return 0
fi
sleep 0.5
done
echo "Warning: RVM camera may not be fully ready"
return 0
}
# Stop RVM camera
stop_rvm_camera() {
echo "Stopping RVM camera..."
RVM_PID=$(get_rvm_pid)
if [ -n "$RVM_PID" ]; then
kill -15 "$RVM_PID" 2>/dev/null || true
# Wait for graceful shutdown
for i in {1..6}; do
if ! kill -0 "$RVM_PID" 2>/dev/null; then
break
fi
sleep 0.5
done
# Force kill if still running
if kill -0 "$RVM_PID" 2>/dev/null; then
kill -9 "$RVM_PID" 2>/dev/null || true
fi
fi
# Also kill any orphaned RVM processes
pkill -f "rvm_camera.py" 2>/dev/null || true
rm -f "$RVM_LOCK_FILE"
echo "RVM camera stopped"
}
# Start OBS with virtual camera
start_virtual_camera() {
echo "Starting virtual camera system..."
# First, start RVM for background removal
start_rvm_camera
# Give RVM a moment to fully initialize
sleep 1
# Start OBS minimized to tray with virtual camera enabled
echo "Starting OBS..."
/usr/bin/obs --startvirtualcam --minimize-to-tray &
OBS_PID=$!
# Save PID to lock file
echo "$OBS_PID" > "$LOCK_FILE"
# Wait a moment for OBS to start
sleep 2
notify "Virtual Camera Started" "RVM + OBS running (background removal active)"
echo "Virtual camera started (OBS PID: $OBS_PID)"
}
# Stop OBS virtual camera
stop_virtual_camera() {
echo "Stopping virtual camera system..."
# Stop OBS first
OBS_PID=$(get_obs_pid)
if [ -n "$OBS_PID" ]; then
# Send SIGTERM for graceful shutdown
kill -15 "$OBS_PID" 2>/dev/null || true
# Wait up to 3 seconds for graceful shutdown
for i in {1..6}; do
if ! kill -0 "$OBS_PID" 2>/dev/null; then
break
fi
sleep 0.5
done
# Force kill if still running
if kill -0 "$OBS_PID" 2>/dev/null; then
echo "Force killing OBS..."
kill -9 "$OBS_PID" 2>/dev/null || true
fi
fi
rm -f "$LOCK_FILE"
# Then stop RVM
stop_rvm_camera
notify "Virtual Camera Stopped" "RVM + OBS have been closed"
echo "Virtual camera system stopped"
}
# Main toggle logic
main() {
if is_obs_running || is_rvm_running; then
# Something is running - stop everything
stop_virtual_camera
else
# Nothing is running - start everything
start_virtual_camera
fi
}
# Run main function
main