Many thanks to Ben Chapman and his blog post How to Use Your DSLR Camera as a Webcam in Linux.
v4l2loopback is a linux kernel module that allows you to create "virtual video devices". Kernel modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system.
This guide was created using the following:
Operating System | Kernel | Camera |
---|---|---|
Arch Linux x86_64 | 5.17.9-arch1-1 | Cannon EOS 1500D (Rebel T7) |
All commands are intended to be used with Arch Linux. Commands related to packages will have to be altered if using a different distrobution.
- Install kernel headers.
$ sudo pacman -S "linux$(uname -r | awk -F. '{print $1$2}')-headers"
- Install dependancies.
$ sudo pacman -S gphoto2 v4l-utils v4l2loopback-dkms ffmpeg
Test v4l2loopback kernel module by loading it upon demand.
- Load v4l2loopback kernel module.
$ sudo modprobe v4l2loopback exclusive_caps=1 max_buffers=2
- View all loaded kernel modules and confirm v4l2loopback is loaded.
$ modinfo v4l2loopback
$ lsmod
$ sudo nano /etc/modules-load.d/modules.conf
# List of modules to load at boot
dslr-webcam
$ sudo nano /etc/modprobe.d/v4l2loopback.conf
# Module options for Video4Linux, needed for our DSLR Webcam
alias dslr-webcam v4l2loopback
options v4l2loopback exclusive_caps=1 max_buffers=2
Connect camera vis usb and turn camera on. Change the following settings on the camera (Canon EOS Rebel T7), else it will not connect to PC via USB.
- Auto power off > Disable
- Wi-Fi/NFC > Disable
$ lsusb
$ gphoto2 --auto-detect
$ gphoto2 --summary`
$ gphoto2 --abilities`
If connection is verified via previous commands, try taking an image and saving it to your computer.
$ cd ~
$ gphoto2 --capture-image-and-download
$ gphoto2 --stdout --capture-movie | ffmpeg -i - -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 /dev/video0
Stream can now be reconized by various apps, such as Cheese, OBS Studio, QT V4L2 test Utility, Shotcut, VLC media playter, etc. ctrl + c
will end the stream.
If you obtain an Error (-53: 'Could not claim the USB device')
from gphoto, killing all gphoto processes will resolve the issue. Use $ pkill -f gphoto2
.
You can create an alias to so you do not have to copy and paste the command each time. Bash users can add the following to ~/.bashrc
, ZSH .zshrc
, and oh-myzsh ~/.oh-my-zsh/custom/alias.zsh
(or whichever custom file you create).
alias start_dslr='pkill -f gphoto2 && gphoto2 --stdout --capture-movie | ffmpeg -i - -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 /dev/video0'
Restarting your terminal session will include alias. You can then start the stream with $ start_dslr
.