FFmpeg w/ V4L2 for virtual webcam on Linux that crops, filters real uvc webcam
Wondering if it would be possible to use ffmpeg w/ v4l2 support (on say Ubuntu Linux) to create a virtual webcam device, that presents a transformed version of real webcam's output. The idea would be that the virtual webcam device could then be used by popular video conferencing applications like Teams, Zoom, Google Talk etc.
The nature of transformations I am looking at is cropping on few of the edges, applying overlay text, applying chroma-keying, background replacement etc. I am looking for a mechanism that has least system overhead as well, which is why considering ffmpeg.
Solution 1:
Yes, this is possible.
- Get the
v4l2loopback
moduleapt-get install v4l2loopback-dkms
- Load the module
modprobe v4l2loopback exclusive_caps=1 card_label="Virtual Webcam" max_buffers=2
- Run
ffmpeg(1)
reading from the actual/physical webcam and writing to the “virtual” webcam. For instance, I got some old webcam that only supports 640 pixels × 480 pixels @ 30.00 FPS (among other modes), but I like to have it cropped to a 16 : 10 aspect ratio:
Pay attention to the order of parameters. E. g. here it was crucial to specifyffmpeg -f v4l2 -i /dev/video0 -pix_fmt yuyv422 -vf crop=h=400 -f v4l2 /dev/video1
‑pix_fmt
before (any)‑vf
. - If you’ve changed the resolution:
v4l2-ctl -d /dev/video1 --set-fmt-video=width=640,height=400,pixelformat=yuyv422
- Use the virtual camera from your application and celebrate.
I presume it wasn’t worth mentioning, but for the sake of completeness: apt‑get(8)
and modprobe(8)
need to be executed with root
privileges, whereas ffmpeg(1)
and v4l2-ctl(1)
are run as a regular user usually (also) being a member of the video
group. Confer the group ownership of /dev/video*
.
PS: Some duplicate questions/answers are OK, especially if you can’t merge them because they are on different sites.