Using digicam as a webcam on Linux

A short memo on how to use a digital camera as a web camera on Linux.

I recently dug out a pretty old Canon EOS 550D, and since I only have a poor built-in webcam, that was a chance to improve my video quality significantly!

Once plugged into a USB port, the camera is detected just as storage. We need two things to make it work properly:

  1. Stream the video feed from it
  2. Pipe this feed into a virtual video input device

The first step is pretty simple — just use gphoto2:

$ # Check if the camera is detected correctly
$ gphoto2 --abilities
Abilities for camera             : Canon EOS 550D                              
Serial port support              : no
USB support                      : yes
Capture choices                  :
                                 : Image
                                 : Preview
                                 : Trigger Capture
Configuration support            : yes
Delete selected files on camera  : yes
Delete all files on camera       : no
File preview (thumbnail) support : yes
File upload support              : yes
$ # Now get the video!
$ gphoto2 --stdout --capture-movie
Capturing preview frames as movie to 'stdout'. Press Ctrl-C to abort.
<enjoy the matrix>

The second piece of the puzzle is v4l, in particular, v4l2loopback. If you’re using Arch like me, you can just install it:

pacman -S v4l2loopback-utils

This will also install the required DKMS package to bring in the kernel module named v4l2loopback.

Now run

sudo modprobe v4l2loopback

To create a virtual video device. You can check the kernel logs for the exact path in my case, it was /dev/video2

Now we can stream the video feed from the camera to that device using ffmpeg!

gphoto2 --stdout --capture-movie | ffmpeg -i - -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 /dev/video2

And we’re done!