-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtualcam.py
More file actions
146 lines (118 loc) · 3.66 KB
/
virtualcam.py
File metadata and controls
146 lines (118 loc) · 3.66 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
#!/usr/bin/env python3
import subprocess
import os
import signal
import sys
PID_FILE = "/tmp/virtualcam.pid"
# ---------- Helpers ----------
def is_running():
if not os.path.exists(PID_FILE):
return False
try:
pid = int(open(PID_FILE).read())
os.kill(pid, 0)
return True
except:
return False
def start(bg=False):
if is_running():
print("Virtual camera is already running.")
return
cmd = [
"ffmpeg",
"-re",
"-f", "lavfi",
"-i", "testsrc=size=1280x1280:rate=1",
"-f", "v4l2",
"/dev/video0"
]
if bg:
# Background: suppress all FFmpeg output
p = subprocess.Popen(cmd, preexec_fn=os.setsid,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
open(PID_FILE, "w").write(str(p.pid))
print("Virtual camera started (background).")
return
# Foreground: show FFmpeg logs
p = subprocess.Popen(cmd, preexec_fn=os.setsid)
open(PID_FILE, "w").write(str(p.pid))
print("Virtual camera started (foreground). Press Ctrl+C to stop.")
try:
p.wait()
except KeyboardInterrupt:
stop()
def stop():
if not is_running():
print("Virtual camera is not running.")
return
pid = int(open(PID_FILE).read())
try:
os.killpg(pid, signal.SIGTERM)
except:
pass
if os.path.exists(PID_FILE):
os.remove(PID_FILE)
print("Virtual camera stopped.")
def status():
print("Virtual camera is ON" if is_running() else "Virtual camera is OFF")
def toggle():
if is_running():
stop()
else:
start(bg=True)
def help_menu():
print("""
Usage: virtualcam <command>
Commands:
start Start camera (foreground)
start --bg Start camera in background
stop Stop virtual camera
status Show camera status
toggle Turn ON if OFF, turn OFF if ON
help Show this help menu
Default:
Running 'virtualcam' with no arguments = start --bg
Prerequisites:
Requires v4l2loopback module and kernel headers:
sudo apt install linux-headers-$(uname -r) v4l2loopback-dkms
--------------
VirtualCam is a tiny command-line tool that creates a virtual camera device using FFmpeg.
It was built to be lightweight, fast, and easy to control with simple commands.
No GUI, no extra services—just a clean script that can start, stop, and toggle a virtual camera on Linux.
It runs quietly in the background when launched without arguments, making it convenient for screen sharing,
testing, or feeding custom video into applications. Everything is handled through one small script.
Prerequisites:
- Linux system with /dev/video0 available for virtual camera
- FFmpeg installed and accessible in your PATH
- Python 3.x (tested with Python 3.13+)
- v4l2loopback kernel module installed
Install kernel module and headers (if missing):
sudo apt install linux-headers-$(uname -r) v4l2loopback-dkms
Load module:
sudo modprobe v4l2loopback video_nr=0 card_label="VirtualCam" exclusive_caps=1
----
HASAN
https://codebyhasan.link
10:42 PM BST (UTC+6)
Gaibandha, Bangladesh • Monday, November 24, 2025
""")
# ---------- Main ----------
if __name__ == "__main__":
# Default: background start
if len(sys.argv) == 1:
start(bg=True)
sys.exit()
cmd = sys.argv[1]
if cmd == "start":
start(bg="--bg" in sys.argv)
elif cmd == "stop":
stop()
elif cmd == "status":
status()
elif cmd == "toggle":
toggle()
elif cmd == "help":
help_menu()
else:
print("Unknown command. Use 'virtualcam help'")