Using a DSLR as a camera in OBS Studio (and other apps) on Fedora Linux

This describes how to use a DSLR camera (I use a Nikon D7200) as an input camera into applications on Linux, specifically Fedora. This allows you to use the camera as input for applications like OBS Studio, BlueJeans conferencing, etc.

Firstly, install the pre-requisites. You need v4l2loopback, which you can either build from source, or install from the COPR repository - and run modprobe:

dnf copr enable sentry/v4l2loopback
sudo dnf install gphoto2 v4l2loopback ffmpeg

Then run modprobe to enable the v4l2loopback module. This allows it to detect the camera and create a device in under /dev:

sudo modprobe v4l2loopback exclusive_caps=1 max_buffers=2

Next, turn on your camera. Run ls /dev/video* to see which video device the camera has been assigned to - in this case, mine is /dev/video2 (because it was created when I turned my camera on):

$ ls -al /dev/video*
crw-rw----+ 1 root video 81, 0 Aug 15 11:50 /dev/video0
crw-rw----+ 1 root video 81, 1 Aug 15 11:50 /dev/video1
crw-rw----+ 1 root video 81, 2 Aug 16 13:10 /dev/video2

Then run this, replacing /dev/video2 with your allocated device name you found above:

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

The camera’s mirror will lock, and you should be able to access in OBS Studio as Video Capture Device, or in BlueJeans as an additional Camera input, even in your web browser.

Note: for some reason I needed to run pkill first, because otherwise it complains “Could not claim interface 0 (Device or resource busy)”. This seems to happen because gphoto2 launches a couple of processes which prevent other processes from capturing the video stream.