How to Use Your DSLR Camera As a Webcam in Linux

Ben Chapman
Mar 7, 2020 · 7 min read

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. It’s straightforward to get setup within Linux, this is the guide for how it’s done.

This article was originally published at Cracked the Code. Head over there for the latest updates and an improved reading experience.

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:

Before Getting Started:

The configuration as detailed here works very well for most people. There are however a few important limitations to be aware of before you get started:

  1. You will not capture the full resolution of your camera’s image sensor.

This guide uses the gPhoto2 application and associated libraries, which enables you to control your camera through terminal commands. gPhoto2's movie capture mode captures a series of preview frames from your camera and outputs them as a motion JPEG video format. The resolution of the preview frames captured from your camera varies, but will likely be less than 720p (1280x720).

It would be great to capture an HD quality image from a large, sophisticated and expensive camera, but in my experience the resolution doesn’t matter as much as you might think. The greatly enhanced optics, large image sensor, and depth of field provided by a DSLR camera contribute to a much better looking image.

2. Video capture and encoding is CPU intensive and your system may have performance issues.

I developed this guide on an AMD Ryzen 7 workstation which had the processing power to ingest and transcode the video stream from my camera with no issues. Make no mistake though, video transcoding (as we’re doing here) is CPU intensive, and some lower-powered computers will have performance issues. It’s hard to say what the minimum specs are for this guide, but an Intel i7 class processor is close to the minimum needed to use this effectively.

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

Video4Linux 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.

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:

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 Webcamalias 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.

gPhoto2 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 database. 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:

gPhoto2 Output

DSLR Webcam 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
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 decoding of the gphoto output to your GPU to reduce the CPU load associated with this process.

You can verify if you have capability for GPU offload by running ffmpeg -hide_banner -decoders |grep mjpeg_cuvid. If this command returns V..... mjpeg_cuvid Nvidia CUVID MJPEG decoder (codec mjpeg) (or something similar), you’re all set.

For GPU-based decoding you’ll want to pass-hwaccel nvdec -c:v mjpeg_cuvid into your ffmpeg command:

$ gphoto2 --stdout --capture-movie | ffmpeg -hwaccel nvdec -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:

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.

VLC Capture Device

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

DSLR Webcam In Linux

Video Conferencing with a DSLR Webcam

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-moviecommand running before starting your video conferencing application. Google Meet, Zoom, Microsoft Teams, WebEx, BlueJeans all work well with this setup.

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

3. 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

Nerdery by Ben Chapman

Adventures in electronics, tinkering and retro computing

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store