Contents

How to Use Your DSLR Camera as a Webcam in Linux

If you desire a more professional looking image when video conferencing, it turns out that you can use a Digital SLR camera as your webcam. Using a DSLR provides a number of benefits over an off-the-shelf webcam, most notably higher image quality, ability to finely adjust the frame of an image (zoom, wide angle), and adjust the depth of field. The depth of field adjustment is what allows for a nice, smooth, blurred background.


/how-to-use-your-dslr-as-a-webcam-in-linux/webcam-before-after_feature.jpg

It’s straightforward to get setup within Linux, this is the guide for how it’s done.

Hardware Needed

  • A camera supported by gPhoto, with Liveview capabilities
  • Ability to connect your camera to your Linux-based PC through a USB cable
  • Ideally some way to power your camera through a power supply. You can run from your camera’s battery, but run time will be limited

Software Needed

Software Setup

Installing the required software can be accomplished through your distro’s package manager:

Debian/Ubuntu

sudo apt-get install gphoto2 v4l2loopback-utils v4l2loopback-dkms ffmpeg

Arch Linux

pacman -S gphoto2 v4l-utils v4l2loopback-dkms ffmpeg

Fedora

sudo dnf install gphoto2 v4l2loopback ffmpeg

DSLR Configuration

Once you’ve installed the required packages, connect your camera to your PC via USB and power on the camera. You can safely ignore any OS popup message which may appear.

/how-to-use-your-dslr-as-a-webcam-in-linux/camera-popup_feature.png
KDE DSLR is Connected— Safe to Ignore

Open your Terminal application, and enter the following command:

sudo modprobe v4l2loopback exclusive_caps=1 max_buffers=2

Loading this kernel module manually (through modprobe) means you will have to remember to modprobe every time you reboot. To ensure this module is enabled when your system is booted, you need to edit one config file /etc/modules, and create a new module config file /etc/modprobe.d/dslr-webcam.conf:

  • /etc/modules:
    • Add dslr-webcam as a new line at the end of /etc/modules. If you have nothing but comments in this file, just add dslr-webcam as the last line in this file.
      $ sudo vi /etc/modules
    
      # /etc/modules: kernel modules to load at boot time.
      #
      # This file contains the names of kernel modules that should be loaded
      # at boot time, one per line. Lines beginning with "#" are ignored.
    
      dslr-webcam
    
  • Using sudo create a new file /etc/modprobe.d/dslr-webcam.conf
    • Add the following content to this file, and save
    # Module options for Video4Linux, needed for our DSLR Webcam
    alias dslr-webcam v4l2loopback
    options v4l2loopback exclusive_caps=1 max_buffers=2
    
Note
We’re using the alias functionality of modprobe.d to provide an alternate and more descriptive name (dslr-webcam) for our Video4Linux kernel module.

DSLR Testing

At this point, you should have everything configured to start testing. Still within your terminal, validate that gPhoto can see and interact with your camera:

  • gphoto2 --auto-detect
    • List auto-detected cameras and the ports to which they are connected.
  • gphoto2 --summary
    • Summary of camera status.
  • gphoto2 --abilities
    • Display the camera and driver abilities specified in the libgphoto2 driver. This all does not query the camera, it uses data provided by the library. Use --summary to query an overview of the camera.

You should now see the correct camera and gPhoto is able to report on it’s capabilities:

/how-to-use-your-dslr-as-a-webcam-in-linux/gphoto2-summary_feature.png
gPhoto2 Output

DSLR Video Testing

Still within the terminal let’s start with something simple, capturing a single photograph. This command will take a single photograph using the settings defined on your camera, and save it to your PC:

gphoto2 --capture-image-and-download
/how-to-use-your-dslr-as-a-webcam-in-linux/appleiie_feature.jpg
Apple //e Image Captured through gPhoto

Assuming that was successful, let’s try to stream video from the camera:

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

With this command, we’re:

  • Capturing date from the camera’s image sensor with gphoto2
  • Piping that data into ffmpeg, which is taking the mjpeg stream from ghoto2 and exposing it to the Video4Linux loopback device
Note

If you have a build of ffmpeg which supports GPU-based encoding, you can offload the conversion of the gphoto output to your GPU to greatly reduce the CPU load associated with this process.

Specifically you’ll want to specify -hwaccel cuvid -c:v mjpeg_cuvid:

gphoto2 --stdout --capture-movie | ~/src/ffmpeg/ffmpeg \
-hwaccel cuvid \
-c:v mjpeg_cuvid \
-i - \
-vcodec rawvideo \
-pix_fmt yuv420p \
-threads 0 \
-f v4l2 /dev/video0

You should see the verbose output from ffmpeg, with the last line providing detail on encoding status:

/how-to-use-your-dslr-as-a-webcam-in-linux/gphoto-ffmpeg_feature.png
ffmpeg Encoding Status

Now, open the VLC application, select the Media Menu -> Capture Device (Ctrl+c). Enter /dev/video0 as the “Video Device Name”, and click the “Play” button.

/how-to-use-your-dslr-as-a-webcam-in-linux/vlc-capture-device_feature.png
VLC Capture Device

You should now see a live stream from your DSLR camera!

/how-to-use-your-dslr-as-a-webcam-in-linux/vlc-live-video_feature.png
DSLR Webcam In Linux!

Video Conferencing

You now have all the components working, allowing your DSLR to be used as a webcam in Linux. Ensure that you have the gphoto2 --stdout --capture-movie command running before starting your video conferencing application. Research shows that Google Hangouts, Zoom, WebEx, BlueJeans should all work well with this setup.

/how-to-use-your-dslr-as-a-webcam-in-linux/meet-webcam_feature.png
Hangouts Meet with a Linux DSLR Webcam

Effectively using gPhoto

The capabilities of gPhoto are impressive — providing a unified interface to programmatically control hundreds of different physical cameras. With this broad capability comes some complexity — but once you understand the basics it’s not nearly as intimidating.

Three gPhoto Commands you need to know

  1. gphoto2 --list-config

    • --list-config will produce a listing of all configuration entries specific to and available for your camera.
  2. gphoto2 --get-config [config]

    • --get-config will list the type, the current value and the available options of a configuration value. As an example:
$ gphoto2 --get-config whitebalance                                
    Label: WhiteBalance
    Readonly: 0
    Type: RADIO
    Current: Auto
    Choice: 0 Auto
    Choice: 1 Daylight
    Choice: 2 Shadow
    Choice: 3 Cloudy
    Choice: 4 Tungsten
    Choice: 5 Fluorescent
    Choice: 6 Flash
    Choice: 7 Manual
  1. gphoto2 --set-config-value [config]

    • --set-config-value will set the specified configuration entry by specifying its new value. The output of — get-config will provide the values which are possible to set. Another example:
gphoto2 --set-config-value whitebalance="Daylight"

You can also chain multiple --set-config-value commands, to get the exact setup you’re looking for:

gphoto2 --set-config-value whitebalance="Daylight" --set-config-value aperture="3.5"

gPhoto Shell

gPhoto also provides an interactive shell, which is very useful for testing out various configurations:

$ gphoto2 --shell
gphoto2: {/home/ben} /> get-config whitebalance
Label: WhiteBalance
Readonly: 0
Type: RADIO
Current: Auto
Choice: 0 Auto
Choice: 1 Daylight
Choice: 2 Shadow
Choice: 3 Cloudy
Choice: 4 Tungsten
Choice: 5 Fluorescent
Choice: 6 Flash
Choice: 7 Manual
END
gphoto2: {/home/ben} /> set-config whitebalance=4